Commands

See the org.eclipse.ui.commands section for creating command definitions, hooking them up with keybindings, and placing them in menus.

Associating a legacy action with a command

When still using the old legacy action extension points, actions can be associated with a command in code or in the plugin.xml for action sets.  Your choice depends on where the action is defined.

Instead of using an IAction you should prefer activating an AbstractHandler.

    // IWorkbench, IWorkbenchWindow, and IWorkbenchPartSite are all IServiceLocators
    IHandlerService handlerService = (IHandlerService) locator.getService(IHandlerService.class);
    handlerService.activateHandler(org.eclipse.ui.IWorkbenchCommandConstants.FILE_SAVE, new MySaveHandler());

But when working with actions, actions that are instantiated in code can also be associated with an action definition using IAction protocol.  This is typically done when the action is created.  The SaveAction uses this technique when it initializes itself.

public SaveAction(IWorkbenchWindow window) {
	...
	setText...
	setToolTipText...
	setImageDescriptor...
	setActionDefinitionId("org.eclipse.ui.file.save"); 
}

(Note:  The method name setActionDefinitionID could more appropriately be named setCommandID.  The method name reflects the original implementation of key bindings and uses outdated terminology.)

By invoking setActionDefinitionID, the implementation action (SaveAction) is associated with the command id that was used in the command definition markup. It is good practice to define constants for your action definitions so that they are easily referenced in code.

If you define an action in an action set, then you typically do not need to instantiate an action yourself.  The workbench will do it for you when the user invokes your action from a menu or the keyboard.  In this case, you can associate your action with a command ID in your  XML markup.  The following shows a hypothetical markup for an action set:

<extension point = "org.eclipse.ui.actionSets">
	   <actionSet id="com.example.actions.actionSet"
		   label="Example Actions"
		   visible="true">
		   <action id="com.example.actions.action1"
			   menubarPath="additions"
			   label="Example Save Action"
			   class="org.example.actions.ExampleActionDelegate"
			   definitionId="org.eclipse.ui.file.save">
		   </action>
		   ...
	   </actionSet>
   </extension>

The definitionId attribute is used to declare a command ID for the action.

Using either technique, associating your action with a command ID causes any key bindings that get defined for the command org.eclipse.ui.file.save to invoke your action when appropriate. 

Now let's look at how these key bindings get defined.