| Free tutorials for Java, Eclipse and Web programming |
A command is a declarative description of a component and is independent from the implementation details. A command can be categorized and a hot key (key binding) can be assigned to the command.
Commands can be used in menus, toolbars and / or context menus.
The following will focus on a simple definition and usage of commands. For details on the usage of commands please see Eclipse Commands - Tutorial
Our first example will be a command which will exit the application. Create a new RCP project "de.vogella.rcp.commands.first" and use the "Hello RCP" Template.
Click on the plugin.xml and select the Extensions tab.

Press the add button and search for the extension org.eclipse.ui.commands. Select it and press finish.

Create a new command by right-clicking 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 this class.
Choose org.eclipse.core.commands.AbstractHandler as Superclass.

Implement the following coding
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;
}
}
You have correctly implemented the command. This command can now be used in various places in your application.
The command which we just defined should now be used in a new menu.
Add the extension point "org.eclipse.ui.menus" to your application. Select therefore again plugin.xml and the tab "Extensions". Press Add, select the extension point "org.eclipse.ui.menus" and press Finish.

Right click on the extension point and select new -> menuContribution.

Create a new menu contribution with the location URI "menu:org.eclipse.ui.main.menu".

Right click your menucontribution and select New -> Menu. Add a menu with the label "File" and the id "fileMenu".


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

Your work 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>
If you run the example you should see menu with the file. Selecting Exit should end your application.
