Version 3.1
Copyright © 2007, 2008, 2009, 2010, 2011, 2012 Lars Vogel
22.02.2012
| Revision History | |||
|---|---|---|---|
| Revision 0.1 | 30.03.2009 | Lars Vogel |
Created |
| Revision 0.2 - 3.1 | 11.04.2009 - 22.02.2012 | Lars Vogel |
bugfixes and improvements |
Table of Contents
This tutorial describes the Eclipse 3.X API which is still used for Eclipse IDE plug-in development.
For Eclipse RCP you should use the new Eclipse 4 API which is described in the Eclipse RCP tutorial .
Eclipse IDE extensions use the commands framework to contribute actions to the user interface. This concept is based on extension points.
Eclipse RCP application in version 3.x are also using this concept. Eclipse RCP applications based on version 4.x do not use the extension point approach but the concepts are still the same.
This description explains the usage of commands in Eclipse IDE extensions. To define and use a command you have to define three extensions. The following extension points are relevant to define a menu or toolbar contribution for an Eclipse plug-in.
Table 1.
| Extension Point | Description |
|---|---|
| org.eclipse.ui.command | Declarative description of the component |
| org.eclipse.ui.handler | Defines the behavior, e.g. the Java class which should be called |
| org.eclipse.ui.menu | Where and how should the command be included in the user interface, e.g. menu, popup menu, toolbar, etc. |
This tutorial assumes what you have basic understanding of Eclipse plug-in development. See Eclipse Plug-in Tutorial for an introduction into Eclipse plug-in development.
A command in Eclipse is a declarative description of a component and is independent from the implementation details.
A command can be categorized, assigned to the user interface and a key binding can be defined for the command. The behavior of a command is defined via a handler.
The
org.eclipse.ui.menu
extension point is used to define where the command is visible in the
application, e.g. you could define that the command is visible in the
application menu or in a
view
menu.

The menuContribution contribution has an ID value which allow to identify the contribution.
menuContribution
has an
locationURI
attribute which allows to
define where in the user interface a
command
is displayed. This different attribute values are summarized in the
following table.
Table 2. Location URIs
| Contribution to | Description | Uri |
|---|---|---|
| Application menu | Displays the command in the menu of the application | menu:org.eclipse.ui.main.menu |
| Application toolbar | displays the command in the toolbar of the application | toolbar:org.eclipse.ui.main.toolbar |
| View toolbar | displays the command in the toolbar of the view | toolbar:viewId - For example to display a menu to view with the Id "View1" use "toolbar:View1". |
| Context menu / pop-up | Command is displayed in a content menu, e.g. right mouse click on an object | popup:ID |
Frequently you want to position a new command behind or before another command. For example the Save All menu entry should be positioned after the Save menu entry.
You can define the relative position of a command in this
locationURI
by using the
pattern
?before=ID
or
?after=ID.
The ID can be an existing separator name, menu ID, or item ID. The command will then be placed before or after the element with the corresponding ID.
For example if you want to add
a command to an
existing menu with
the
fileMenu
ID behind the menu
entry with the
oneEntry
ID use the
menu:fileMenu?after=oneEntry
locationURI .
The
org.eclipse.ui.handler
extension point connects the command to a certain class which is
called once the command is executed.
The behavior of a command is
defined via
handlers. The handler is
the
class which is executed once the command is
called and must
implement
the
IHandler
interface from the
org.eclipse.core.commands
package.
In most cases you extend the
org.eclipse.core.commands.AbstractHandler
class which
provides a
default
implementation for the
IHandler
interface.
The
execute()
method is called if the handler is executed.
In the
execute()
method you get access to frequently used values
and services via the
HandlerUtil
class.
Handler can be defined with conditions (activeWhen) under which define the conditions under which the handlers are valid for a command. You can define several handlers for a command but only handler can be valid for a command at a certain point. Otherwise the Eclipse runtime will not enabled the command.
This tutorial assumes what you have basic understanding of development for the Eclipse platform. Please see Eclipse RCP Tutorial or Eclipse Plug-in Tutorial if you need any basic information.
We will create a command which will exit the application. Create a new RCP project "de.vogella.rcp.commands.first" using the "Hello RCP" template. Click on the plugin.xml and select the Extensions tab. Press the button.

Search for the extension "org.eclipse.ui.commands". Select it and press finish.

Create a new command by right-click on your extension point and by selecting New -> command.

