Time for action – creating a pop-up menu and a view menu

Pop-up and view menus are defined declaratively in the Application.e4xmi file. These are specific to a part, so the option is defined underneath the part declaration.

  1. Open the Application.e4xmi file.
  2. Navigate to the Application | Windows and Dialogs | Trimmed Window | Controls | Perspective Stack | Perspective | Controls | PartSashContainer | Part Stack | Part (Hello) | Menus node.
  3. Right-click on the Menus node and choose Add child | Popup Menu. Set the ID to com.packtpub.e4.application.popupmenu.hello, which will be used in code later.
    Time for action – creating a pop-up menu and a view menu
  4. Right-click on the Popup Menu and choose Add child | Handled Menu Item. This is exactly the same as for other menus; fill in the details as follows:
    1. Label: Hello
    2. Command: helloCommand - com.packtpub.e4.application.command.hello
    Time for action – creating a pop-up menu and a view menu
  5. Right-click on the Menus node again, and choose Add child | View Menu. Give the menu a label View Menu and right-click to choose Add child | Handled Menu Item. Use the same label and command as for the pop-up menu.
  6. Run the application. On the top-right, there will be a triangular drop-down which should contain the view menu. However, the pop-up menu won't be triggered, because the SWT component has to be bound to the pop-up menu through its ID in code.
  7. Add a line to the Hello class's create method that registers the context menu with the ID specified. To do this, a new parameter EMenuService menu needs to be passed, from which registerContextMenu can be called. Since this returns a boolean value indicating success, log an error if the registration does not work:
    public void create(Composite parent, EMenuService menu) {
      if (!menu.registerContextMenu(parent,
       "com.packtpub.e4.application.popupmenu.hello")) {
        logService.log(LogService.LOG_ERROR,
         "Failed to register pop-up menu");
      }
      ...
  8. Run the application, and right-click on the hello label or elsewhere in the Hello part. The pop-up menu should be shown, and the Hello command can be run.

What just happened?

The pop-up menu can be associated with a part, but it doesn't get shown by default. Instead, it has to be registered with a SWT widget. The popup can be for the entire part's component, or it can be just for specific components in the part.

The EMenuService is the interface to the E4 menus. It gets injected into the creation of the widget and provides the detector to listen to the mouse and keyboard events that trigger the popup menu.

Adding a View Menu is exactly the same as a Popup Menu, except that no additional code is required to make it happen.

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

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