Previous Page
Next Page

6.4. View Actions

There are several ways in which actions can be manifested as part of a view. For example, the Members view has toolbar buttons that appear in its title bar, a pull-down menu appearing at the right of the toolbar buttons, and a context menu containing yet more actions (see Figure 6-9). Actions are added to views using the extension point mechanism, similar to the discussions in the previous two sections. In addition, views can programmatically provide their own actions, bypassing the extension point mechanism (see Section 7.3, View Actions, on page 283).

Figure 6-9. View actions.


6.4.1. Defining a view context submenu

Similar to an objectContribution, a viewerContribution is used to add a menu item to a context menu. Whereas an objectContribution causes a menu item to appear based on the selection in the viewer, a viewerContribution causes a menu item to appear based on the type of viewer. As with an objectContribution, the viewerContribution element can have a single visibility subelement that takes control when all its other subelements are visible to the user (see Section 6.3.2, The visibility element, on page 228).

The Favorites submenu shows up in several different types of views, but not in the Members view. It would probably be more appropriate to use the objectContribution approach discussed in Section 6.3, Object Actions, on page 224 to target objects contained in the Members view; however, use the viewerContribution instead as an example.

Start by right-clicking on the popupMenu extension that was added as part of Section 6.3.1, Defining an object-based action, on page 224 and select New > viewerContribution. Fill in the following attributes for the newly added viewerContribution.

id "com.qualityeclipse.favorites.membersViewPopup"

The identifier for this view contribution.

targetID "org.eclipse.jdt.ui.MembersView"

The identifier of the view's context menu to which the submenu will be added (see Section 20.6, Modifying Eclipse to Find Part Identifiers, on page 727).

Add the Favorites menu to the Members view context menu by right-clicking on the viewerContribution and selecting New > menu. Enter the following attributes for the new menu:

id "com.qualityeclipse.favorites.membersViewPopupSubMenu"

The identifier for the Favorites menu in the Members view context menu.

label "Favorites"

The text appearing in the Members view context menu as the name of the Favorites submenu.

path "additions"

The insertion point that determines the location in the Members view context menu where the Favorites submenu will appear (see Section 6.2.5, Insertion points, on page 215).

Next, add a groupMarker to the menu with the name "content" and a separator with the name "additions" (see Section 6.2.2, Groups in a menu, on page 212).

6.4.2. Defining a view context menu action

Finally, add an action to the Favorites submenu by right-clicking on the viewerContribution, selecting New > action, and entering the following attributes for the new action:

class "com.qualityeclipse.favorites.actions.

AddToFavoritesActionDelegate"

The fully qualified name of the class that implements the org.eclipse.ui.IViewActionDelegate interface and performs the action. In this case, the same action delegate used in the object contribution is used here as well, with a few modifications (see Section 6.4.3, IViewActionDelegate, on page 240). The class is instantiated using its no argument constructor, but may be parameterized using the IExecutableExtension interface (see Section 20.5, Types Specified in an Extension Point, on page 723).

id "com.qualityeclipse.favorites.addToFavoritesInMembersView"

The identifier for the action.

label "Add"

The name of the action as it appears in the Favorites submenu.

menubarPath "com.qualityeclipse.favorites.membersViewPopup SubMenu/content"

The insertion point that determines the location in the Favorites submenu where the action will appear (see Section 6.2.5, Insertion points, on page 215). If the action is to appear directly in the Members view context menu rather than in the Favorites submenu, use the value "additions" instead.

tooltip "Add selected member's compilation unit to the Favorites view" The text describing the action.

Other action attributes applicable but not used here include the following.

enablesFor An expression indicating when the action will be enabled (see Section 6.3.2, Action filtering and enablement, on page 227).

helpContextId The identifier for the help context associated with the action (see Chapter 15, Implementing Help).

icon The associated image (see Section 6.2.4, Action images, on page 214).

