Section 6.5. Editor Actions


6.5. Editor Actions

Actions 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:

  • Section 8.5, Editor Actions, on page 354 for more on editor actions

  • Section 14.2.4, Marker resolutionquick fix, on page 520 for an example of manipulating text in an editor

  • Chapter 17, Creating New Extension Points, for more on extension points

Figure 6-10. Editor actions.


6.5.1. Defining an editor context menu

To 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).

id "com.qualityeclipse.favorites.compilationUnitEditorPopup"

The identifier for the viewer contribution.

targetID "#CompilationUnitEditorContext"

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

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.

id "com.qualityeclipse.favorites.compilationUnitEditorPopup SubMenu"

The identifier for the Favorites menu in the editor's context menu.

label "Favorites"

The text appearing in the editor's context menu as the name of the Favorites submenu.

path "additions"

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

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 action

Add 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.

class "com.qualityeclipse.favorites.actions.

AddToFavoritesActionDelegate"

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

id "com.qualityeclipse.favorites.addToFavoritesInCompilationUnitEditor"

The identifier for the action.

label "Add"

The name of the action appearing in the Favorites submenu.

menubarPath "com.qualityeclipse.favorites.compilationUnitEditorPopupSubMenu/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). To make the action appear directly in the editor's context menu rather than in the Favorites submenu, use a value of "additions" instead.

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. IEditorActionDelegate

The 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 menu

Using 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.

id "com.qualityeclipse.favorites.compilationUnitEditorActions"

The identifier for the editor contribution.

targetID "org.eclipse.jdt.ui.CompilationUnitEditor"

The identifier of the type of editor that should be open for these menus and actions to be visible.

Add the Favorites menu by right-clicking on editorContribution and selecting New > menu. Enter the following attributes for the new menu.

id "com.qualityeclipse.favorites.compilationUnitEditorPopupSubMenu"

The identifier for the Favorites menu.

label "Favorites"

The text appearing in the workbench window menu bar as the name of the Favorites submenu.

path "additions"

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

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 action

Add 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).

class "com.qualityeclipse.favorites.actions.

AddToFavoritesActionDelegate"

The fully qualified name of the class that implements the org.eclipse.ui.IEditorActionDelegate interface and performs the action. In this case, the action delegate used in the object contribution was modified in Section 6.5.3, IEditorActionDelegate, on page 246 and thus can be used here as well.

id "com.qualityeclipse.favorites.addToFavoritesInCompilationUnitEditor"

The identifier for the action.

label "Add"

The text appearing in the Favorites menu for the action.

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

The insertion point that indicates where the menu will be positioned in the menu bar (see Section 6.2.5, Insertion points, on page 215).

Other available action attributes that are not used in this example include the following:

definitionId The command identifier for the action, allowing a key sequence to be associated with the action. For more details, see Section 6.7, RFRS Considerations, on page 256.

enablesFor An expression indicating when the action will be enabled (see Section 6.3.2, Action filtering and enablement, on page 227). If blank, then the action is always active unless overridden programmatically by using the IAction interface.

helpContextId The identifier for the help context associated with the action (see Section 15.3.1, Associating context IDs with items, on page 553).

hoverIcon An image displayed when the mouse hovers over the action without being clicked (see Section 6.2.4, Action images, on page 214 for more detail).

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

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 that defines the visual form of the action and that has one of the following values:

push A normal menu or toolbar item (the default style).

radio A radio button-style menu or toolbar item where only one item at a time in a group can be active (see the state attribute).

toggle A checked menu item or toggle tool item (see the state attribute).

toolbarPath The insertion point that indicates where the button will appear in the toolbar (see Section 6.2.5, Insertion points, on page 215 for more detail).

tooltip The text appearing when the mouse hovers over the action's icon in the workbench toolbar.

6.5.6. Defining an editor toolbar action

Similar 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:

icon"icons/sample.gif"

toolbarPath"Normal/additions"

tooltip"Add the editor selection to the Favorites view"

6.5.7. Adding tests for the new actions

As 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 identifiers

The 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 




Eclipse(c) Building Commercial-Quality Plug-ins
Eclipse: Building Commercial-Quality Plug-ins (2nd Edition)
ISBN: 032142672X
EAN: 2147483647
Year: 2004
Pages: 200

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net