| Free tutorials for Java, Eclipse and Web programming |
Version 1.5
Copyright © 2007- 2009 Lars Vogel
02.12.2009
| Revision History | ||
|---|---|---|
| Revision 0.1 | 30.03.2009 | Lars Vogel |
| Separated from http://www.vogella.de/articles/RichClientPlatform/article.html | ||
| Revision 0.2 | 11.04.2009 | Lars Vogel |
| Re-written introduction | ||
| Revision 0.3 | 15.04.2009 | Lars Vogel |
| Simplified toolbar examples | ||
| Revision 0.4 | 16.04.2009 | Lars Vogel |
| Keybinding, overriding default Eclipse key binding scheme added | ||
| Revision 0.5 | 30.05.2009 | Lars Vogel |
| Update to Eclipse 3.5 (Galileo), included command start example from RCP Tutorial | ||
| Revision 0.6 | 08.07.2009 | Lars Vogel |
| Reworked core expressions | ||
| Revision 0.7 | 09.07.2009 | Lars Vogel |
| Reworked standard commands contributions | ||
| Revision 0.8 | 12.07.2009 | Lars Vogel |
| ISourceProvider example added | ||
| Revision 0.9 | 25.07.2009 | Lars Vogel |
| Added plugin.xml for command example | ||
| Revision 1.0 | 26.07.2009 | Lars Vogel |
| Added parameters for commands | ||
| Revision 1.1 | 30.07.2009 | Lars Vogel |
| Improved keybindings section | ||
| Revision 1.2 | 15.09.2009 | Lars Vogel |
| Added Link to core expressions | ||
| Revision 1.3 | 18.10.2009 | Lars Vogel |
| Removed redundant screenshot | ||
| Revision 1.4 | 30.11.2009 | Lars Vogel |
| Fixed typo | ||
| Revision 1.5 | 02.12.2009 | Lars Vogel |
| Added dynamic command contributions, thanks to Robert Einsle for the tips | ||
Eclipse Commands
This article describes the usage of Eclipse commands.
The article describes how to create commands, handlers, add commands into the menu, pop-ups, views and editors. It explains how to use the core expressions and your own expressions to restrict your UI contributions. The usage of keybindings is also explained.
This article is based on Eclipse Galileo (3.5).
Table of Contents
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 can be defined via a handler.
Therefore to define and use a command 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
Commands are defined via the extension point org.eclipse.ui.commands.
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.
| 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:Views1". |
| Content menu / pop-up | Command is displayed in a content menu, e.g. right mouse click on an object | n.a. |
The behavior of a command is defined via handlers. You can define several handlers for a command but only only handler can be valid for a command at a certain point otherwise the command is not active. The handler is the class which will be executed once the command is called. A command handler must implement the interface org.eclipse.core.commands.IHandler.
IHandler (which is implemented by org.eclipse.core.commands.AbstractHandler) defines the following important methods which can be implemented:
isEnabled: Called during instantiation, defines if this action is enabled
isHandled: Defines if the handler can be called or not
execute: Coding which perform the action
fireHandlerChanged: needs to be called if isEnabled is changed.
Handler can be defined with conditions (activeWhen) under which they are valid handlers for the command. If more then one handler is valid for a given selection the Eclipse runtime cannot decided which one is used and the command will not be enabled.