Readme 工具動作集也定義了重新目標化的動作。只要 Readme 工具集是可見的,動作就維持為可見的,但是只有當實作動作的檢視畫面或編輯器在作用中時才會啟用動作。使用動作集來定義重新目標化的動作時,動作是建立在動作集標記中而非程式碼中。下列是 Readme 工具的動作集定義:
<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.gif" 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.gif" retarget="true" allowLabelUpdate="true"> </action> ...
重新目標化的動作是使用 retarget="true" 屬性來指定的。將使得 RetargetAction 建立在動作集中。請注意,重新目標化的動作不會指定實作類別,因為 它是依每一個外掛程式中檢視畫面或編輯器的需求來設定實作每一個動作的處理程式。如果 allowLabelUpdate 為 True,將建立 LabelRetargetAction 取而代之。
當 Readme 動作集為可見時,將在視窗功能表中看見重新目標化的動作。然而,如果 Readme 工具的編輯器或概要檢視畫面不在作用中,將不會啟用重新目標化動作。
編輯器和檢視畫面必須要做些什麼?再一次,用戶端近似於為工作台或編輯器的重新目標化動作登錄處理程式。登錄廣域動作處理程式時,必須使用標記中所指定的動作 ID。
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); }
當內容摘要器在作用中時,我們應該可以看到重新標籤的動作。