org.eclipse.ui.viewActions
It is common for plug-ins to contribute behavior to views that already exist in the workbench. This is done through the
org.eclipse.ui.viewActions
extension point. This extension point allows plug-ins to contribute menu items, submenus and tool bar entries to an
existing view's local
pull-down menu and local tool bar.
Contributing menu items to views is also done using the
org.eclipse.ui.menus
extension point. This allows command targeted at a view to be contributed to
an existing view's local pull-down menu and local tool bar, as in the Contribution location section
covering the Info View of
org.eclipse.ui.menus.
You may have noticed an item in the project explorer's local tool bar that becomes
enabled whenever a readme file is selected. This item also appears in the
project explorer's local
pull-down menu. These actions appear because the readme tool plug-in contributes
them using the
viewActions extension.
The relevant plugin.xml contribution is below.
<extension
point = "org.eclipse.ui.viewActions">
<viewContribution
id="org.eclipse.ui.examples.readmetool.vc1"
targetID="org.eclipse.ui.navigator.ProjectExplorer">
<action id="org.eclipse.ui.examples.readmetool.va1"
label="%PopupMenu.ResourceNav.label"
menubarPath="additions"
toolbarPath="additions"
icon="icons/obj16/editor.png"
tooltip="%PopupMenu.ResourceNav.tooltip"
helpContextId="org.eclipse.ui.examples.readmetool.view_action_context"
class="org.eclipse.ui.examples.readmetool.ViewActionDelegate"
enablesFor="1">
<selection class="org.eclipse.core.resources.IFile" name="*.readme"/>
</action>
</viewContribution>
</extension>
A view contribution with a unique id is specified. The view to which we are adding
the action is specified in the
targetID. We are contributing to the Project
Explorer's menu. We specify the label and the menu bar and
tool bar locations for the new action. (For a complete discussion of menu and toolbar locations, see
Menu and toolbar paths).
We also specify the conditions under which the action should be enabled. You can see that this action will be enabled when there is one selection
(enablesFor="1") of type
IFile
(class="org.eclipse.core.resources.IFile"), whose name has
".readme" in the file extension (name="*.readme"). Sure enough, that's exactly what happens when you click around in the Project Explorer.
The information in the plugin.xml is all that's needed to add items to menus and tool
bars since plug-in code will only run when the action is actually selected from the menu or toolbar.
To provide the action behavior, the implementation class specified in the
plugin.xml must implement the
IViewActionDelegate
interface.
In this example, the readme plug-in supplies ViewActionDelegate to implement the action. If you browse this class you will see that it includes methods for
remembering its view, handling selection changes, and invoking its action.
When invoked the action itself simply launches a dialog that announces it was executed.
public void run(org.eclipse.jface.action.IAction action) {
MessageDialog.openInformation(view.getSite().getShell(),
MessageUtil.getString("Readme_Editor"),
MessageUtil.getString("View_Action_executed"));
}
Although this action is simple, we can imagine how using selections and more
functional dialogs could make this action do something more interesting.