自述文件工具操作集还定义可重定目标的操作。只要自述文件操作集是可视时,该操作就是可视的,但仅当实现该操作的视图或编辑器活动时才会启用该操作。当使用操作集定义可重定目标的操作时,操作是在操作集标记中创建的而不是使用代码创建的。以下内容来自于自述文件工具的操作集定义:
<extension point = "org.eclipse.ui.actionSets"> <actionSet id="org_eclipse_ui_examples_readmetool_actionSet" label="%ActionSet.name" visible="true"> ... <action id="org_eclipse_ui_examples_readmetool_readmeRetargetAction" menubarPath="window/org_eclipse_ui_examples_readmetool/slot1" toolbarPath="readme" label="%ReadmeRetargetAction.label" tooltip="%ReadmeRetargetAction.tooltip" helpContextId="org.eclipse.ui.examples.readmetool.open_browser_action_context" icon="icons/ctool16/openbrwsr.png" retarget="true"> </action><action id="org_eclipse_ui_examples_readmetool_readmeRelabelRetargetAction" menubarPath="window/org_eclipse_ui_examples_readmetool/slot1" toolbarPath="readme" label="%ReadmeRelabelRetargetAction.label" tooltip="%ReadmeRelabelRetargetAction.tooltip" helpContextId="org.eclipse.ui.examples.readmetool.open_browser_action_context" icon="icons/ctool16/openbrwsr.png" retarget="true" allowLabelUpdate="true"> </action>...
通过使用 retarget="true" 属性指定重定目标操作。这将导致在操作集中创建 RetargetAction。注意,可重定目标的操作不指定实现类,因为要靠插件中的每个视图或编辑器设置实现每个操作的处理程序。如果 allowLabelUpdate 为 true,则将创建 LabelRetargetAction。
当自述文件操作集可视时,重定目标操作将在窗口菜单中可视。但是,如果自述文件工具的编辑器或大纲视图不活动,则不会启用这些操作。
编辑器和视图必须进行什么操作?同样的,客户机端与为工作台或编辑器的可重定目标的操作注册处理程序相似。注册全局操作处理程序时,必须使用标记中指定的操作标识。
ReadmeEditorActionBarContributor 对编辑器完成此任务。首先,它为这些操作定义处理程序。
public ReadmeEditorActionBarContributor() { ... handler4 = new EditorAction(MessageUtil.getString("Editor_Action4")); handler5 = new EditorAction(MessageUtil.getString("Editor_Action5")); handler5.setToolTipText(MessageUtil.getString("Readme_Editor_Action5")); ... }
这些处理程序与编辑器可重定目标的操作的处理程序在同一时间注册。
public void init(IActionBars bars, IWorkbenchPage page) { ... bars.setGlobalActionHandler(IReadmeConstants.ACTION_SET_RETARGET4, handler4); bars.setGlobalActionHandler(IReadmeConstants.ACTION_SET_LABELRETARGET5, handler5); ... }
记住,操作栏添加程序在同一编辑器的不同实例之间共享。这意味着如果 ReadmeEditorActionBarContributor 的活动编辑器更改,则必须通知处理程序。
public void setActiveEditor(IEditorPart editor) { ... handler4.setActiveEditor(editor); handler5.setActiveEditor(editor); ... }
这是用于该编辑器的。应该会看到激活编辑器时将启用这些操作。
注意,并未使用第一个可重定目标的操作(“Editor Action 4”)的标签,因为操作集 XML 标记未设置 allowLabelUpdate。
ReadmeContentOutlinePage 在为编辑器的可重定目标的操作定义处理程序的相同位置定义它的处理程序:
public void createControl(Composite parent) { ... action = new OutlineAction(MessageUtil.getString("Outline_Action4")); getSite().getActionBars().setGlobalActionHandler( IReadmeConstants.ACTION_SET_RETARGET4, action); action = new OutlineAction(MessageUtil.getString("Outline_Action5")); action.setToolTipText(MessageUtil.getString("Readme_Outline_Action5")); getSite().getActionBars().setGlobalActionHandler( IReadmeConstants.ACTION_SET_LABELRETARGET5, action); }
内容大纲窗口活动时,我们应该会看到它的重新标记的操作。