6.5. Editor ActionsActions can be added to editors in a way that is similar to how they are added to views. For example, the Java editor has a context menu, so naturally the Favorites action should show up there regardless of whether it's really needed (see Figure 6-10). In addition, editors can add actions to themselves bypassing the standard extension point mechanism. Some related sections include the following:
Figure 6-10. Editor actions.6.5.1. Defining an editor context menuTo add the Favorites menu to the Java editor's context menu, revisit the popupMenus extension declared in Section 6.3.1, Defining an object-based action, on page 224, right-click, and then select New > viewerContribution. Enter the following attributes for the new viewer contribution. As with object contributions, the visibility subelement can be used to control when the menu and actions appear in the editor's context menu (see Section 6.3.2.2, The visibility element, on page 228).
Next, create the Favorites submenu in the editor's context menu by right-clicking on the new viewer contribution extension and selecting New > menu. Enter the following attributes for the new menu declaration.
Finally, 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.5.2. Defining an editor context actionAdd the Add to Favorites action to the Favorites submenu by right-clicking on the viewer contribution defined in Section 6.5.1, Defining an editor context menu, on page 245 and selecting New > action. Enter the following action attributes.
Other action attributes not listed here are the same as for the viewer contributions outlined in Section 6.4.2, Defining a view context menu action, on page 238. 6.5.3. IEditorActionDelegateThe action delegate for an editor contribution must implement the org.eclipse.ui.IEditorActionDelegate interface, so you must modify the AddToFavoritesActionDelegate class first introduced in Section 6.3.3, IObjectActionDelegate, on page 233. First add the IEditorActionDelegate interface to the implements clause, and then add the following setActiveEditor() method to cache the target part. All other aspects of the action delegate can stay the same. public void setActiveEditor(IAction action, IEditorPart editor) { this.targetPart = editor; } 6.5.4. Defining an editor top-level menuUsing the org.eclipse.ui.editorActions extension point, you can define a workbench window menu and toolbar button that are only visible when an editor of a particular type is open. As discussed in Section 6.2.9, Discussion, on page 222, think twice before adding menus or toolbar buttons to the workbench window itself. The Favorites example doesn't really need this, but the following takes you through the process as a matter of course. To start, click the Add button in the Extensions page of the plug-in manifest editor and add a new org.eclipse.ui.editorActions extension. Right-click on the new extension and select New > editorContribution, then enter the following editorContribution attributes.
Add the Favorites menu by right-clicking on editorContribution and selecting New > menu. Enter the following attributes for the new menu.
Finally, 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.5.5. Defining an editor top-level actionAdd an action to the Favorites menu by right-clicking on the editorContribution, selecting New > action and entering the following attributes shown for the new action. Similar to object contributions, the selection and enablement elements can be used to limit the visibility and enablement of the action (see Section 6.3.2.4, The selection element, on page 231 and Section 6.3.2.5, The enablement element, on page 231).
Other available action attributes that are not used in this example include the following:
6.5.6. Defining an editor toolbar actionSimilar to the way that workbench menu actions can be displayed as toolbar buttons, the editor action defined in Section 6.5.5, Defining an editor top-level action, on page 248 can be modified to show up in the workbench window toolbar by making the following modifications to its attributes: tooltip"Add the editor selection to the Favorites view" 6.5.7. Adding tests for the new actionsAs stated in Section 6.4.7, Adding tests for the new actions, on page 242, tests will be added in Chapter 7, Views, for new types of selections to the same test case that was outlined in Section 6.3.6, Adding a test for the new action, on page 235. 6.5.8. Editor context menu identifiersThe following are the context menu identifiers for some Eclipse editors. For more information on how this list was generated, see Section 20.6, Modifying Eclipse to Find Part Identifiers, on page 727. Ant Editor (build.xml) id = org.eclipse.ant.ui.internal.editor.AntEditor menuId = #TextEditorContext menuId = #TextRulerContext Class File Editor (*.class) id = org.eclipse.jdt.ui.ClassFileEditor menuId = #ClassFileEditorContext menuId = #ClassFileRulerContext Compilation Unit Editor (*.java) id = org.eclipse.jdt.ui.CompilationUnitEditor menuId = #CompilationUnitEditorContext menuId = #CompilationUnitRulerContext Default Text Editor id = org.eclipse.ui.DefaultTextEditor menuId = #TextEditorContext menuId = #TextRulerContext Snippet Editor (*.jpage) id = org.eclipse.jdt.debug.ui.SnippetEditor menuId = #JavaSnippetEditorContext menuId = #JavaSnippetRulerContext |