Monday April 14, 2008
org.openide.util.ChangeSupport
I'm learning about org.openide.util.ChangeSupport. Some notes below.
In the visual panel:
private final ChangeSupport changeSupport = new ChangeSupport(this);
private final DocumentListener docListener = new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
fireChange();
}
public void removeUpdate(DocumentEvent e) {
fireChange();
}
public void changedUpdate(DocumentEvent e) {
fireChange();
}
private void fireChange() {
changeSupport.fireChange();
}
};
In the visual panel constructor:
field1.getDocument().addDocumentListener(docListener); field2.getDocument().addDocumentListener(docListener); changeSupport.addChangeListener(panel);
In the wizard panel, implement ChangeListener and then:
private ChangeSupport changeSupport = new ChangeSupport(this);
@Override
public final void addChangeListener(ChangeListener l) {
changeSupport.addChangeListener(l);
}
@Override
public final void removeChangeListener(ChangeListener l) {
changeSupport.removeChangeListener(l);
}
public void stateChanged(ChangeEvent e) {
changeSupport.fireChange();
}
Then an experiment with DataObject and a DataNode. This in the DataObject:
private final ChangeSupport changeSupport = new ChangeSupport(this);
private final FileChangeListener fileListener = new FileChangeListener() {
public void fileFolderCreated(FileEvent arg0) {
}
public void fileDataCreated(FileEvent arg0) {
}
public void fileChanged(FileEvent arg0) {
changeSupport.fireChange();
}
public void fileDeleted(FileEvent arg0) {
}
public void fileRenamed(FileRenameEvent arg0) {
}
public void fileAttributeChanged(FileAttributeEvent arg0) {
}
};
And in the constructor of the DataObject:
getPrimaryFile().addFileChangeListener(fileListener); changeSupport.addChangeListener(createNodeDelegate());
This in the DataNode:
public class DemoDataNode extends DataNode implements ChangeListener {
private ChangeSupport changeSupport = new ChangeSupport(this);
DemoDataObject obj;
Date date;
public DemoDataNode(DemoDataObject obj) {
super(obj, Children.LEAF);
this.obj = obj;
date = new Date();
changeSupport.addChangeListener(this);
}
DemoDataNode(DemoDataObject obj, Lookup lookup) {
super(obj, Children.LEAF, lookup);
setName(obj.getPrimaryFile().getName());
}
public void stateChanged(ChangeEvent e) {
long mills = System.currentTimeMillis();
DateFormat dateFormatter = DateFormat.getDateTimeInstance(
DateFormat.LONG,
DateFormat.LONG);
String formatted = dateFormatter.format(mills);
setName("Changed: " + formatted);
changeSupport.fireChange();
}
}
Something is wrong because I can only change the name, not the display name or the icon. Probably need to do something with fireIconChange and fireDisplayNameChange on the Node.
Update. Fixed the problem. Some of the code above is wrong. Will blog about this soon.
Apr 14 2008, 11:51:50 PM PDT Permalink