Set the ID to "de.vogella.rcp.commands.first.commands.Exit" and the name to "Exit". Enter the class "de.vogella.rcp.commands.first.commands.ExitHandler" as defaultHandler.

Press the hyperlink "defaultHandler" to create the class which should extend "org.eclipse.core.commands.AbstractHandler".
package de.vogella.rcp.commands.first.commands; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.ui.handlers.HandlerUtil; public class ExitHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { HandlerUtil.getActiveWorkbenchWindow(event).close(); return null; } }
The command which we defined should be used in a
menu. Add a extension
to the
org.eclipse.ui.menus
extension point.
Right-click on the
extension point and select
→ .

Create a new menu contribution with the "menu:org.eclipse.ui.main.menu" locationURI. Make sure this URL is correct spelled, otherwise your menu will not be shown.

Right-click your menucontribution and select → . Add a menu with the "File" label and the "fileMenu" ID.


Select your menu, right-click on it, select New-> Command. Maintain your commandID. Set the label to "Exit" and the tooltip to "Exits the application".

This should result in a plugin.xml file which looks like
the
following.
<?xml version="1.0" encoding="UTF-8"?> <?eclipse version="3.4"?> <plugin> <extension id="application" point="org.eclipse.core.runtime.applications"> <application> <run class="de.vogella.rcp.commands.first.Application"> </run> </application> </extension> <extension point="org.eclipse.ui.perspectives"> <perspective name="RCP Perspective" class="de.vogella.rcp.commands.first.Perspective" id="de.vogella.rcp.commands.first.perspective"> </perspective> </extension> <extension point="org.eclipse.ui.commands"> <command defaultHandler="de.vogella.rcp.commands.first.commands.ExitHandler" id="de.vogella.rcp.commands.first.commands.Exit" name="Exit"> </command> </extension> <extension point="org.eclipse.ui.menus"> <menuContribution locationURI="menu:org.eclipse.ui.main.menu"> <menu id="fileMenu" label="File"> <command commandId="de.vogella.rcp.commands.first.commands.Exit" label="Exit" style="push" tooltip="Exit the application"> </command> </menu> </menuContribution> </extension> </plugin>
Run the example. You should see menu with the file and if you select the "Exit" entry you application should exit.

Create a new project "de.vogella.rcp.intro.commands.toolbar". Use the "RCP application with a view" as a template. Create a command "de.vogella.rcp.intro.commands.toolbar.Hello" with the default handler "de.vogella.rcp.intro.commands.toolbar.handler.Hello". This handler will open a JFace Dialog .
package de.vogella.rcp.intro.commands.toolbar.handler; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.ui.handlers.HandlerUtil; public class Hello extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { MessageDialog.openInformation(HandlerUtil.getActiveWorkbenchWindow(event).getShell(), "Info", "Info for you"); return null; } }
Add a menucontribution to the "org.eclipse.ui.menus extension" point. Set the location URI to "toolbar:org.eclipse.ui.main.toolbar". Add a toolbar to your menu contribution.

Add the command "de.vogella.rcp.intro.commands.toolbar.Hello" to the toolbar. Assign a label and an icon to it.

Activate the application toolbar via ApplicationWorkbenchWindowAdvisor.java and set the configurer.setShowCoolBar(true); (
package de.vogella.rcp.intro.commands.toolbar; import org.eclipse.swt.graphics.Point; import org.eclipse.ui.application.ActionBarAdvisor; import org.eclipse.ui.application.IActionBarConfigurer; import org.eclipse.ui.application.IWorkbenchWindowConfigurer; import org.eclipse.ui.application.WorkbenchWindowAdvisor; public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor { public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) { super(configurer); } public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) { return new ApplicationActionBarAdvisor(configurer); } public void preWindowOpen() { IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); configurer.setInitialSize(new Point(400, 300)); configurer.setShowStatusLine(false); configurer.setShowCoolBar(true); configurer.setTitle("RCP Application"); } }
The result should look like the following. If you select the element in the toolbar an information dialog should open.

