X

Geertjan's Blog

  • April 14, 2008

org.openide.util.ChangeSupport

Geertjan Wielenga
Product Manager
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.

Join the discussion

Comments ( 4 )
  • Carl van Denzen Thursday, March 18, 2010

    I cannot find org.openide.ChangeSupport anymore in Netbeans 6.5. Is there a replacement?

    Carl


  • Riggs Wednesday, February 15, 2012

    Hi Geertjan,

    Was wondering if you did blog about this after this post in another location or is this code the correct/updated code?

    Thanks for all the valuable blogs you make!


  • SN Tuesday, May 19, 2015

    Don't get what the panel in "changeSupport.addChangeListener(panel);" is? Where does it come from?


  • Geertjan Wednesday, May 20, 2015

    What do you think the 'panel' is yourself, when you look at the code above? Also, get the book: https://leanpub.com/nbp4beginners


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.