Time for action – adding the pop-up

A pop-up menu is very similar to creating a toolbar or view menu; however, there are a few extra steps required to hook it up to the viewer and to ensure that the right pop-up menu is connected to the right part. As with the view menu, a tag is required; but instead of being a generic hard-coded value, the pop-up tag requires a tight binding with the pop-up menu.

  1. Create a method called createPopupMenu in the SampleView class. It will need to take an SWT Control parameter, which will be the one that is used to trigger the menu.
  2. Ensure that the createPopupMenu is called from the end of the createPartControl method, passing in the control from the viewer as the argument:
    createPopupMenu(viewer.getControl());
  3. First, create a pop-up menu with a MMenuFactory.INSTANCE.createPopupMenu() call. Assign it to a local variable menu, so that it can be referred to throughout the method.
  4. Set the elementId of the menu to be that of the part, using menu.setElementId(part.getElementId()).
  5. Set a tag on the menu which is the concatenation of popup: and the part's elementId, using menu.getTags().add("popup:" + part.getElementId()).
  6. Add org.eclipse.e4.ui.services to the list of dependent bundles in the MANIFEST.MF to allow the EMenuService class to be used.
  7. Add an injected field menuService of type EMenuService to the SampleView class:
    @Inject
    private EMenuService menuService;
  8. Add the menu to the part and get the children of the menu as before.
  9. To ensure that the menu is shown when the control is clicked, use the menuService to register the context menu, using the part's elementId. The code should look like:
    private void createPopupMenu(Control control) {
      MMenu menu = MMenuFactory.INSTANCE.createPopupMenu();
      menu.setElementId(part.getElementId());
      menu.getTags().add("popup:" + part.getElementId());
      part.getMenus().add(menu);
      menuService.registerContextMenu(control,part.getElementId());
      List<MMenuElement> children = menu.getChildren();
    }
  10. Creating a menu item is the same as before; the MMenuFactory is used to create an MDirectMenuItem instance, which is configured and then added to the children list. The code will look like this:
    MDirectMenuItem one = MMenuFactory.INSTANCE.createDirectMenuItem();
    one.setLabel("Action 1");
    one.setTooltip("Action 1 tooltip");
    one.setContributionURI(getURI(HandlerOne.class));
    one.setIconURI("platform:/plugin/org.eclipse.ui/"
     + "icons/full/obj16/info_tsk.png");
    children.add(one);
  11. Run the code and verify that right-clicking on the tree viewer shows the context-sensitive menu.
  12. Add Action 2 with the HandlerTwo implementation in the same way.

What just happened?

A pop-up menu was created using the factory methods in the MMenuFactory instance, and the menu items populated in a very similar way to the toolbar and view menus. To get the pop-up to show however requires a certain amount of steps to be taken in order to work as expected:

  • The menu's elementId must be the same as the part's elementId
  • The menu must have a tag that starts with popup: followed by the part's elementId
  • The menuService must be used to register the control with the part's elementId

If any of these steps are missed or different values are used, then the control will not work as expected.

The value of the part's elementId could be hard-coded, but it is more efficient to use the getElementId method—and should the part's identity change in the future, then the menus will automatically continue to work.

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

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