03 July 2009

Linux or Windows on my Notebook?

A friend had a netbook with Ubuntu Netbook Remix. It looked like a nice OS that simplified the netbook experience.

When I finally bough my own netbook I was more or less convinced that I would throw out the existing operative system and install Linux on it. However I have not felt the need to do so. My notebook is fast enough to run Windows XP Home. With the hibernate mode the netbook is relatively fast to boot/start. In the end I don't feel like going through the hassle of reinstalling anything.

If the it had come pre-installed with Linux my feeling is that I would have kept that OS.

29 June 2009

Micro USB as a standard on mobile phones

Moble Manufacturers argee to use Micro USB interface to connect to new mobile phones. So what is Mirco USB? Well the answer to that was not so simple to find. One of the pictures on Wikipedia that first claimed to show a Micro USB does not look like it...

I downloaded the USB 2.0 specfication and found this picture:


There is also a micro-A plug. An USB A-type plug are often found at places that gives power (for example your computer). A B plug can most often be found at place that draw power (for example your mobile phone).

To confuse things a bit more the connectors in the USB 3.0 specifications looks "half" different. And in the USB 3.0 specification there is also a AB-plug that can take both a A and B. But the USB 3.0 specification uses more wires so it is a completely different story.

Whats the lesson here? First of all don't always trust Wikipedia! Second, the mist behind an article on a new site can take some time to disperse.

27 June 2009

My Compaq Mini

I've bought a Compaq Mini 700 series (730eo) netbook computer. The price was 3000 Swedish kronor (SEK). I've used it for 2 weeks now and gotten myself an opinion of how it is so here is my review.

Pros:
  • Small and light. I put my computer on my kitchen scale and it was 1066 grams. The trend now is bigger netbooks but this is still in the smaller range.
  • Good keyboard. I've tested a bunch of netbooks at my local store and this had one of the best keyboards
  • Good value for money.


Cons:
  • Noisy fan and bad fan control. The fan really makes some noise and the fan control is not that good at adapting the speed of the fan. I have solved this problem and I'll post another blog post about it later
  • Slow hard drive (4200 RPM). But it is good enough for me. I can even develop Java programs in Eclipse with it. So it does not bug me that much.
  • Only a 3 cell battery that lasts 2 hours (3 max). I guess that is a compromise for its small wiegth
  • Low quality web cam. Other netbooks have much better (I tried these in the store as well). But that does not bother me that much.

Here is a picture of the keyboard layout (Swedish):


One thing I really like with a netbook is that I can move it around my apartment so easy. I read websites at the kitchen table, use it at the couch when I watch tv and can have it in my lap when I sit at the balcony.

Update: Here is how I fixed the fan control: http://blog.lesc.se/2009/10/how-to-improve-fan-control-on-hpcompaq.html

26 June 2009

Resize a dialog with a multi line text field that has fixed width

stackoverflow.com is a great forum to ask questions. Sometimes an answer to a question you ask there will trigger yourself to think in new ways so you can answer you own question. This happened to me in the problem with the dialog with the multi line text field with fixed width: http://stackoverflow.com/questions/1048224/get-height-of-multi-line-text-with-fixed-width-to-make-dialog-resize-properly

Base class for ChangeEvent/ChangeListener mangament

If you have written a data model you often want to be able to notify others about the change in the model by using some kind of listener. The Java swing ChangeListener interface is one choice of listener to choose. But the management of a listener list (adding, removing and notification of listeners) is a bit of a hazel to write code for each time you have a model. I searched the Java API but could not find any base class of helper class to solve this. The closest I got was the EventListenerList class. By using this class I've written my own base/helper class:
import javax.swing.event.*;

/**
* Convenience class to notify others of changes
* Can be used as a base class or as an aggregate/helper
*/
public class ChangeEventDispatcher {

protected EventListenerList listenerList = new EventListenerList();
protected ChangeEvent changeEvent = null;

public void addChangeListener(ChangeListener l) {
listenerList.add(ChangeListener.class, l);
}

public void removeFooListener(ChangeListener l) {
listenerList.remove(ChangeListener.class, l);
}

/**
* Called by by child/wrapper class to dispatch a Change event
*
* Code is copied from EventListenerList documentation and adapted to ChangeEvent
*/
public void fireChangeEvent() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length-2; i>=0; i-=2) {
if (listeners[i]==ChangeListener.class) {
// Lazily create the event:
if (changeEvent == null)
changeEvent = new ChangeEvent(this);
((ChangeListener)listeners[i+1]).stateChanged(changeEvent);
}
}
}
}

An example of how to use this class:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

@SuppressWarnings("serial")
public class ChangeTester extends JFrame {

private JButton increaseButton = new JButton("Increase");
private JLabel valueLabel = new JLabel();

private ValueModel model = new ValueModel();

public ChangeTester() {
setLayout(new FlowLayout());
add(increaseButton);
add(valueLabel);

//Change the model when pressing the button
increaseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.increaseValue();
}
});

//The label should be updated when the model is changed
model.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
updateLabelValue();
}
});

updateLabelValue();
pack();
}

private void updateLabelValue() {
valueLabel.setText(Integer.toString(model.getValue()));
}

public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new ChangeTester();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}

/**
* Simple model that extends the ChangeEventDispatcher
* to get listener list handling
*/
public static class ValueModel extends ChangeEventDispatcher {
private int value;

public void increaseValue() {
this.value++;
fireChangeEvent(); //Notify listeners
}

public int getValue() {
return value;
}
}
}

Quick close a dialog by escape key

I Windows there is a convention that a dialog is closed (and canceled) if the user pressed the Esc key. There is also a convention that pressing Enter closes (and confirms) the dialog. In Java there is no default implementation of this.

So we need to implement this ourself. A naive approach would be to add a KeyListener to the dialog. This will not work if the keyboard focus is in a nested component (for example a text field). So how about to add a key listener to both the dialog and all its subcomponents? Well this would work, and I have successfully implemented this using a ContainerListener in the dialog so any components added in the dialog also gets a key listener. But there is a simpler way: the key bindings way!
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;

@SuppressWarnings("serial")
public class QuickCloseDialog extends JDialog {

public QuickCloseDialog() {
initCloseListener();
add(new JTextField("A text field"));
pack();
}

private void initCloseListener() {
getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE , 0), "close");
getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "close");
getRootPane().getActionMap().put("close", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}

public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JDialog dialog = new QuickCloseDialog();
dialog.setVisible(true);
}
});
}
}

With a key binding you don't have to care where the keyboard focus is.

Update: My solution is probably not thread safe, this alternative solution is: http://stackoverflow.com/questions/642925/swing-how-do-i-close-a-dialog-when-the-esc-key-is-pressed

25 June 2009

Static import and GroupLayout tip for Eclipse

I you like GroupLayout and use Eclipse this might be a good tip:
Eclipse is not that good with static imports but it is possible to make the content assist suggest and automatically add static imports. Go to Preferences -> Java -> Editor -> Content Assist -> Favorites and add the types javax.swing.GroupLayout.*, javax.swing.GroupLayout.Alignment.* and javax.swing.LayoutStyle.ComponentPlacement.*:

It is now possible to use the Content Assist (Ctrl + Space) to get help when writing the GroupLayout properties: