Programmatically Putting TopComponents in Modes

http://blogs.oracle.com/geertjan/entry/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.

原文地址:https://www.cnblogs.com/cuizhf/p/2176051.html