Programmatically Putting TopComponents in Modes

First a picture:

The list of menu items above appears when the TopComponent's tab is right-clicked. When a menu item is selected, the TopComponent moves to the selected mode.

Next, the two things you need to know to implement this:

  • How to specify the mode. For each menu item, create a Java class implementing AbstractAction. In the actionPerformed method, send the name of the mode (e.g., "properties" or "editor") as a string to the TopComponent. In the TopComponent, define the open() method like this:

    public void open() {
        Mode mode = WindowManager.getDefault().findMode(selectedMode);
        if (mode != null) mode.dockInto(this);
        super.open();
    }

    Above, selectedMode is a string, received from the actionPerformed method in the AbstractAction implementation.

  • How to put the actions in the TopComponent's tab. Override the TopComponent's getActions method. Like this:

    public Action[] getActions() {
        return new Action[]{
            new OpenExplorerWindowAction(),
            new OpenEditorWindowAction(),
            new OpenPropertiesWindowAction(),
            new OpenOutputWindowAction(),
            new OpenNavigatorWindowAction()
        };
    }

And that's all you need to know to let the user move a TopComponent via a menu item. Of course, they could also just drag and drop the TopComponent via its tab, but by providing the menu items in the tab, the functionality is less hidden. You could also use the code above in a different way, maybe you want the TopComponent to move to a different mode, depending on something done by the user in the application.

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
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today