RSS

Search Engine

Monday, June 14, 2010

Commands

6.1. Overview

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

6.2. Defining commands

Tip

Commands are superior to Actions. In my opinion Actions should be marked as deprecated as having both is confusing for new Eclipse platform developers. Please share your opinion in Bug for deprecating Actions

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.

Tip

Lots of people report that if they try this they can only select a "Generic" entry. The common source of this problem seems to be that you did not download the package "Eclipse for RCP/Plug-in Developers". Please see Eclipse Installation .

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.

6.3. Using commands in menus

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".

Tip

Make sure the URL must is correct, otherwise the menu will not get displayed.

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.

   




id="application"
point="org.eclipse.core.runtime.applications">

class="de.vogella.rcp.commands.first.Application">



point="org.eclipse.ui.perspectives">
name="RCP Perspective"
class="de.vogella.rcp.commands.first.Perspective"
id="de.vogella.rcp.commands.first.perspective">


point="org.eclipse.ui.commands">
defaultHandler="de.vogella.rcp.commands.first.commands.ExitHandler"
id="de.vogella.rcp.commands.first.commands.Exit"
name="Exit">


point="org.eclipse.ui.menus">
locationURI="menu:org.eclipse.ui.main.menu">
id="fileMenu"
label="File">
commandId="de.vogella.rcp.commands.first.commands.Exit"
label="Exit"
style="push"
tooltip="Exit the application">







If you run the example you should see menu with the file. Selecting Exit should end your application.

0 comments:

Post a Comment