X

Geertjan's Blog

  • July 13, 2009

The Divorce of SaveCookies from Nodes

Geertjan Wielenga
Product Manager
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>

Join the discussion

Comments ( 4 )
  • Petr Dvorak Tuesday, July 14, 2009

    This is certainly a great news as the permanent obfuscation with a "dummy node" was quite annoying, it is just pity it is not in 6.7:(...


  • Gerrick Bivins Monday, September 14, 2009

    Is this in 6.7.1?


  • Peter Kirkham Tuesday, October 27, 2009

    I read this post quite a long time ago, so when I recently switched to NB 6.8 beta and my save functionality stopped working I was very relieved to remember that you'd blogged about this. Thanks for the code snippet. I think the save cookie really makes much more sense now (to my mind anyway).


  • Zoran Sevarac Thursday, February 14, 2013

    Hi Geertjan,

    I think this doesnt work for NB 7.2.

    It wont enable save unless you have org.openide.windows.DelegateActionMap in your lookup.

    To make it work you must something like:

    public Lookup getLookup() {

    return new ProxyLookup(

    new Lookup[]{

    super.getLookup(),

    Lookups.fixed(...)

    });

    }

    See http://netbeans-org.1045718.n5.nabble.com/SaveCookie-problem-with-other-Lookups-td3345279.html

    http://wiki.netbeans.org/DevFaqNodesCustomLookup


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.