overrideActionId An optional attribute specifying the identifier for an action that the action overrides.

state For an action with either the radio or toggle style, set the initial state to true or false (see Section 6.2.3, Defining a menu item and toolbar button, on page 212).

style An attribute defining the visual form of the action. This is covered in Section 6.2.3, Defining a menu item and toolbar button, on page 212, with the exception that the pulldown style does not apply to object contributions.

You can also specify selection and enablement subelements to the action element similar to Section 6.3.2.4, The selection element, on page 231 and Section 6.3.2.5, The enablement element, on page 231.

6.4.3. IViewActionDelegate

The action delegate for a view contribution must implement the org.eclipse.ui.IViewActionDelegate interface, so you need to modify the class AddToFavoritesActionDelegate first introduced in Section 6.3.3, IObjectActionDelegate, on page 233. First, add the IViewActionDelegate interface to the implements clause, and then add the following init() method to cache the target part. All other aspects of the action delegate stay the same.

public void init(IViewPart view) {
 this.targetPart = view;
}

6.4.4. Defining a view toolbar action

In addition to being in the Favorites submenu of the view context menu, the action should appear as a toolbar button in the Members view (see Section 7.3.3, Toolbar buttons, on page 287, to programmatically add a toolbar button to a view). As in Section 6.2.1, Defining a workbench window menu, on page 209, and subsequent sections, use the Extensions page of the plug-in manifest editor to create the new view contribution. Click the Add button to add an org.eclipse.ui.viewActions extension, then add a viewContribution to that with the following attributes.

id "com.qualityeclipse.favorites.membersViewActions"

The identifier for the view contribution.

targeted "org.eclipse.jdt.ui.MembersView"

The identifier of the view to which the actions are added.

Next, add an action to the Members view toolbar by right-clicking on the viewContribution, selecting New > action, and then entering the attributes shown below for the new action. All the objectContribution action attributes listed in Section 6.3.1, Defining an object-based action, on page 224 also apply to viewContribution actions.

class "com.qualityeclipse.favorites.actions.

AddToFavoritesActionDelegate"

The fully qualified name of the class that implements the org.eclipse.ui.IViewActionDelegate interface and performs the action. In this case, the same action delegate used in the object contribution is used here as well, with a few modifications (see Section 6.4.3, IViewActionDelegate, on page 240).

icon "icons/sample.gif"

The icon displayed in the view's toolbar for the action.

id "com.qualityeclipse.favorites.addToFavoritesInMembersView"

The identifier for the action.

toolbarPath "additions"

The insertion point that determines the location in the Members view's toolbar where the action will appear (see Section 6.2.5, Insertion points, on page 215).

tooltip "Add the selected items in the Members view to the Favorites view"

The text describing the action appearing in the hover help when the cursor is positioned over the toolbar button associated with the action.

6.4.5. Defining a view pull-down submenu and action

The same viewContribution extension described in the previous section is used to add a view pull-down submenu (see Section 7.3.2, Context menu, on page 283 to programmatically create a view pull-down menu). Typically, a view pull-down menu contains actions, such as sorting and filtering, specific to that view. To add the Favorites submenu and action to the Members view pull-down menu (not that it really needs to be there in addition to everywhere else its been added), right-click on the viewContribution extension, select New > menu, and then set the attributes of the newly created menu as follows:

id "com.qualityeclipse.favorites.membersViewPulldownSubMenu"

The identifier for the Favorites menu in the Members view.

label "Favorites"

The text appearing in the Members view pull-down menu as the name of the Favorites submenu.

path "additions"

The insertion point, which determines the location in the Members view pull-down menu, where the Favorites submenu will appear (see Section 6.2.5, Insertion points, on page 215).

Next, add a groupMarker to the menu with the name "content" and a separator with the name "additions" (see Section 6.2.2, Groups in a menu, on page 212). Finally, the action defined in Section 6.4.4, Defining a view toolbar action, on page 240 can be modified to define a menu item in the menu just created as well as the toolbar button it already described by modifying some of its attributes.

