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.
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.createPopupMenu
is called from the end of the createPartControl
method, passing in the control
from the viewer
as the argument:createPopupMenu(viewer.getControl());
MMenuFactory.INSTANCE.createPopupMenu()
call. Assign it to a local variable menu
, so that it can be referred to throughout the method.elementId
of the menu
to be that of the part
, using menu.setElementId(part.getElementId())
.tag
on the menu
which is the concatenation of popup:
and the part's elementId
, using menu.getTags().add("popup:" + part.getElementId())
.org.eclipse.e4.ui.services
to the list of dependent bundles in the MANIFEST.MF
to allow the EMenuService
class to be used.menuService
of type EMenuService
to the SampleView
class:@Inject private EMenuService menuService;
menu
to the part
and get the children
of the menu
as before.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(); }
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);
Action 2
with the HandlerTwo
implementation in the same way.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:
elementId
must be the same as the part's elementId
menu
must have a tag
that starts with popup:
followed by the part's elementId
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.
3.135.183.221