One of the first things that you will want to do with your RCP plug-in is to provide a menu, establishing its existence with the Eclipse application that it is built into. To do this, as with any additions to our plug-in, we start in the Extensions tab of the plug-in manifest editor.
Up to Eclipse 3.3 Actions was the only API available to deal with menus, but since then the commands API has become available, which we will focus on here.
To add a menu in the command API you will need to follow similar steps to these:
Declare a Command
To do this we use the org.eclipse.ui.commands extension point. Simply click on the Add... button in the Extensions tab and chose the relevant extension point.
First, you will need to associate this command with a category. Categories are useful for managing large numbers of commands. From the org.eclipse.ui.commands node, select New > Category. The required fields are a unique ID and a readable name.
After this right click on the node and choose New>Command. The important attributes for a command are listed below.
Attribute Name |
Required |
Use |
id |
Yes |
A unique id for this command |
Name |
Yes |
A readable name for the command |
Description |
No |
A short description for display in the UI |
CategoryID |
No |
The id of the category for this command (that you described in the previous step). |
DefaultHandler |
No |
A default handler for this command. Usually you will create your own handler. |
Declare a Menu Contribution for the Command
To create a menu, you will first need to add the org.eclipse. ui.menus extension point. From the created node in the UI, select New>menuContribution. The required attribute for the menu contribution is it's locationURI, which specifies where the menu should be placed in the UI. This URI takes the format of [scheme]:[id]?[argument-list]
An example of the more useful locationURI's in the Eclipse platform follow:
Attribute Name |
Required |
menu:org.eclipse.ui.main. menu?after=window |
Insert this contribution on the main menu bar after the Window menu |
menu:file?after=additions |
Inserts contribution in the File menu after the additions group |
toolbar:org.eclipse.ui.main. toolbar?after=additions |
Insert this contribution on the main toolbar |
popup:org.eclipse.ui.popup. any?after=additions |
Adds this contribution to any popup menu in the application |
Once the location of your contribution is chosen, click on New>command on this contribution to define the menu. The following attributes exist for each command:
Attribute |
Required |
Use |
commandId |
Yes |
The id of the Command object to bind to this element, typically already defined, as in our earlier step. Click Browse... to find this |
Label |
No |
The readable label to be displayed for this menu item in the user interface |
id |
No |
A unique identifier fo this item. Further menu contributions can be placed under this menu item using this id in the locationURI |
mnemonic |
No |
The Character within the label to be assigned as the mnemonic |
icon |
No |
Relative path to the icon that will be displayed to the left of the label |
tooltip |
No |
The tooltip to display for this menu item |
Defining a toolbar item is a similar process. Based on a menuContribution with the correct locationURI, select New>Toolbar providing a unique id. Create a new command under the toolbar similar to the menu item approach.
Create a Handler for the Command
The final extension point required for the menu is org.eclipse. ui.handlers. A handler has two vital attributes. The first is the commandId which should be the same as the command id specified in the beginning. As you can see, this is the glue between all three parts of the menu definition.
You will also need to create a concrete class for this handler, which should implement the org.eclipse.core.commands. IHandler interface.
Clicking on the class hyperlink on the manifest editor will pop up a New Class Wizard with the fields autofilled for this.
Finally, you will need to define when this command is enabled or active. This can be done programmatically in the isEnabled() and isHandled() methods. While this is easiest, the recommended approach is to use the activeWhen and enabledWhen expressions in the plug-ins manifest editor which avoids unnecessary plug-in loading.
Once you have added in an extension point plugin. xml will become available. All extension points can be added through the manifest editor, or in XML format through this file.
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ parent.urlSource.name }}