label "Add"

The name of the action appearing in the Favorites submenu.

menubarPath "com.qualityeclipse.favorites.membersViewPulldownSubMenu/content"

The insertion point, which determines the location in the Favorites submenu, where the action will appear (see Section 6.2.5, Insertion points, on page 215). If the action was to appear directly in the Members view pull-down menu rather than in the Favorites submenu, you would have to use a value of "additions" instead.

6.4.6. Manually testing the new actions

When the modifications to the plug-in manifest and the action delegate are complete, launching the Runtime Workbench and inspecting the Members view will show the new Favorites submenu and the Add to Favorites toolbar button.

6.4.7. Adding tests for the new actions

There is no need for any additional test cases other than the ones created in Section 6.3.6, Adding a test for the new action, on page 235 because the same action delegate is being reused. After the Favorites view is fleshed out as part of Chapter 7, Views, more tests for new types of selections can be added.

6.4.8. View context menu identifiers

The context menu identifiers for some Eclipse views follow. For more information on how this list was generated, see Section 20.6, Modifying Eclipse to Find Part Identifiers, on page 727.

Ant

id = org.eclipse.ant.ui.views.AntView
menuId = org.eclipse.ant.ui.views.AntView

Bookmarks

id = org.eclipse.ui.views.BookmarkView
menuId = org.eclipse.ui.views.BookmarkView

Breakpoints

id = org.eclipse.debug.ui.BreakpointView
menuId = org.eclipse.debug.ui.BreakpointView

Console

id = org.eclipse.ui.console.ConsoleView
menuId = org.eclipse.ui.console.ConsoleView

Debug

id = org.eclipse.debug.ui.DebugView
menuId = org.eclipse.debug.ui.DebugView

Display

id = org.eclipse.jdt.debug.ui.DisplayView
menuId = org.eclipse.jdt.debug.ui.DisplayView

Expressions

id = org.eclipse.debug.ui.ExpressionView
menuId = org.eclipse.debug.ui.VariableView.detail
menuId = org.eclipse.debug.ui.ExpressionView

Members

id = org.eclipse.jdt.ui.MembersView
menuId = org.eclipse.jdt.ui.MembersView

Memory

id = org.eclipse.debug.ui.MemoryView
menuId = org.eclipse.debug.ui.MemoryView.MemoryBlocksTreeViewPane

Navigator

id = org.eclipse.ui.views.ResourceNavigator
menuId = org.eclipse.ui.views.ResourceNavigator

Package Explorer

id = org.eclipse.jdt.ui.PackageExplorer
menuId = org.eclipse.jdt.ui.PackageExplorer

Packages

id = org.eclipse.jdt.ui.PackagesView
menuId = org.eclipse.jdt.ui.PackagesView

Problems

id = org.eclipse.ui.views.ProblemView
menuId = org.eclipse.ui.views.ProblemView

Projects

id = org.eclipse.jdt.ui.ProjectsView
menuId = org.eclipse.jdt.ui.ProjectsView

Registers

id = org.eclipse.debug.ui.RegisterView
menuId = org.eclipse.debug.ui.VariableView.detail
menuId = org.eclipse.debug.ui.RegisterView

Tasks

id = org.eclipse.ui.views.TaskList
menuId = org.eclipse.ui.views.TaskList

Threads and Monitors

id = org.eclipse.jdt.debug.ui.MonitorsView
menuId = org.eclipse.jdt.debug.ui.MonitorsView

Types

id = org.eclipse.jdt.ui.TypesView
menuId = org.eclipse.jdt.ui.TypesView

Variables

id = org.eclipse.debug.ui.VariableView
menuId = org.eclipse.debug.ui.VariableView.detail
menuId = org.eclipse.debug.ui.VariableView


Previous Page
Next Page