Time for action – upgrading the actions

The JFace Action class is used in pop-up menus using the Eclipse 2.x model, but in Eclipse 3.x and Eclipse 4.x, the separation between handlers (code that does the work) and commands (logical operations such as copy that may be processed by different handlers) allows for a more flexible system, as described in Chapter 4, Interacting with the User. The first step is to replace the actions with handlers.

  1. Open the SampleView class and go to the makeActions method, which creates anonymous inner subclasses of Action with an associated run method. To convert these, they will need to be static inner classes, so highlight the first new Action expression and navigate to the Refactor | Convert Anonymous Class to Nested… menu.
  2. In the Convert Anonymous Class to Nested dialog that appears, ensure that public and static are selected, and give it the name HandlerOne:
    Time for action – upgrading the actions
  3. Do the same steps for the other two actions, calling them HandlerTwo and DoubleClick.
  4. To fix the compile-time errors introduced by making the inner classes static, change the showMessage method to public and static and replace the first parameter with null:
    public static void showMessage(String message) {
      MessageDialog.openInformation(
      // viewer.getControl().getShell(),
      null,
      "Sample View",
      message);
    }
  5. The DoubleClick handler will also need to comment out references to the non-static viewer; the selection can be passed in as an argument instead, as follows:
    public static final class DoubleClick {
      public void run(
       @Named("selection") IStructuredSelection selection) {
        SampleView.showMessage("Double-click detected on "
         + selection.getFirstElement());
      }
    }
  6. The hookDoubleClickAction method will need to pass the current selection to the DoubleClick class, which can be accessed from the DoubleClickEvent parameter. Modify it as follows:
    private void hookDoubleClickAction() {
      viewer.addDoubleClickListener(new IDoubleClickListener() {
        public void doubleClick(DoubleClickEvent event) {
          new DoubleClick().run(
           (IStructuredSelection) event.getSelection());
        }
      });
    }

    Note

    At this point, the class should be free of compile-time errors; fix any remaining issues before moving forward.

  7. Although the classes could be kept as an inner class, Eclipse 4.x bugs prevent inner classes from being used as handlers. To resolve this, extract the HandlerOne, HandlerTwo, and DoubleClick classes to their own files by using the Refactor | Move Type to New File… menu. If the showMessage method has not been updated, the refactoring will suggest changing it to public.
  8. The next step is to remove the Action superclass and annotate the run method with the @Execute annotation from the org.eclipse.e4.core.di.annotations bundle. To do this, open the META-INF/MANIFEST.MF file, go to the Dependencies tab, and add the org.eclipse.e4.core.di.annotations bundle. Then the run method of the HandlerOne, HandlerTwo, and DoubleClick classes should be annotated with @Execute:
    public class HandlerOne {
      @Execute
      public void run() {
        showMessage("Action 1 executed");
      }
    }
  9. Since Eclipse 4.x doesn't require that the handlers be instantiated in the class, delete the contributeToActionBars, fillContextMenu, fillLocalPullDown, fillLocalToolBar, hookContextMenu, and makeActions methods and the action1, action2, and doubleClickAction fields.
  10. The code is now a lot cleaner, but it doesn't have the functionality from before; it is necessary to register the handlers with the menus. This will be done in the next step.

What just happened?

Since Eclipse 4.x handles the creation of menus and wires elements to handlers, the custom code that builds up menus isn't required. By recursively removing references to Action, the handler code can be reduced to a minimum.

Of course if the application is run now, there will be no menus or interactivity. These will be added in the next step.

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

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