surviveFocusChange=true

Here's a very cool thing that I keep forgetting about but that Jesse reminded me of in the recent blog entries on Undo/Redo: "surviveFocusChange=true".

Look at the screenshot below. You see two windows with a toolbar button. The toolbar button is enabled whenever an object named "Bla" is in the Lookup. The "Demo" window has a "Bla" object in its Lookup and hence the toolbar button is enabled when the focus is in the "Demo" window, as shown below:

Now the focus is in the "Output" window, which does not have a "Bla" object in its Lookup and hence the button is disabled:

However, there are scenarios where you might like the button to remain enabled even when the focus changes. (One such scenario is the Undo/Redo scenario in this blog a few days ago, i.e., even when the Properties window has the focus the Undo/Redo buttons should be enabled.)

Here you can see that the button is enabled even though the focus has switched to the "Output" window:

How to achieve this? Well, you need to register your Action to have "surviveFocusChange" set to "true". It is, by default, set to "false":

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionReferences;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle.Messages;

@ActionID(category = "File", id = "org.mymodule.BlaAction")
@ActionRegistration(surviveFocusChange=true, iconBase = "org/mymodule/Datasource.gif", displayName = "#CTL_BlaAction")
@ActionReferences({
    @ActionReference(path = "Toolbars/Bla", position = 0)
})
@Messages("CTL_BlaAction=Bla")
public final class BlaAction implements ActionListener {

    private final Bla context;

    public BlaAction(Bla context) {
        this.context = context;
    }

    @Override
    public void actionPerformed(ActionEvent ev) {
        // TODO use context
    }
 
}

That's all. Now folders and files will be created in the NetBeans Platform filesystem from the annotations above when the module is compiled such that the NetBeans Platform will automatically keep the button enabled even when the user switches focus to a window that does not contain a "Bla" object in its Lookup. Hence, the same "Bla" object will remain available when switching from one window to another, until a new "Bla" object will be made available in the Lookup.

Comments:

"keep the button enabled even when the user switches focus to a window that does not contain a "Bla" object in its Lookup" - almost. If the focus is switched to a window such as Services which _can have_ selections, but which does not happen to have a Bla in its current selection (either because that selection is empty or because it just contains something unrelated), then the button will be disabled. Surviving focus change just means the button will remain enabled if focus is switched to a window which _never has_ selections, such as the Output Window.

Posted by Jesse Glick on November 28, 2011 at 08:31 AM PST #

That's interesting, it seems surviveFocusChange=true causes behaviour that is very close to that of StickyLookup (as if wrapping Utilities.actionsGlobalContext(), for a single TC, inside a StickyLookup).

Posted by Ernest on November 28, 2011 at 04:36 PM PST #

Another useful blogentry to help everyone. Thanks a lot. But maybe you should add j.glick's explanation to your posting. Without it i was not able to understand it.

Posted by j.boesl on November 28, 2011 at 06:53 PM PST #

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
« July 2014
SunMonTueWedThuFriSat
  
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today