| Free tutorials for Java, Eclipse and Web programming |
Version 2.0
Copyright © 2007- 2011 Lars Vogel
28.03.2011
| Revision History | ||
|---|---|---|
| Revision 0.1 | 30.03.2009 | Lars Vogel |
| Created | ||
| Revision 0.2 - 2.0 | 11.04.2009 - 28.03.2011 | Lars Vogel |
| bugfixes and improvements | ||
Table of Contents
A command in Eclipse is a declarative description of a component and is independent from the implementation details. Commands are defined via the extension point "org.eclipse.ui.commands". 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 can be defined via a handler.
To use a command in your User Interface you need:
Command - Declarative description of the component
Handler - Defines the behavior
UI Assignment - Where and how should the command be included in the UI
The following example used are based on Eclipse RCP but the concept also applies to general Eclipse plugin development .
Commands can be used in menus, toolbars and / or context menus. Where and how these commands are displayed is defined via a location URI.
Table 1. Location URI's
| 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 | n.a. |
You can define the relative position of a command in this location URI 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 id "fileMenu" behind the menu entry with the id "oneEntry" use the locationURI of "menu:fileMenu?after=oneEntry".
The behavior of a command is defined via handlers. The handler is the class which will be executed once the command is called and must implement the interface "org.eclipse.core.commands.IHandler". "org.eclipse.core.commands.AbstractHandler" provides a default implementation for the IHandler interface.
IHandler defines the following important methods which can be implemented:
isEnabled: Called during instantiation, defines if this handler is enabled
isHandled: Defines if the handler can be called or not
execute: Coding which performs the action
fireHandlerChanged: needs to be called if isEnabled is changed
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 cannot decide which one should be used and the command will not be enabled.