Transferred from: http://cnfree2000.spaces.live.com/blog/cns! 6201144f025f9d77! 426. Entry
In eclipse, each editor has its own view. Sometimes we need to add its own functions to it. Expanding the view pop-up menu is one of the essential tasks.
Add a menu item to the popup menu of a view, which includes two methods:
1. viewercontribution, that is, view ID. In this way, only when the view ID is consistent with the given ID will the menu items we define be added to its pop-up menu.
2. objectcontribution: object type. When the selected object type in the view is consistent with the given object type, add the menu item we define in the pop-up menu of the view.
The action of the menu item must inherit from org. Eclipse. UI. Actions. actiondelegate, and then implement the special view actiondelegate interface. For example, to extend the Java Editor:
Public class finderactiondelegate extends actiondelegate implements
Ieditexceptiondelegate {
Let's take a look at the components of the plug-in. xml:
Viewercontribution format:
<Extension
Point = "org. Eclipse. UI. popupmenus">
<Viewercontribution
Targetid = "# texteditorcontext"
Id = "com. cnfree. Finder. Editor">
<Action
Label = "find the largest ID"
Icon = "icon/search.gif"
Helpcontextid = "com. cnfree. Finder. finderaction"
Class = "com. cnfree. Finder. finderactiondelegate"
Menubarpath = "additions"
Enablesfor = "+"
Id = "com. cnfree. Finder">
</Action>
</Viewercontribution>
<Viewercontribution
Targetid = "# compilationuniteditorcontext"
Id = "com. cnfree. Finder. Editor">
<Action
Label = "find the largest ID"
Icon = "icon/search.gif"
Helpcontextid = "com. cnfree. Finder. finderaction"
Class = "com. cnfree. Finder. finderactiondelegate"
Menubarpath = "additions"
Enablesfor = "*"
Id = "com. cnfree. Finder">
</Action>
</Viewercontribution>
</Extension> <br/> <Extension
Point = "org. Eclipse. UI. editexceptions">
</Extension>
Here we need to know the ID defined in the Eclipse plug-in registry for the menu items of the extended view (I will attach the Eclipse plug-in menu item ID Registry later ). For example, the Java editor corresponds to the following ID:
# Texteditorcontext and # compilationuniteditorcontext. After we define the targetid, we point our plug-in ID to the View menu ID. In this way, the Java editor will add our menu items.
<! ATTLIST action
Id CDATA # required
Label CDATA # required
Menubarpath CDATA # implied
Toolbarpath CDATA # implied
Icon CDATA # implied
Disabledicon CDATA # implied
How.con CDATA # implied
Tooltip CDATA # implied
Helpcontextid CDATA # implied
Style (push | radio | toggle) "push"
State (true | false)
Class CDATA # required
Enablesfor CDATA # implied>
<Br/> This element defines the operations that can be called on the user interface.
- ID-used as the unique identifier for reference to this operation.
- Label-the name that can be translated for the menu item text or toolbar button label. This name can contain mnemonic information.
- Menubarpath-specifies the path to which the operation is operated by slash ("/") in the drop-down menu. Each tag in the path (except the last one) must be a valid identifier of the existing menu in the hierarchy. The last tag indicates the name group in which the operation is to be added. If the path is omitted, this operation will not appear in the drop-down menu.
- Toolbarpath-name group in the local Toolbar of the target view. If the group does not exist, it is created. If this option is omitted, the operation will not appear in the local toolbar.
- Icon-indicates the relative path of the Operation icon in the context of the operation. If this item is omitted and the operation appears in the toolbar, the workbench uses a placeholder icon. This path is relative to the position of the plug-in. xml file added to the plug-in. The icon appears in the toolbar but does not appear in the drop-down menu.
- Disabledicon-when the operation is disabled, the relative path of the icon used to visually represent the operation in the context of the operation. If this item is omitted, the normal icon will become grayed out. This path is relative to the position of the plug-in. xml file added to the plug-in. The disabled icon appears in the toolbar, but not in the drop-down menu.
- How.con-when the mouse pointer is on the Operation side, it is used to visually represent the relative path of the Operation icon in the context of the operation. If this option is omitted, the normal icon is used. This path is relative to the position of the plug-in. xml file added to the plug-in.
- Tooltip-indicates the translatable text of the tooltip for the operation. It is used only when the operation appears in the toolbar.
- Helpcontextid-a unique identifier indicating the help context for this operation. If an operation appears as a menu item, Press F1 to display help when the menu item is highlighted.
- Style-optional attributes of the user interface style type used to define the operation. If this item is defined, the attribute value is one of the following:
Push-as a normal menu item or tool item.
Radio-serves as a menu item or tool item for a Single-choice style. Operations with a single-choice style in the same menu or toolbar group are equivalent to a single-choice button set. The initial value is specified by the state attribute.
Toggle-as a menu item for selecting a style or a tool item for switching. The initial value is specified by the state attribute.
- State-an optional attribute indicating the initial state (true or false). This attribute is used when the style property has a value of radio or toggle.
- Class-name of the standard class that implements org. Eclipse. UI. iviewactiondelegate.
- Enablesfor-indicates the value of the selected count that must be met before the operation can be enabled. If this attribute is specified and the condition is met, the operation is enabled. If the conditions are not met, the operation is disabled. If no attribute is specified, any number of items are selected. The following attribute formats are supported :! -Zero items selected
? -Zero or one item is selected.
+-Select one or more
Multiple, 2 +-select two or more
N-the exact number is selected. For example, nablesfor = "4" is enabled only when four items are selected.
*-You have selected any number of items.
Objectcontribution format:
<Objectcontribution objectclass = "com. cnfree. Action. iopenable">
<Action
Id = "com. cnfree. Open"
Menubarpath = "New. Ext"
Definitionid = "com. cnfree. Open. Selection"
Label = "& Open @ Ctrl + O"
Tooltip = "Open the selected item (s )"
Helpcontextid = "com. cnfree. Open. Selection"
Icon = "icon/open.gif"
Class = "com. cnfree. Action. Op enselectedactiondelegate"
Enablesfor = "+">
</Action>
</Objectcontribution>
Action open implements iopenable, which means that all views that contain iopenable objects add custom menu items to the pop-up menu of iopenable objects.
Example:
Cnfree editor plugin
Instructions for use: Create a Java file and add XXX in its comment. YYY, XXX is a class name, And YYY is a three-digit number ID, such as cnfee.100, cnfree.101, and cnfree.102. Open this Java file in the Java perspective. The right-click menu has an option: search for the largest ID. Enter cnfree, And the cnfree.102 string is automatically found and highlighted.
EclipsePlug-in menu ID Registry reference: http://www.jdg2e.com/ch21.actions.table/doc/
References:
Http://www.eclipsesource.com/EN_ARCHIVE/etips_07072004.html
Eclipse help:Org.eclipse.platform.doc. isV/GUIDE/workbench_basicext_popupmenus.htm