You can also add a command directly to a view toolbar. For this we will extend the previous example. Change the class "Perspective" to the following (a stand-alone view does not have a own toolbar).
package de.vogella.rcp.intro.commands.toolbar; import org.eclipse.ui.IPageLayout; import org.eclipse.ui.IPerspectiveFactory; public class Perspective implements IPerspectiveFactory { public void createInitialLayout(IPageLayout layout) { String editorArea = layout.getEditorArea(); layout.setEditorAreaVisible(false); layout.setFixed(true); layout.addView(View.ID, IPageLayout.LEFT, 1.0f, editorArea); } }
Create a new menu contribution to the extension point "org.eclipse.ui.menus" with the locationURI: "toolbar:de.vogella.rcp.intro.commands.toolbar.view". ""toolbar:"" tells the system to add it to the toolbar while the second argument is the id of your view.
Create then a new command for this menucontribution and set the command id to "de.vogella.rcp.intro.commands.toolbar.Hello". Assign the label "Say Hello" to it.

Run the application to see your new view contribution.

The following adds a dropdown list to the application coolbar.
This creation is a bit strange. You create a helper drop-down command to which later the other (real) commands will be assigned to.
Therefore create a command with the id "referenceToMenuId". Maintain also the default handler. For example you could re-use "de.vogella.rcp.intro.commands.toolbar.handler.Hello".

Add a new menucontribution to the "org.eclipse.ui.menus" extension point. Set the location URI to "toolbar:org.eclipse.ui.main.toolbar". Add a toolbar to this extension and a new command to this new toolbar. As the id use "referenceToMenuId" give it a label and an icon and change the style to "pulldown".

Create a new menucontribution and set the locationURI to: "menu:referenceToMenuId"
Add your exiting command "de.vogella.rcp.intro.commands.toolbar.Hello" two times to this menu. Use different labels.

Run your application, it should now have a drop-down list in the application toolbar.

Now lets add a ContextMenu to a table. Create a new project "de.vogella.rcp.intro.commands.popup" based on the "RCP application with a view" example.
Create a new command with the ID "de.vogella.rcp.intro.commands.popup.showSelected" and the name "Show".
In this example we will not use the default handler. Therefore add the extension point "org.eclipse.ui.handlers" to your plugin.xml and add a handler. The first parameter is the commandId and the second the class for the handler. We will use class "de.vogella.rcp.intro.commands.popup.handler.ShowSelected".

Implement now the coding for your handler. I just print the selected elements to the console.
package de.vogella.rcp.intro.commands.popup.handler; import java.util.Iterator; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.ui.handlers.HandlerUtil; public class ShowSelected extends AbstractHandler { @SuppressWarnings("unchecked") @Override public Object execute(ExecutionEvent event) throws ExecutionException { ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event) .getActivePage().getSelection(); if (selection != null & selection instanceof IStructuredSelection) { IStructuredSelection strucSelection = (IStructuredSelection) selection; for (Iterator<Object> iterator = strucSelection.iterator(); iterator .hasNext();) { Object element = iterator.next(); System.out.println(element.toString()); } } return null; } }
Add a new menuContribution with the locationURI "popup:de.vogella.rcp.intro.commands.popup.view", where "de.vogella.rcp.intro.commands.popup.view" is the ID of your view which has been automatically created for you.
Right-click your new menuContribution and select New -> Command. Assign your command to the field "commandId". Label it "One Item selected".
Now you have add a MenuManager to your view. Select View.java and in the method createPartControl add the following:
public void createPartControl(Composite parent) { viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); viewer.setContentProvider(new ViewContentProvider()); viewer.setLabelProvider(new ViewLabelProvider()); viewer.setInput(getViewSite()); // This is new code // First we create a menu Manager MenuManager menuManager = new MenuManager(); Menu menu = menuManager.createContextMenu(viewer.getTable()); // Set the MenuManager viewer.getTable().setMenu(menu); getSite().registerContextMenu(menuManager, viewer); // Make the selection available getSite().setSelectionProvider(viewer); }
Run your application. If you perform a right-clic, your menu should become visible. If you select it menu then the names of the selected items should be written to the console.
For more advanced usages of Eclipse Commands please see Eclipse Commands Advanced.
Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.
http://wiki.eclipse.org/Platform_Command_Framework Command Framework wiki
http://wiki.eclipse.org/Command_Core_Expressions Commands Core Expressions
vogella Training Android and Eclipse Training from the vogella team
Android Tutorial Introduction to Android Programming
GWT Tutorial Program in Java and compile to JavaScript and HTML
Eclipse RCP Tutorial Create native applications in Java
JUnit Tutorial Test your application
Git Tutorial Put everything you have under distributed version control system