Task 2: Assigning Context Menus to the Viewer

 

Solution

In general, this task consists of two steps: You must create a context menu instance, which you then link with the desired UI component.

 

1. Creating the Context Menu

Proceed as follows:

 

a)  Create an instance of the menu factory:

 

 

IMenuFactory menuFactory = new MenuFactory();

 

 

b) Add the path provider to the container and assign the menu factory:

 

 

menuFactory.getPathProviderManager().addPathProvider(new DefaultPathProvider());

 

 

c) Create a context menu instance:

 

 

IContextMenu contextMenu = new DynamicContextMenu(menuFactory);

 

 

2. Linking the Context Menu with the Control

There are different ways of linking with the context menu:

If you want to connect a context menu with SWT controls, use the static method ContextMenu.setContextMenuToStructuredViewer()or ContextMenu.setContextMenuToControl().

If you are using a SAP Viewer (GenericDataModelTreeViewer or other), you can also use the relevant set method setContextMenu(IcontextMenu contextMenu).

 

Example

This example shows how a context menu is linked with the Model Viewer using the setContextMenu() method:

 

 

GenericDataModelFilteredTreeViewer viewer =                 GenericDataModelFilteredTreeViewer.createFilteredTreeViewer(parent);

 

viewer.setContextMenu(contextMenu);