Download NetBeans!

20090713 Monday July 13, 2009

The Divorce of SaveCookies from Nodes

I am happy to announce the permanent separation between Nodes and SaveCookies. If you're not happy with this announcement, then you're unlikely to benefit much from this blog entry. :-) (But here is a tiny bit of background.)

Using a build from sometime after the beginning of this month (or just update your trunk), I was able to do this:

...
...
...

    private InstanceContent content;
    private SaveCookieImpl impl;

    public DemoTopComponent() {
        initComponents();
        setName(NbBundle.getMessage(DemoTopComponent.class, "CTL_DemoTopComponent"));
        setToolTipText(NbBundle.getMessage(DemoTopComponent.class, "HINT_DemoTopComponent"));
    
        //JTextArea fires changes when the document changes:
        jTextArea1.getDocument().addDocumentListener(new DocumentListener() {
            public void insertUpdate(DocumentEvent arg0) {
                fire(true);
            }
            public void removeUpdate(DocumentEvent arg0) {
                fire(true);
            }
            public void changedUpdate(DocumentEvent arg0) {
                fire(true);
            }
        });

        //Create a new instance of our SaveCookie implementation:
        impl = new SaveCookieImpl();

        //Create a new instance of our dynamic object:
        content = new InstanceContent();

        //Add the dynamic object to the TopComponent Lookup:
        associateLookup(new AbstractLookup(content));

    }

    private class SaveCookieImpl implements SaveCookie {
        @Override 
        public void save() throws IOException {
            NotifyDescriptor.Message message = new NotifyDescriptor.Message("Do you really want to save?");
            Object result = DialogDisplayer.getDefault().notify(message);
            //When user clicks "Yes", indicating they really want to save,
            //we need to disable the Save action,
            //so that it will only be usable when the next change is made
            //to the JTextArea:
            if (NotifyDescriptor.YES_OPTION.equals(result)) {
                fire(false);
                //Implement your save functionality here.
            }
        }
    }

    public void fire(boolean modified) {
        if (modified) {
            //If the text is modified,
            //we add SaveCookie impl to Lookup:
            content.add(impl);
        } else {
            //Otherwise, we remove the SaveCookie impl from the lookup:
            content.remove(impl);
        }
    }

...
...
...

Notice that there is no Node at all above. I'm simply using the standard pattern with InstanceContent to add/remove the SaveCookie to/from the InstanceContent upon changes to the document. Hurray!

Update 23 July 2009. How to add the Save button:

<folder name="Toolbars">
    <folder name="File">
        <file name="org-openide-actions-SaveAction.shadow">
            <attr name="originalFile" stringvalue="Actions/System/org-openide-actions-SaveAction.instance"/>
            <attr name="position" intvalue="444"/>
        </file>
    </folder>
</folder>

Jul 13 2009, 11:24:25 AM PDT Permalink