Integrating an OutlineView with the Save Action

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.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today