X

Geertjan's Blog

  • August 21, 2010

Integrating an OutlineView with the Save Action

Geertjan Wielenga
Product Manager
When the user clicks the Enter key in your OutlineView, you'd like the Save Action to become enabled, so that the Save button (and Save menu item) can be clicked. For example, while the user is changing something, as shown below, the Save button should not be enabled:

However, once the user is happy with the change and confirms it by pressing the Enter key, the Save button should be enabled, as shown below:

How to do this? The Outline component, which provides the table above, inherits from org.netbeans.swing.etable.ETable, which inherits from JTable. So, there's nothing new here:

KeyStroke enterKey = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true);
outlineView1.getOutline().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(enterKey, "enter");
outlineView1.getOutline().getActionMap().put("enter", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
fire(true);
}
});

When Enter is pressed, "true" is passed to "fire". And that's been discussed before, i.e., this is where you add the SaveCookie to the InstanceContent:

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

That's all. Now you've integrated your OutlineView with the NetBeans Platform's save functionality.

Be the first to comment

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