Free tutorials for Java, Eclipse and Web programming



Follow me on twitter

Eclipse Commands Tutorial

Lars Vogel

Version 1.7

28.06.2010

Revision History
Revision 0.130.03.2009Lars Vogel
Created
Revision 0.2 - 0.411.04.2009 - 16.04.2009Lars Vogel
bugfixes and improvements
Revision 0.530.05.2009Lars Vogel
Update to Eclipse 3.5 (Galileo)
Revision 0.6 - 1.508.07.2009 - 02.12.2009Lars Vogel
bugfixes and improvements
Revision 1.7 28.06.2010Lars Vogel
Update to Eclipse 3.6 (Helios)

Eclipse Commands

This article describes the usage of Eclipse commands. It describes how to create commands, handlers, add commands into the menu, pop-ups, views and editors and the usage of expressions to restrict UI contributions. The usage of keybindings is also explained.

This article is based on Eclipse Helios (3.6).


Table of Contents

1. Eclipse Commands - Overview
1.1. Overview
1.2. Location URI - User interface
1.3. Command Handler
2. Commands and menus
2.1. Assumptions
2.2. Defining commands
2.3. Using commands in menus
3. Commands and toolbars
3.1. Overview
3.2. Application toolbar (coolbar)
3.3. Contribution to the View Toolbar
3.4. Drop down list
4. Commands and context menus
5. Enabled when (visible when)
6. Keybindings for Commands
6.1. Overview
6.2. Keybinding
6.3. Defining your own schema
7. Eclipse standard commands
7.1. Overview
7.2. Example
8. Calling commands from a button
9. Providing your own expression
10. Using parameters in commands
11. Defining commands at runtime
12. Thank you
13. Questions and Discussion
14. Links and Literature
14.1. Source Code
14.2. Eclipse Commands Resources
14.3. vogella Resources

1. Eclipse Commands - Overview

1.1. Overview

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.

1.2. Location URI - User interface

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 toDescriptionUri
Application menuDisplays the command in the menu of the application"menu:org.eclipse.ui.main.menu"
Application toolbardisplays 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-upCommand is displayed in a content menu, e.g. right mouse click on an objectn.a.

1.3. Command Handler

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.

Tip

The class HandlerUtil provides access methods to the important Eclipse services.

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.