Time for action – creating commands and handlers

Since the Action class is deprecated, the supported mechanism is to create a command, a handler, and a menu to display the command in the menu bar.

  1. Open the plug-in manifest for the project, or double-click on the plugin.xml file.
  2. Edit the source on the plugin.xml tab, and add a definition of a Hello command as follows:
    <extension point="org.eclipse.ui.commands">
      <command name="Hello"
        description="Says Hello World" 
        id="com.packtpub.e4.clock.ui.command.hello"/>
    </extension>
  3. This creates a command, which is just an identifier and a name. To specify what it does, it must be connected to a handler, which is done by adding the following extension:
    <extension point="org.eclipse.ui.handlers">
      <handler class=
      "com.packtpub.e4.clock.ui.handlers.HelloHandler"
       commandId="com.packtpub.e4.clock.ui.command.hello"/>
    </extension>
  4. The handler joins the processing of the command to a class that implements IHandler, typically AbstractHandler. Create a class HelloHandler in a new com.packtpub.e4.clock.ui.handlers package, which implements AbstractHandler (from the org.eclipse.core.commands package).
    public class HelloHandler extends AbstractHandler {
      public Object execute(ExecutionEvent event) {
        MessageDialog.openInformation(null, "Hello", "World");
        return null;
      }
    }
  5. The command's ID com.packtpub.e4.clock.ui.command.hello is used to refer to it from menus or other locations. To place the contribution in an existing menu structure, it needs to be specified by its locationURI, which is a URL that begins with menu: such as menu:window?after=additions or menu:file?after=additions. To place it in the Help menu, add this to the plugin.xml file.
    <extension point="org.eclipse.ui.menus">
      <menuContribution allPopups="false"
       locationURI="menu:help?after=additions">
        <command commandId="com.packtpub.e4.clock.ui.command.hello"
         label="Hello"
         style="push">
        </command>
      </menuContribution>
    </extension>
  6. Run the Eclipse instance, and there will be a Hello menu item under the Help menu. When selected, it will pop up the Hello World message. If the Hello menu is disabled, verify that the handler extension point is defined, which connects the command to the handler class.

What just happened?

The main issue with the actions framework was that it tightly coupled the state of the command with the user interface. Although an action could be used uniformly between different menu locations, the Action superclass still lives in the JFace package, which has dependencies on both SWT and other UI components. As a result, Action cannot be used in a headless environment.

Eclipse 3.x introduced the concept of commands and handlers, as a means of separating their interface from their implementation. This allows a generic command (such as Copy) to be overridden by specific views. Unlike the traditional command design pattern, which provides implementation as subclasses, the command in Eclipse 3.x uses a final class and then a retargetable IHandler to perform the actual execution.

Note

What just happened? E4: In Eclipse 4.x, the concepts of commands and handlers are used extensively to provide the components of the user interface. The key difference is in their definition; for Eclipse 3.x, this typically occurs in the plugin.xml file, whereas in E4 it is part of the application model.

In the example, a specific handler was defined for the command, which is valid in all contexts. The handler's class is the implementation; the command ID is the reference.

The org.eclipse.ui.menus extension point allows menuContributions to be added anywhere in the user interface. To address where the menu can be contributed to, the locationURI object defines where the menu item can be created. The syntax for the URI is as follows:

  • menu: Menus begin with the menu: protocol (can also be toolbar: or popup:)
  • identifier: This can be a known short name (such as file, window, and help), the global menu (org.eclipse.ui.main.menu), the global toolbar (org.eclipse.ui.main.toolbar), a view identifier (org.eclipse.ui.views.ContentOutline), or an ID explicitly defined in a pop-up menu's registerContextMenu() call.
  • ?after(or before)=key: This is the placement instruction to put this after or before other items; typically additions is used as an extensible location for others to contribute to.

The locationURI allows plug-ins to contribute to other menus, regardless of where they are ultimately located.

Note, that if the handler implements the IHandler interface directly instead of subclassing AbstractHandler, the isEnabled() method will need to be overridden as otherwise the command won't be enabled, and the menu won't have any effect.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset