SaveCookie (Part 1)

Let's switch on the Save button (and the related Save menu item under File menu) for a text field. First, we need to listen to changes in the text field and then set a boolean on the change:

private boolean change = false;

myTextField.getDocument().addDocumentListener(new DocumentListener() {

    public void insertUpdate(DocumentEvent arg0) {
        change = true;
    }

    public void removeUpdate(DocumentEvent arg0) {
        change = true;
    }

    public void changedUpdate(DocumentEvent arg0) {
        change = true;
    }

});

Now we are listening on the text field and whenever something changes within it, a boolean is set to true. Next, we create a node in the TopComponent that holds the text field. A TopComponent has an activated node, which we set in the TopComponent's constructor:

setActivatedNodes(new Node[]{dummyNode = new DummyNode()});

And declare the node at the top of the class:

private DummyNode dummyNode;

And here is the node itself. We are dealing here with cookies, which are capabilities. If the capability to save is available (which is true by default for TopComponents) and our boolean is set to true, the save button (and menu item under File menu) will be enabled. Whenever we want to tell the node that we have made a change, we call fire(), which fires a change event on the node. So, below we call fire() after the button is enabled, at which point we set the boolean to false, so that our fire() will result in the button being disabled, thus toggling on/off when clicked.

private class DummyNode extends AbstractNode {

    public DummyNode() {
        super(Children.LEAF);
    }

    @Override
    public Node.Cookie getCookie(Class type) {
        if (type == SaveCookie.class && change == true) {
            return new SaveCookie() {
                public void save() throws IOException {
                    change = false;
                    fire();
                    Confirmation msg = new NotifyDescriptor.Confirmation(
                            "Do you want to save \\"" + guessedWord.getText() + "\\"?",
                            NotifyDescriptor.OK_CANCEL_OPTION,
                            NotifyDescriptor.QUESTION_MESSAGE);
                    DialogDisplayer.getDefault().notify(msg);
                    //Implement your save functionality here.
                }
            };
        } else {
            return super.getCookie(type);
        }
    }

    public void fire() {
        fireCookieChange();
    }

}

Now we need to also fire change events in the document listener. Below I've highlighted the code I added:

myTextField.getDocument().addDocumentListener(new DocumentListener() {

    public void insertUpdate(DocumentEvent arg0) {
        change = true;
        dummyNode.fire();
    }

    public void removeUpdate(DocumentEvent arg0) {
        change = true;
       dummyNode.fire();
    }

    public void changedUpdate(DocumentEvent arg0) {
        change = true;
       dummyNode.fire();
    }

});

And that's it. Now the Save button (and menu item under File menu) is enabled whenever a change is made in the text field. At that point, a small dialog appears asking whether you want to save. When the user says OK, something should happen, maybe the content of the text field ends up stored in a file. But that's something you need to decide on and implement yourself. Here the focus is purely on one way of integrating your text field with the NetBeans Platform, i.e., by hooking it up to the NetBeans Platform's Save functionality.

Comments:

Two comments on the above code from Jaroslav Tulach: "Instead of the change field, I suggest to use
AbstractNode.getCookieSet().assign(SaveCookie.class, saveInstance);
and
AbstractNode.getCookieSet().assign(SaveCookie.class);

Second note is less important: this might be deadlock prone, as documentlistener is notified under Document's internal lock and the SaveCookie change calls into unknown code which could deadlock with this lock. But I guess for demo purposes, you can ignore this."

Posted by Geertjan on November 15, 2007 at 08:44 PM PST #

See here for an update to this blog entry:
http://blogs.sun.com/geertjan/entry/savecookie_part_2

Posted by Geertjan on November 18, 2007 at 08:35 PM PST #

Can you provide me a solution how to change the same code to JEditorPane instead ov textfield

Posted by Ranjith V on June 05, 2008 at 01:05 AM PDT #

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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today