Search code examples
javaeclipseuser-interfacercpe4

Eclipse RCP 4: Adding a Popup Menu to an MPart


I am trying to add a Popup menu for an MPart when it is right clicked, but I am having difficulty doing so. It should function the same way it did in Eclipse 3.x (Right click near the title of the view and the popup menu to detach the view/move it appears). I have added a Popup Menu with a Handled Menu Item. I assumed this would be all that is required to allow the popup menu to appear on right click, but nothing happens when I do.

I did follow Lars Vogel's tutorials, but his tutorial only shows how to add a JFace viewer to a part, and then adding the popup menu to that viewer. What is the proper way to add a popup menu when you right click on an MPart?

What the MPart looks like in my Application Model

Thanks for any help you may be able to provide! :)

EDIT: Example of what I want

Example


Solution

  • As well as declaring the Popup Menu in the Application.e4xmi (or fragment) you must also use the EMenuService in the code to register the menu with the control that you are going to right click on.

    @Inject
    EMenuService menuService;
    
    
    menuService.registerContextMenu(control, "menu id");
    

    Changing the menu shown when right clicking on the tab for a part is much more complex. To do this you must define a custom renderer for the MPartStack using a custom renderer factory (see here for basic details).

    Your renderer can extend the standard StackRenderer class and override the populateTabMenu method.

    This is an example method I use which reduces the menu to just show 'Close':

    @Override
    protected void populateTabMenu(final Menu menu, final MPart part)
    {
      if (!isClosable(part))
        return;
    
      // Just add Close menu item and handle enabling it correctly
    
      final MenuItem menuItemClose = new MenuItem(menu, SWT.NONE);
    
      menuItemClose.setText(SWTRenderersMessages.menuClose);
    
      menuItemClose.addListener(SWT.Selection, this::closeSelected);
    
      menu.removeListener(SWT.Show, _menuListener);
      menu.addListener(SWT.Show, _menuListener);
    }