X
  • November 16, 2007

SaveCookie (Part 1)

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

Join the discussion

Comments ( 3 )
  • Geertjan Friday, November 16, 2007

    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."


  • Geertjan Monday, November 19, 2007

    See here for an update to this blog entry:

    http://blogs.sun.com/geertjan/entry/savecookie_part_2


  • Ranjith V Thursday, June 5, 2008

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


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

Integrated Cloud Applications & Platform Services