org.eclipse.ui.popupMenus
The
org.eclipse.ui.popupMenus
extension
point allows a plug-in to contribute to the popup menus of other
views and editors.
You can contribute an action to a specific popup menu by its id (viewerContribution), or
by associating it with a particular object type (objectContribution).
- An objectContribution will cause the menu
item to appear in popup menus for views or editors where objects of
the specified type are selected.
- A viewerContribution will cause the menu
item to appear in the popup menu of a view or editor specified by id in the
markup.
You can add commands to context menus for a similar result, see the
Contributing to popup menus section in
org.eclipse.ui.menus.
The readme tool defines both. Let's look at the object contribution
first.
<extension point = "org.eclipse.ui.popupMenus">
<objectContribution
id="org.eclipse.ui.examples.readmetool"
objectClass="org.eclipse.core.resources.IFile"
nameFilter="*.readme">
<action id="org.eclipse.ui.examples.readmetool.action1"
label="%PopupMenus.action"
icon="icons/ctool16/openbrwsr.png"
menubarPath="additions"
helpContextId="org.eclipse.ui.examples.readmetool.open_browser_action_context"
class="org.eclipse.ui.examples.readmetool.PopupMenuActionDelegate"
definitionId="org.eclipse.ui.examples.readmetool.action1"
enablesFor="1">
</action>
</objectContribution>
...
Object contribution
The action "Show Readme Action" is contributed for the object class
IFile. This means that any view containing
IFile
objects will show the contribution if
IFile
objects are selected. We see that the selection criteria is restricted further with a name filter
(nameFilter="*.readme") and for single selections (enablesFor="1"). As we've discussed before, the registration of this menu does not run any code from our plug-in until the menu item is actually selected.
When the menu item is selected, the workbench will run the specified class.
Since the popup is declared as an objectContribution,
the supplied class must implement
IObjectActionDelegate
.
The action is implemented in PopupMenuActionDelegate.
public void run(IAction action) {
MessageDialog.openInformation(
this.part.getSite().getShell(),
"Readme Example",
"Popup Menu Action executed");
}
We can see the popup menu contribution when we select a readme file from the resource navigator.
Viewer contribution
A viewer contribution is used to contribute to a specific view or editor's popup menu
by using its id.
Here is the readme tool's viewer contribution:
...
<viewerContribution
id="org.eclipse.ui.examples.readmetool2"
targetID="org.eclipse.ui.examples.readmetool.outline">
<action id="org.eclipse.ui.examples.readmetool.action1"
label="%PopupMenus.action"
icon="icons/ctool16/openbrwsr.png"
menubarPath="additions"
helpContextId="org.eclipse.ui.examples.readmetool.open_browser_action_context"
definitionId="org.eclipse.ui.examples.readmetool.action1"
class="org.eclipse.ui.examples.readmetool.ViewActionDelegate">
</action>
</viewerContribution>
</extension>
Note: The name viewerContribution is somewhat misleading, as it does not relate to JFace viewers. A better name would
be popupMenuContribution.
When the extension is a viewerContribution, the
supplied class must implement the
IEditorActionDelegate
or
IViewActionDelegate
interface, depending on whether the
action is contributed to an editor's or view's popup menu.
The targetID specifies the view whose popup menu will be altered. In this
case, we are adding an action to one of the readme tool views, the
outliner. The action itself is similar to others that we've seen. We specify the id,
definitionId, label, and icon of the action, and
the path within the popup for our contribution. The action will be
shown only in the readme outline view's popup menu.
The interfaces required to contribute a
viewerContribution
to the
popupMenus extension
point are the same as those required by the
viewActions and
editorActions extension points. If you want to contribute the same action to the popup menu and the local menu of a view or editor, you can use the same class for both extensions.