Training „git“
07.10.2010 - 08.10.2010, Essen
Training „Eclipse RCP“
28.03.2011 - 01.04.2011, Dortmund
24.07.2009

Adding a command with a key binding for an editor

Declare a command which describes the action that takes place when the command is triggered using the extension point org.eclipse.ui.commands. Check if there is a default command in Eclipse UI (like delete, refresh, etc.) first. For example:

<extension point="org.eclipse.ui.commands">
   <command
         id="de.ralfebert.somecommand"
         name="Some command">
   </command>
</extension>

Add the command to your menu bar / tool bar using the extension point org.eclipse.ui.menus:

<extension point="org.eclipse.ui.menus">
  <menuContribution locationURI="menu:org.eclipse.ui.main.menu">
     <menu id="edit" label="Edit">
        <command commandId="de.ralfebert.somecommand" style="push"/>
     </menu>
  </menuContribution>
</extension>

Declare a handler for your command which is only active when the editor is active:

<extension point="org.eclipse.ui.handlers">
  <handler
        class="de.ralfebert.SomeCommandHandler"
        commandId="de.ralfebert.somecommand">
     <activeWhen>
        <with variable="activePartId">
           <equals value="de.ralfebert.SomeEditorPart"/>
     </activeWhen>
  </handler>
</extension>

Implement the handler:

public class SomeCommandHandler extends AbstractHandler {

    public Object execute(ExecutionEvent event) throws ExecutionException {
        SomeEditorPart editor = (SomeEditorPart) HandlerUtil.getActiveEditor(event);
        // ...
        return null;
    }

}

Declare a key binding for the command using the extension point org.eclipse.ui.bindings:

<extension point="org.eclipse.ui.bindings">
  <key
        commandId="de.ralfebert.somecommand"
        schemeId="org.eclipse.ui.defaultAcceleratorConfiguration"
        sequence="F6">
  </key>
</extension>
I'm looking forward to your comments:

Schulungen

Eclipse RCP

Ralf Ebert | Blog | Eclipse RCP | Adding a command with a key binding for an editor