6.4. View ActionsThere 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 submenuSimilar 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.
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:
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 actionFinally, 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:
Other action attributes applicable but not used here include the following.
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. IViewActionDelegateThe 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 actionIn 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.
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.
6.4.5. Defining a view pull-down submenu and actionThe 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:
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.
6.4.6. Manually testing the new actionsWhen 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 actionsThere 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 identifiersThe 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. id = org.eclipse.ant.ui.views.AntView menuId = org.eclipse.ant.ui.views.AntView id = org.eclipse.ui.views.BookmarkView menuId = org.eclipse.ui.views.BookmarkView id = org.eclipse.debug.ui.BreakpointView menuId = org.eclipse.debug.ui.BreakpointView id = org.eclipse.ui.console.ConsoleView menuId = org.eclipse.ui.console.ConsoleView id = org.eclipse.debug.ui.DebugView menuId = org.eclipse.debug.ui.DebugView id = org.eclipse.jdt.debug.ui.DisplayView menuId = org.eclipse.jdt.debug.ui.DisplayView id = org.eclipse.debug.ui.ExpressionView menuId = org.eclipse.debug.ui.VariableView.detail menuId = org.eclipse.debug.ui.ExpressionView id = org.eclipse.jdt.ui.MembersView menuId = org.eclipse.jdt.ui.MembersView id = org.eclipse.debug.ui.MemoryView menuId = org.eclipse.debug.ui.MemoryView.MemoryBlocksTreeViewPane id = org.eclipse.ui.views.ResourceNavigator menuId = org.eclipse.ui.views.ResourceNavigator id = org.eclipse.jdt.ui.PackageExplorer menuId = org.eclipse.jdt.ui.PackageExplorer id = org.eclipse.jdt.ui.PackagesView menuId = org.eclipse.jdt.ui.PackagesView id = org.eclipse.ui.views.ProblemView menuId = org.eclipse.ui.views.ProblemView id = org.eclipse.jdt.ui.ProjectsView menuId = org.eclipse.jdt.ui.ProjectsView id = org.eclipse.debug.ui.RegisterView menuId = org.eclipse.debug.ui.VariableView.detail menuId = org.eclipse.debug.ui.RegisterView id = org.eclipse.ui.views.TaskList menuId = org.eclipse.ui.views.TaskList id = org.eclipse.jdt.debug.ui.MonitorsView menuId = org.eclipse.jdt.debug.ui.MonitorsView id = org.eclipse.jdt.ui.TypesView menuId = org.eclipse.jdt.ui.TypesView id = org.eclipse.debug.ui.VariableView menuId = org.eclipse.debug.ui.VariableView.detail menuId = org.eclipse.debug.ui.VariableView |