The next step is to add the view menu, which is the drop-down menu shown by the triangle in the top-right of the original view. Adding this takes the same pattern as before, but the drop-down menu requires an additional tag to be shown as a view menu.
createViewMenu
in the SampleView
class. Add a call to the createPartControl
method so that it is called when the view is created.createViewMenu
method, create an instance of MMenu
using MMenuFactory.INSTANCE.createMenu()
.menu
to the part
using part.getMenus().add(menu)
.menu
as a view menu, add a tag ViewMenu
using menu.getTags().add("ViewMenu")
. MMenuFactory.INSTANCE.createDirectMenuItem()
and store it in a local field one
. Set the tooltip and label appropriately, and use the same icon as the previous step.setContributionURI
method as before. Either use a hard-coded string, "bundleclass://com.packtpub.e4.migration/com.packtpub.e4.migration.views.HandlerOne"
, or use the getURI
method as shown in the previous hero section.menu.getChildren().add(one)
. private void createViewMenu() { MMenu menu = MMenuFactory.INSTANCE.createMenu(); part.getMenus().add(menu); menu.getTags().add("ViewMenu"); List<MMenuElement> children = menu.getChildren(); MDirectMenuItem one = MMenuFactory.INSTANCE.createDirectMenuItem(); one.setLabel("Action 1"); one.setTooltip("Action 1 tooltip"); one.setIconURI("platform:/plugin/org.eclipse.ui/" + "icons/full/obj16/info_tsk.png"); one.setContributionURI(getURI(HandlerOne.class)); children.add(one); }
createMenuSeparator
from the MMenuFactory
:children.add(MMenuFactory.INSTANCE.createMenuSeparator());
HandlerTwo
as well, using the same pattern as mentioned before.Adding a view menu item is very similar to adding a toolbar menu item. Each element has a model object (which begins with M
), so MMenu
stands for M(odel) Menu
. These are then added using standard collections and added to the part as before.
The tags are a way of appending additional information to the model. These are sometimes used to disambiguate between different types of elements; in this case, it allows the menu to be shown as a view menu when the ViewMenu
tag is added (without this tag, the menu won't be shown in the UI).
A direct menu item works in the same way as a direct toolbar item; the contribution URI (stored as a bundleclass
URI) dictates which class will be invoked when the item is chosen from the menu.
Separators provide a horizontal line and can be used to separate items in a menu. When added at the end of a menu, there is no visible effect; but if they are added between menu items, the separator is shown.
3.17.181.21