Enabling the NetBeans Platform's Menu Items

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.

Comments:

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

Posted by Anton Epple on November 01, 2006 at 02:40 AM PST #

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?

Posted by Sven Reimers on November 01, 2006 at 05:40 AM PST #

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

Posted by Anton Epple on November 01, 2006 at 06:55 PM PST #

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.

Posted by Geertjan on November 01, 2006 at 11:53 PM PST #

Dear Friend,

How I use the default netbeans find / replace dialog ?

Posted by Joao Francisco Medeiros Neto on May 15, 2009 at 04:28 AM PDT #

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

Posted by jagadeesan on May 04, 2010 at 08:22 PM PDT #

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