Eclipse 提供三大類菜單,覆蓋不同交互場景:
| 擴展點 | 別名 | 核心特點 | 適用菜單類型 |
|---|---|---|---|
| org.eclipse.ui.commands | Commands 方式 | 界面表現與實現分離:Command → MenuContribution(locationURI) → Handler。推薦新開發使用。 | 全部 |
| org.eclipse.ui.actionSets | Actions 方式 | 表現與實現耦合在 Action 中,舊插件常用。需理解 menubarPath/toolbarPath 路徑語法。 | 主菜單、視圖菜單 |
| org.eclipse.ui.popupMenus | popupMenus 方式 | 專門針對上下文菜單,通過 objectClass 綁定目標對象類型。 | 上下文菜單 |
org.eclipse.ui.commands):邏輯分組管理。org.eclipse.ui.menus):通過 locationURI 精確定位。
menu:org.eclipse.ui.main.menu?after=additionstoolbar:org.eclipse.ui.main.toolbar?after=additionsmenu:viewId?after=additionstoolbar:viewId?after=additionspopup:viewId?after=additions 或 popup:org.eclipse.ui.popup.any?after=additions(全局)menu:org.eclipse.search.menu?dialogGrouporg.eclipse.ui.handlers):Handler 類實現 IHandler 接口。<extension point="org.eclipse.ui.commands">
<category id="com.free.menu.category" name="Menu Example"/>
<command categoryId="com.free.menu.category" id="com.free.menu.commands.jokeCommand" name="Joke Command"/>
<command categoryId="com.free.menu.category" id="com.free.menu.commands.angryCommand" name="Angry Command"/>
</extension>
<extension point="org.eclipse.ui.menus">
<menuContribution locationURI="menu:org.eclipse.ui.main.menu?after=additions">
<menu id="com.free.menu.MenuExample" label="Menu Example">
<command commandId="com.free.menu.commands.jokeCommand" style="push"/>
<command commandId="com.free.menu.commands.angryCommand" style="push"/>
</menu>
</menuContribution>
<menuContribution locationURI="menu:com.free.menu.view.MenuExplorer?after=additions">
<command commandId="com.free.menu.commands.jokeCommand" icon="icons/searchres.gif" style="push">
<visibleWhen checkEnabled="false">...</visibleWhen> <!-- 選中 Person 類型時顯示 -->
</command>
</menuContribution>
<menuContribution locationURI="popup:com.free.menu.view.MenuExplorer?after=additions">
<command commandId="com.free.menu.commands.jokeCommand" icon="icons/searchres.gif" style="push"/>
<command commandId="com.free.menu.commands.angryCommand" style="push"/>
</menuContribution>
</extension>
<extension point="org.eclipse.ui.handlers">
<handler class="com.free.menu.actions.JokeCommand" commandId="com.free.menu.commands.jokeCommand">
<enabledWhen><count value="2"/></enabledWhen> <!-- 僅選中 2 個對象時啟用 -->
</handler>
<handler class="com.free.menu.actions.AngryCommand" commandId="com.free.menu.commands.angryCommand"/>
</extension>
通過 org.eclipse.ui.actionSets,Action 類實現 IWorkbenchWindowActionDelegate。menubarPath 指定位置(如 org.eclipse.search.menu/dialogGroup),toolbarPath 指定工具欄位置。enablesFor="+" 表示選中至少一個對象啟用。
通過 org.eclipse.ui.popupMenus,objectContribution 的 objectClass 綁定目標類型(如 IResource、IGraphicalEditPart、自定義 Model 類)。Action 實現 IObjectActionDelegate。支持 visibility 和 enablement 通過 objectState 根據模型屬性值動態控制顯示/啟用。
visibleWhen 控制顯隱,enabledWhen/activeWhen 控制啟用/禁用。enablement 控制啟用/禁用。objectContribution 的 visibility/enablement 或 Action 級別的 enablement。高級場景:GMF 編輯器中可擴展 org.eclipse.gmf.runtime.common.ui.services.action.actionFilterProviders,實現 AbstractActionFilterProvider 的 testAttribute 方法,無需模型實現 IActionFilter。
示例創建「Menu Example」視圖,集成三種擴展點方式:
visibleWhen(選中 Person 顯示)與 enabledWhen(選中 2 個啟用)。視圖代碼中 hookContextMenu() 註冊上下文菜單,viewMenuAction() 通過 IMenuManager.update() 程序化刷新菜單狀態。模型類 Person 實現 IActionFilter.testAttribute() 支持屬性級可見性控制。
locationURI,善用 ?after=additions 等位置參數控制排序。visibleWhen/enabledWhen 表達式實現聲明式動態控制,減少程序化代碼。