Download NetBeans!

20071116 Friday November 16, 2007

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.

Nov 16 2007, 12:31:15 AM PST Permalink