X

Geertjan's Blog

  • November 1, 2006

Enabling the NetBeans Platform's Menu Items

Geertjan Wielenga
Product Manager
I've kind of been wondering how to enable those greyed out menu items that the NetBeans Platform gives you:

And today, in an old e-mail thread on dev@openide.netbeans.org, I found out about CallbackSystemAction. Using this NetBeans API class, you can register an action with a TopComponent. When the TopComponent opens, the menu item is no longer greyed out and delegates to your own action. For example, I enabled the Find and Replace menu items, as can be seen here:

To do this, I filled out the TopComponent's getDefault() method (which was generated by the Window Component wizard). The lines in bold below are my additions:

public static synchronized TestTopComponent getDefault() {
if (instance == null) {
instance = new TestTopComponent();ActionMap actionMap = instance.getActionMap();
CallbackSystemAction callFindAction = (CallbackSystemAction)SystemAction.get(FindAction.class);
actionMap.put(callFindAction.getActionMapKey(), new MyFindAction());
CallbackSystemAction callReplaceAction = (CallbackSystemAction)SystemAction.get(ReplaceAction.class);
actionMap.put(callReplaceAction.getActionMapKey(), new MyReplaceAction());

}
return instance;
}

Then I created two new actions, one called MyFindAction and the other MyReplaceAction. (I could create different action classes like this per TopComponent, because each TopComponent can have its own ActionMap with its own actions that can be delegated to from the menu items in the NetBeans Platform's menu bar.) It's good to see those menu items enabled at last! And now I understand them better: they're nothing more than placeholders for your own code, saves you some time, because you don't need to code the presentation of your menu item, just the code that is performed when the menu item is chosen by the user. More info can also be found here.

Join the discussion

Comments ( 6 )
  • Anton Epple Wednesday, November 1, 2006
    Hi Geertjan, thanks a lot. Some tutorial that shows how to activate those menu items is something I was really missing. I already had some hard time finding out how to enable the undo/redo and copy/paste. I'm looking forward to using the find action in my components. cheers, Toni
  • Sven Reimers Wednesday, November 1, 2006
    Some of those actions can also be enabled via Nodes. The selected Node has to provide the correct Cookies inside it's cookieSet (lookup should work as well) so that the associated CookieAction can be enabled.
    One example is the Delete you can see in the Menu. Do you need more info?
  • Anton Epple Thursday, November 2, 2006
    There was a question on the dev@openide.netbeans.org today on how to activate the undo/redo, so I thought I'd give an example:
    http://maxnitribitt.blog-city.com/netbeans_undoredo_for_topcomponent.htm
  • Geertjan Thursday, November 2, 2006
    Hi Toni and Sven. Thanks for the comments. Toni, I looked at your blog and I will try it out soon. Looks interesting and is a great tip. Maybe you can add a link to it from the right place in the Wiki (http://wiki.netbeans.info/wiki/view/NetBeansDeveloperFAQ), I guess somewhere within the section on actions. Sven, I'm pretty sure that I know what you mean. It's interesting that these menus, which all look the same, seem to have different ways of enabling them. Thanks both for the comments.
  • Joao Francisco Medeiros Neto Friday, May 15, 2009

    Dear Friend,

    How I use the default netbeans find / replace dialog ?


  • jagadeesan Wednesday, May 5, 2010

    Hi Geertjan,

    thanks a lot.

    I have some query here,

    I have a SystemAction defined that I would like to enable/disable programmatically within a TopComponent.

    The following options are I Tried

    Options -1

    public class TopNode extends AbstractNode{

    .

    ..

    ....

    public Action[] getActions(boolean popup) {

    return new SystemAction[] {

    SystemAction.get(OpenAction.class)}

    }

    It is showing the menu but, it is not enable

    Options-2

    class RootTopComponent extends TopComponent{

    public RootTopComponent () {

    initComponents();

    getActionMap().put("rename", RenameAction.class));

    put(DeleteAction.get(DeleteAction.class).getActionMapKey(),

    ExplorerUtils.actionDelete(projectViewerExplorerMgr, true));

    ...

    projectViewerExplorerMgr.setRootContext(new RootNode(rootFolderObject));

    associateLookup(ExplorerUtils.createLookup(projectViewerExplorerMgr, getActionMap()));

    ...

    }

    @Override

    public void componentActivated() {

    ExplorerUtils.activateActions(projectViewerExplorerMgr, true);

    }

    @Override

    public void componentDeactivated() {

    ExplorerUtils.activateActions(projectViewerExplorerMgr, false);

    }

    }

    MyTopcomponent class:

    public final class DiskExplorerTopComponent extends TopComponent {

    private static final String MODE = "explorer";

    OpenAction callFindAction = SystemAction.get(OpenAction.class);

    public MyTopComponent() {

    tree = new BeanTreeView();

    tree.setDragSource(true);

    tree.setRootVisible(false);

    ActionMap actionMap = getActionMap();

    map.put(DefaultEditorKit.cutAction, ExplorerUtils.actionCut(explorerManager));

    map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(explorerManager));

    map.put("rename", SystemAction.get(RenameAction.class));

    map.put("delete", ExplorerUtils.actionDelete(explorerManager, true)); // NOI18N

    setLayout(new java.awt.BorderLayout());

    add(tree, java.awt.BorderLayout.CENTER);

    associateLookup(ExplorerUtils.createLookup(explorerManager, actionMap));

    Node node = new AbstractNode(Children.create(new RootChildren(), true));

    explorerManager.setRootContext(node);

    }

    public static synchronized DiskExplorerTopComponent getDefault() {

    ///here i am getting the default instance

    }

    public ExplorerManager getExplorerManager() {

    return explorerManager;

    }

    protected void componentActivated() {

    ExplorerUtils.activateActions(explorerManager, true);

    }

    protected void componentDeactivated() {

    ExplorerUtils.activateActions(explorerManager, false);

    }

    }

    I heave tried these options,but menus are not enable


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