我们刚了解了当编辑器活动时,它们可以如何将它们自己的操作添加到工作台菜单和工具栏。当另一个插件的编辑器变成活动时, org.eclipse.ui.editorActions 扩展点允许插件向工作台菜单和工具栏作添加。
在自述文件示例中,插件使用 editorActions 扩展点来为自述文件编辑器添加的菜单添加附加操作。现在,plugin.xml 中的定义应该看起来相当熟悉。
<extension
point = "org.eclipse.ui.editorActions">
<editorContribution
id="org.eclipse.ui.examples.readmetool.ec1"
targetID="org.eclipse.ui.examples.readmetool.ReadmeEditor">
<action id="org.eclipse.ui.examples.readmetool.ea1"
label="&Readme Editor Extension"
toolbarPath="ReadmeEditor"
icon="icons/basic/obj16/editor.gif"
tooltip="Run Readme Editor Extension"
class="org.eclipse.ui.examples.readmetool.EditorActionDelegate"
/>
</editorContribution>
</extension>
与视图操作类似,扩展必须指定它正在为其添加操作的编辑器的 targetID。该操作本身非常类似于视图操作(id、label和 toolbarPath), 只不过指定的类必须实现 IEditorActionDelegate。
注意,此标记中未指定菜单栏路径。这就意味着当编辑器活动时,该操作将出现在工作台工具栏中,而不是在工作台菜单栏中。(有关工具栏和菜单路径的讨论,参见菜单和工具栏路径。)
的确,当编辑器活动时,我们会看到编辑器操作出现在由编辑器本身添加的操作旁边的工具栏上。
自述文件工具提供了 EditorActionDelegate 来实现该操作。这很象我们先前看到的视图操作代理。
public void run(IAction action) {
MessageDialog.openInformation(editor.getSite().getShell(),
"Readme Editor",
"Editor Action executed");
}