Download NetBeans!

20071119 Monday November 19, 2007

SaveCookie (Part 2)

SaveCookie (Part 1) worked but wasn't very efficient, because we were passing a field around, i.e., a boolean that was set whenever a text field was modified. Plus, we could have used getCookieSet().assign to assign our implementation of SaveCookie to the cookie set. So, here's the new and improved save functionality for a text field in a TopComponent, with help from Jarda to create it. As before, we begin by creating a node in the TopComponent.

private class DummyNode extends AbstractNode {

    SaveCookieImpl impl;

    public DummyNode() {
        super(Children.LEAF);
        impl = new SaveCookieImpl();
    }

    public void fire(boolean modified) {
        if (modified) {
            //If the text is modified,
            //we implement SaveCookie,
            //and add the implementation to the cookieset:
            getCookieSet().assign(SaveCookie.class, impl);
        } else {
            //Otherwise, we make no assignment
            //and the SaveCookie is not made available:
            getCookieSet().assign(SaveCookie.class);
        }
    }

    private class SaveCookieImpl implements SaveCookie {

        public void save() throws IOException {

            Confirmation msg = new NotifyDescriptor.Confirmation("Do you want to save \"" + 
                guessedWord.getText() + "\"?", NotifyDescriptor.OK_CANCEL_OPTION, 
                NotifyDescriptor.QUESTION_MESSAGE);

            Object result = DialogDisplayer.getDefault().notify(msg);

            //When user clicks "Yes", indicating they really want to save,
            //we need to disable the Save button and Save menu item,
            //so that it will only be usable when the next change is made
            //to the text field:
            if (NotifyDescriptor.YES_OPTION.equals(result)) {
                fire(false);
                //Implement your save functionality here.
            }

        }
    }
}

We'll need to refer to this node in other places, so let's create a class variable:

private DummyNode dummyNode;

Here's how the node is added to the TopComponent, i.e., in the constructor:

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

And then we notify the node whenever changes are made to the text field:

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

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

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

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

Because we pass true to the node, our implementation of SaveCookie is added to the cookie set, which enables the Save button (if you have one, which you don't by default), as well as the Save menu item under the File menu. If you want a Save button in the toolbar, add this to the layer:

<folder name="Toolbars">
    <folder name="File">
        <file name="org-openide-actions-SaveAction.instance"/>
    </folder>
</folder>

In other news. Have a look at this pretty brilliant clip on YouTube: The future is in the past!

Nov 19 2007, 04:33:29 AM PST Permalink