Adding Eclipse IDE Menu Options to Your RCP Applications
Join the DZone community and get the full member experience.Join For Free
one of the many things that eclipse rcp provides is a set of standard menu options that are used by the eclipse ide itself. these include standard menu options such as exit , save , cut/copy/paste , but also workbench specific options such as reset perspective and show views .
note: the post below is actually incorrect, which is a good
thing in my opinion. it is indeed possible to use the command framework
to add the standard eclipse ide menu options. rather than take down the
post, which i think is unethical, please read the post and then the
note at the end for clarification.
in short, almost all of the menu options you see in the eclipse ide are available for use in your rcp applications. and i tell most teams starting out with eclipse rcp to go through the eclipse ide menu and make a list of those options that make sense for their application.
so how can we add these menu options to our applications?
traditionally, eclipse ide menu options have been added as actions in the actionbaradvisor. the eclipse actions can be referenced as constants in the actionfactory class, and adding a menu option looks like this:
iaction saveaction = actionfactory.save.create(window);
during the last few years, most eclipse rcp developers have transitioned from actions to commands . commands, and their associated handlers and menus, provide a clean separation between ui and non-ui concerns. as part of the transition to commands, eclipse rcp now provides us with the eclipse ide menu options as a set of extensions defined in the org.eclipse.ui bundle.
the commands point to handlers that implement the behavior associated with menu options. we could, if we like, create org.eclipse.ui.menus extensions that refer directly to these commands. this would allow us to define our entire menu using declarative syntax.
which approach should we use?
so the question is, should we choose the command-based approach? the answer (for eclipse ide menu options only) is no. using the org.eclipse.ui.menus extension point to add eclipse ide menu options is problematic in that we lose access to look and feel attributes. specifically, the actionfactory constants provide us with the following menu attributes:
- image (enabled and disabled)
- help context
not only that, but the actions provide us with these values as externalized strings with support for internationalization. so while actions are really the old way of doing things, the actionfactory/actionbaradvisor combo is still the preferred way of utilizing eclipse ide menu options.
when should i use commands?
well you should definitely be using the command-based approach for your own menu options. also the eclipse ide commands are still useful if you want to link your own menu options to them. you may want to reference the standard eclipse ide options in view menus, cheat sheets, dialogs, etc.
finally, you should know that the actions produced by the actionfactory are actually bridged to commands and handlers behind the scenes. this means that even though you are using actions to define the menu options, you can still provide your own handlers for them.
clarification: what i missed when writing this post was that the eclipse ide commands contain localized text which will show up in your menu if you leave the menu label blank. also, images will appear in the menu, and these are contributed via the org.eclipse.ui.commandimages extension point.
i’m still not clear on how tooltips or help contexts are handled, but you can get almost all the way there with the command framework. so in my opinion, the best practice for new eclipse rcp applications would be to use the command framework for all menu options and discontinue usage of the actionbaradvisor.
thanks to lars vogel for the correction!
clarification #2: having looked into this further, it appears that some of the standard eclipse ide menu options require that the actionfactory action be created and registered before the commands will work. you don’t actually need to use the actions to create the menu options in the actionbaradvisor, though. for more information on this, see this bugzilla entry .
Opinions expressed by DZone contributors are their own.