





















































(For more resources related to this topic, see here.)
@PostConstruct
public void postConstruct(Composite parent, IEclipseContext ctx,
EMenuService menuService)
menuService.registerContextMenu(snippetsList.getTable(),
"codesnippetapp.snippetlist.popupmenu");
To add a pop-up menu, you first need to create a menu in the application model for a part in which you want to display the menu. In this recipe, we added a menu to the Code Snippets part.
Then, you add menu items. In this recipe, we added two DirectMenuItem. For the main menu bar in the task of adding menu and toolbar buttons, we added HandledMenuItem, because we wanted to share the handler for the menu between toolbar button and menu item. However, in this case, we need only one implementation of options in the pop-up menu, so we created DirectMenuItem. But, if you want to add keyboard shortcuts for the menu options, then you may want to create HandledMenuItem instead of DirectMenuItem.
For each menu item, you set a class URI that is a handler class for the menu item.
The next step is to register this pop-up menu with a UI control. In our application, we want to associate this menu with the TableViewer class that displays a list of snippets. To register a menu with any UI control, you need to get an instance of EMenuService. We obtained this instance in the postConstruct method of SnippetListView using DI—we added the EMenuService argument to the postConstruct method.
Then, we used registerContextMenu of EMenuService to associate the pop-up menu with the TableViewer class. registerContextMenu takes instances of the UI control and menu ID as arguments.
The Delete option in our pop-up menu makes sense only when you click on any snippet. So, when you right-click on an area of TreeViewer that does not have any snippet at that location, the Delete option should not be displayed, only the New Snippet option.
This can be done using core expressions. You can find more information about core expressions at http://wiki.eclipse.org/Platform_Expression_Framework, and http://wiki.eclipse.org/Command_Core_Expressions.
We will use a core expression to decide if the Delete menu option should be displayed. We will add a mouse listener to the TableViewer class. If the mouse was clicked on a Snippet, then we will add SnippetData to IEclipseContext with the snippet_at_mouse_click key. If there is no snippet at the location, then we will remove this key from IEclipseContext.
Then, we will add a core expression to check if the snippet_at_mouse_click variable is of type codesnippetapp.data.SnippetData. We will then associate this core expression with the Delete menu item in the application model.
private static String SNIPPET_AT_MOUSE_CLICK = "snippet_at_mouse_
click";
//Add mouse listener to check if there is a snippet at mouse click
snippetsList.getTable().addMouseListener(new MouseAdapter() {
@Override
public void mouseDown(MouseEvent e)
{
if (e.button == 1) //Ignore if left mouse button
return;
//Get snippet at the location of mouse click
TableItem itemAtClick = snippetsList.getTable().
getItem(new Point(e.x, e.y));
if (itemAtClick != null)
{
//Add selected snippet to the context
ctx.set(SNIPPET_AT_MOUSE_CLICK, itemAtClick.getData());
}
else
{
//No snippet at the mouse click. Remove the variable
ctx.remove(SNIPPET_AT_MOUSE_CLICK);
}
}
});
Carry out to the following steps:
<extension point="org.eclipse.core.expressions.definitions"> <definition id="CodeSnippetApp.delete.snippet.expression"> <with variable="snippet_at_mouse_click"> <instanceof value="codesnippetapp.data.SnippetData"> </instanceof> </with> </definition> </extension>
Run the application. When you right-click on the Snippets List view, which does not have any snippet at this point, you should see only the New Snippet menu option.
In this task, we created a pop-up menu that is displayed when you right-click in the snippets list. If no snippet is selected at a location where you right-click, then it displays a pop-up menu with a single option to add a snippet. If there is a snippet at the location, then we display a menu that has options to delete the snippet and add a snippet.