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 Application | Windows | Trimmed Window | Controls | Perspective Stack | Perspective | Controls | PartSashContainer | Part Stack | Part (Hello) | Menus.
  3. Right-click on the Menus node and go to Add child | Popup Menu. Now right-click on the Popup Menu and do Add child | HandledMenuItem. This is exactly the same as for other menus; fill in the details as follows:
    • Label: Hello
    • Command: helloCommand - com.packtpub.e4.application.command.hello
    Time for action – creating a pop-up menu and a view menu
  4. Right-click on the Menus node again, and go to Add child | View Menu. Give the menu a label View Menu and right-click to Add child | Handled MenuItem. Use the same label and command as for the pop-up menu.
  5. Run the application. On the top-right, there will be a triangular drop-down icon 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.
  6. Go to Popup Menu and set the ID to com.packtpub.e4.application.popupmenu.hello:
    Time for action – creating a pop-up menu and a view menu
  7. Add the org.eclipse.e4.ui.workbench.swt dependency to the dependencies tab of the plugin.xml editor.
  8. Add a line to the Hello class' 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:
    public void create(Composite parent, EMenuService menu) {
      menu.registerContextMenu(parent,
        "com.packtpub.e4.application.popupmenu.hello");
  9. 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 pop-up menu 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 pop-up 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