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.

Comments:

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

Carl

Posted by Carl van Denzen on March 18, 2010 at 08:39 AM PDT #

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!

Posted by Riggs on February 15, 2012 at 04:29 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today