eclipse plugin 菜單

簡介:?菜單是各種軟件及開發平臺會提供的必備功能,Eclipse 也不例外,提供了豐富的菜單,包括主菜單(Main Menu),視圖 / 編輯器菜單(ViewPart/Editor Menu)和上下文菜單(Context Menu)。在 Eclipse 中,幾乎所有的 Workbench Part 提供了人性化的菜單,大大方便了用戶的操作。因此,如何擴展 Eclipse 的菜單功能,并實現特定于我們自己插件的菜單,是插件開發者必須掌握的重要技能,同時,Eclipse 提供了豐富的擴展點供開發人員使用。本文將首先介紹 Eclipse 中的菜單,然后詳細說明如何進行擴展,最后以一個實例的形式引導讀者深入理解 Eclipse 的菜單功能。

引言

Eclipse 具有豐富的菜單功能,給開發人員提供了很好的用戶體驗。總體而言,Eclipse 菜單種類包括視圖 / 編輯器菜單,主菜單(Main Menu),視圖 / 編輯器菜單(ViewPart/EditorPart Menu)和上下文菜單(Context Menu)。插件開發人員通過靈活應用這些菜單,可以給用戶提供很好的體驗。由于視圖和編輯器菜單功能類似,因此本文重點講述視圖菜單(視圖下拉菜單及其工具欄菜單),除此之外,還將講述主菜單和上下文菜單。

如圖 1 所示為 Project Explorer 視圖的菜單,包括視圖下拉菜單和工具欄菜單(折疊樹節點)。通常而言,出現在視圖工具欄的菜單都會出現在視圖的下拉菜單,也就是說,比較常用的視圖菜單放在視圖的工具欄。


圖 1. Project Explorer 視圖的菜單
圖 1. Project Explorer 視圖的菜單

如圖 2 所示為 Project Explorer 視圖中的上下文菜單,只有當我們右鍵點擊時才會出現。通常而言,出現頻率較高的菜單項才會出現在菜單中。上下文菜單具有很強的靈活項,它可以隨著我們點擊的對象不同,彈出的菜單也會有相應的變化。


圖 2. Project Explorer 視圖中的上下文菜單
圖 2. Project Explorer 視圖中的上下文菜單

如圖 3 所示為 Eclipse 的主菜單,包括最上面的主菜單項(不可移動)及其下面的工具欄菜單(可以移動,并且 Eclipse 提供了顯示 / 不顯示這些菜單的功能),Eclipse 并不建議我們為每一個插件都添加新的主菜單,這樣容易造成冗余,而且不方便用戶操作。通常,我們可以把菜單項添加到 Eclipse 已有的菜單,如插件的查找功能可以添加一個查找菜單項到 Eclipse 的 Search 主菜單上。


圖 3. Eclipse 的主菜單
圖 3. Eclipse 的主菜單

前面講到 Eclipse 的各種菜單,那么,如何在開發插件或 RCP 應用程序的時候添加這些菜單?本文下面的篇幅將詳細介紹如何擴展 Eclipse 的菜單功能,使讀者深入了解 Eclipse 的菜單功能,并能夠開發具有這些菜單的應用程序。因此,必須掌握三方面的內容:菜單種類,菜單的擴展點,菜單控制(顯示 / 隱藏或啟用 / 禁用菜單項)。下面從概念上介紹這三方面內容,下一小節將會進行詳細介紹。

菜單種類

正如前面所講到的,Eclipse 的菜單包括視圖菜單,主菜單及上下文菜單三個種類。

菜單項的擴展點

Eclipse 提供了兩種擴展點供用戶添加菜單項到相應的位置。這兩種擴展點為 org.eclipse.ui.commands(本文簡稱為 Commands 方式)和 org.eclipse.ui.actionSets(本文簡稱為 Actions 方式)。Actions 方式為界面上不同區域的表現方式提供了相應的擴展點,并且沒有分離其界面表現和內在實現。恰恰相反,Commands 方式通過三步有效的達到界面表現和內部實現的分離:首先,通過 org.eclipse.ui.commands 擴展點創建命令和類別(Category),并且可以把某些命令放在一個類別(Category)中;然后,通過 org.eclipse.ui.menus 指定命令出現在界面的哪個區域(視圖菜單 / 主菜單 / 上下文菜單);最后通過 org.eclipse.ui.handlers 指定命令的實現。因此,Eclipse 推薦新開發的插件使用 Commands 來創建您的界面菜單。當然,由于 Actions 在現有的插件中用得比較多,如果我們需要擴展或基于之前的插件開發,也需要對其進行了解。除此之外,針對上下文菜單,雖然 Commands 和 Actions 方式均可以創建上下文菜單,但是 Eclipse 還提供了另外一種創建上下文菜單的擴展點 org.eclipse.ui.popupMenus(本文簡稱為 popupMenus 方式),本文將就這三種擴展點做詳細的介紹。

菜單控制

菜單控制是一個非常常見的功能,例如,隨著選定的內容或當前窗口的不同,菜單中的菜單項會有相應的變化(顯示 / 隱藏或啟用 / 禁用菜單項),因此,如何控制菜單是插件開發人員必須掌握的知識。Eclipse 為菜單控制提供了兩種方法,一種是通過擴展點;另一種是通過 API 的方式編寫程序控制。


Eclipse 菜單功能及其擴展點

至此,我們對 Eclipse 菜單有了感觀的認識。由上一節我們可知,要深入理解 Eclipse 菜單功能,我們需要從三個方面去掌握:菜單種類,菜單的擴展點和菜單控制。下面將進行詳細講述。

菜單種類

針對各種菜單,Eclipse 提供了相應的擴展點,因此,開發人員可以通過這些擴展點把菜單放到界面的不同區域,詳細內容請參考 2.2 小節。

菜單的擴展點

視圖菜單的擴展點

采用 Commands 方式創建視圖菜單,需要引入 org.eclipse.ui.menus 擴展點;而 Actions 方式需要引入 org.eclipse.ui.actionSets.

1、視圖菜單(Commands 方式):

MenuContribution locationURI = “[Scheme]:[id]?[argument-list]”

其中,Scheme 為該菜單項出現的區域,menu 為視圖的下拉菜單,toolbar 為視圖的工具欄菜單;id 為菜單區域 ID;argument-list 為該菜單項出現在指定菜單的位置。

例如:在 ProbelmView 的下拉菜單加一個菜單項,其 MenuContribution 的 locationURI 應為:menu:org.eclipse.ui.views.ProblemView?after=additions;在 ProblemView 的工具欄菜單中加入一個菜單項,其 locationURI 應為:toolbar:org.eclipse.ui.views.ProblemView?after=additions。

2、視圖菜單(Actions 方式):

采用 Actions 方式創建菜單,需要引入 org.eclipse.ui.actionSets 擴展點,并通過設定 action 的 menubarPath 指定下拉菜單 / 菜單項出現的位置;通過設定 action 的 toolbarPath 設定工具欄菜單 / 菜單項出現的位置。

例如,添加一個下拉菜單項到 Problems 視圖中,其 menubarPath 應為:

org.eclipse.ui.views.ProblemView/additions

主菜單的擴展點

1、主菜單(Commands 方式)

通過 Commands 方式把菜單項添加到主菜單及其工具欄上,和視圖菜單一樣,也是通過擴展點 org.eclipse.ui.menus 實現,需要設定其 menuContribution 的 locationURI。

例如,添加一個菜單(菜單可以包含若干個菜單項)到主菜單一欄中,其 locationURI 為:

menu:org.eclipse.ui.main.menu?after=additions

添加一個菜單到工具欄之中,其 locationURI 為:

toolbar:org.eclipse.ui.main.toolbar?after=additions

當然,我們也可以把菜單項添加到已經存在的菜單當中,例如添加一個菜單項到 Eclipse 的 Search 主菜單當中,其 locationURI 為:

menu:org.eclipse.search.menu?dialogGroup

2、主菜單(Actions 方式)

通過 Actions 方式把菜單項添加到主菜單及其工具欄上,和視圖菜單一樣,也是通過擴展點 org.eclipse.ui.actionSets 實現,需要設定 action 的 menubarPath 和 toolbarPath 實現。

例如,添加一個菜單項到 Eclipse 的 Search 主菜單中,其 menubarPath 應為:

org.eclipse.search.menu/dialogGroup

注意:如果采用上述方式添加一個菜單項到 Search 主菜單,當我們運行時并沒有出現添加的菜單項,這時候需要換一個 workspace,其原因是 Eclipse 緩存了與其相關的某些信息在 workspace 當中。

上下文菜單的擴展點

上下文菜單除了通過 Commands 和 Actions 方式添加,還可以使用擴展點 org.eclipse.ui.popupMenus 方式添加,下面分別進行介紹。

1、上下文菜單(Commands 方式)

Commands 方式與添加視圖菜單和主菜單的方式一樣,通過設定其 menuContribution 的 locationURI 來實現。

例如,添加一個上下文菜單到 Problems 視圖中,其 locationURI 為:

popup:org.eclipse.ui.views.ProblemView?after=additions。

如果我們想讓某個上下文菜單項出現在任何區域,則可以使用下面的 locationURI:

popup:org.eclipse.ui.popup.any?after=additions

2、上下文菜單(Actions 方式)

Actions 方式沒有直接提供擴展點添加上下文菜單,但是我們可以通過編程的方式實現,如下代碼清單 1 為 TreeViewer 添加上下文菜單,通過 IMenuManager 的 add 方法添加 actions。


清單 1. 通過 Actions 方式編程實現添加上下文菜單
				private void hookContextMenu() { IMenuManager fMenuMgr = new MenuManager(“#PopupMenu”); fMenuMgr.setRemoveAllWhenShown(true); // 添加 Actions fMenuMgr.add(action … ) fMenuMgr.createContextMenu(treeViewer.getControl()); treeViewer.getControl().setMenu(fMenu); getSite().registerContextMenu(fMenuMgr, treeViewer); } 

3、上下文菜單(popupMenus 方式)

通過 popupMenus 擴展點實現上下文菜單,需要設定 objectContribution 的 objectClass 屬性把上下文菜單添加到相應的區域。

例如,如果我們想當用戶點擊 Eclipse 中的資源時,彈出的上下文菜單包括某個菜單項,我們可以設定 objectClass 屬性為:

org.eclipse.core.resources.IResource

通過 Commands 方式創建菜單項

通過 Commands 方式創建菜單項,首先需要創建 Command,通過擴展點 org.eclipse.ui.commands,然后我們可以把這個 Command 放到任何區域,上一小節已經講到,通過 org.eclipse.ui.menus 擴展點確定菜單創建的區域,最后通過擴展點 org.eclipse.ui.handlers 定義這個 command 的具體行為。

在創建 Command 時,我們可以先創建一個 Category,并把相關的一些命令放到這個 Category 中,這樣有利于管理。代碼清單 2 創建一個 Command(“Show in Glossary Explorer”),并放到一個 Category 中,然后把該 Command 放到 BGSearchResultView 視圖的上下文菜單中,最后通過擴展 org.eclipse.ui.handlers 定義該 Command 的實現類。


清單 2. 通過 Commands 方式添加菜單項
				<!-- 添加 command --> <extension point="org.eclipse.ui.commands"> <category description="Business Glossary"id="com.ibm.bg.ui.commands.category"name="%category.BusinessGlossary.name"> </category> <command categoryId="com.ibm.bg.ui.commands.category"description="Show in Glossary Explorer"id="com.ibm.bg.ui.commands.BGShowInBrowser"name="%command.ShowInGE.name"> </command> </extension> <!-- 把 Command 放到界面的對應區域 --> <extension point="org.eclipse.ui.menus"> <menuContribution locationURI= "popup:com.ibm.bg.internal.ui.search.BGSearchResultView?after=additions"> <command commandId="com.ibm.bg.ui.commands.BGShowInBrowser"style="push"tooltip="%command.ShowInGE.tooltip"> </command> </menuContribution> </extension> <!-- 定義 command 的實現類 --> <extension point="org.eclipse.ui.handlers"> <handler class="com.ibm.bg.internal.ui.handlers.BGShowInBrowser"commandId="com.ibm.bg.ui.commands.BGShowInBrowser"> </handler> </extension> 

通過 Actions 方式創建菜單項

正如前面講到,Actions 方式沒有分離界面的表現和內部實現,因此,所有這些均通過 action 來完成。如下代碼清單 3 為添加一個 Search 菜單項到 Eclipse 的 Search 主菜單(通過 action 的 menubarPath 指定)中,其中 class 對應的值為該 Action 的實現類,該類需要實現接口 IWorkbenchWindowActionDelegate。


清單 3. 通過 Actions 方式添加菜單項
				<extension point="org.eclipse.ui.actionSets"> <actionSet id="com.ibm.bg.ui.workbenchActionSet"label="%category.name.0"visible="true"> <action class="com.ibm.bg.internal.ui.handlers.BGSearchHandler"definitionId="com.ibm.bg.ui.commands.BGSearch"icon="icons/search.png"id="com.ibm.bg.ui.commands.BGSearch"label="%action.searchGlossayInMainMenu.label"menubarPath="org.eclipse.search.menu/dialogGroup"style="push"> </action> </actionSet> </extension> 

通過 popupMenus 方式創建菜單項

popupMenus 方式創建上下文菜單項也是通過 action 來實現,下面例子為添加一個菜單項到用戶右擊 IGraphicalEditPart 對象時彈出的上下文菜單,通過 menubarPath 指定該 Action 出現的區域,通過 class 指定該 action 的實現類,該類需要實現接口 IObjectActionDelegate。


清單 4. 通過 popupMenus 方式添加菜單項
				<extension point="org.eclipse.ui.popupMenus"> <objectContribution adaptable="false"id="com.ibm.bg.uml.objectContributions.BGAssignToGlossary"objectClass="org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart"> <action class="com.ibm.bg.internal.uml.actions.BGAssignToGlossary"enablesFor="+"icon="icons/assign.png"id="com.ibm.bg.internal.uml.actions.BGAssignToGlossary"label="%BGAssignToGlossary.item"menubarPath="com.ibm.bg.uml.popupMenuGroup"> </action> </objectContribution> </extension> 

菜單控制

視圖菜單的控制主要包括啟用 / 禁用,顯示 / 隱藏菜單。

通過 Command 方式創建的菜單,可以通過 org.eclipse.ui.commands 的 visibleWhen 屬性控制菜單的隱藏和顯示,通過 org.eclipse.ui.handlers 的 activewhen 或 enabledWhen 控制菜單的啟用或禁用。

通過 Actions 方式創建的菜單,可以通過 action 的 enablement 屬性控制菜單的啟用 / 禁用。

通過 popupMenus 方式創建的菜單,可以通過 objectContribution 的 visibility 和 enablement 來設置該 objectContribution 下的 action 的顯示 / 隱藏和啟用 / 禁用,我們也可以設置 action 的 enablement 來控制該菜單的啟用 / 禁用。

這里不詳細講述 enablement,visibleWhen 和 enabledWhen 的參數及如何設置,讀者可以參考第三節的例子和本文的參考文獻。


編程實踐

本文將結合前兩節講到的知識,以例子的形式說明如何創建并且控制菜單。首先創建一個視圖(Menu Example),然后分別通過 Commands,Actions 和 popupMenus 方式創建若干個菜單,并添加相應的菜單控制點。

創建 Menu Example 視圖

擴展 org.eclipse.views 創建“Menu Example”視圖,如下代碼清單 5 為創建視圖的 xml 代碼。


清單 5. 擴展 org.eclipse.ui.views 創建視圖
				<extension point="org.eclipse.ui.views"> <category id="com.free.menu.category"name="Menu Example View"> </category> <view category="com.free.menu.category"class="com.free.menu.view.MenuExplorer"id="com.free.menu.view.MenuExplorer"name="Menu Explorer"restorable="true"> </view> </extension> 

創建 Commands

采用 Command 方式創建“Menu Example”主菜單(包含 AngryCommand 和 JokeCommand 兩個菜單項),并且基于這兩個菜單項創建了 Menu Example 視圖的下拉菜單和工具欄菜單,及其 TreeViewer 的上下文菜單。

如下代碼清單 6 為擴展 org.eclipse.ui.commands 創建 Menu Example 命令和類別,并且包含兩個命令:Joke Command 和 Angry Command。


清單 6. 擴展 org.eclipse.ui.commands 創建命令
				<extension point="org.eclipse.ui.commands"> <category id="com.free.menu.category"name="Menu Example"> </category> <command categoryId="com.free.menu.category"id="com.free.menu.commands.jokeCommand"name="Joke Command"> </command> <command categoryId="com.free.menu.category"id="com.free.menu.commands.angryCommand"name="Angry Command"> </command> </extension> 

關聯 Commands 到主菜單

如下代碼清單 7 為擴展 org.eclipse.ui.menus,并基于前面創建的 Comands,添加一個主菜單 Menu Example,并且包含 Joke Command 和 Angry Command 菜單項。


清單 7. 創建 Menu Example 主菜單
				<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> <command commandId="com.free.menu.commands.angryCommand"style="push"> </command> </menu> </menuContribution> 

關聯 Commands 到視圖菜單

如下代碼清單 8 為擴展 org.eclipse.ui.menus,并基于 Commands 方式創建 Menu Example 視圖的下拉菜單,工具欄菜單和上下文菜單,通過 locationURI 來設定。Joke Command 即為下拉菜單也是工具欄菜單,只有當我們選擇了 TreeViewer 中的節點時該菜單項才是可見的,參考下面的 visibleWhen->with->iterate->or->instanceof。


清單 8. 通過 Commands 方式創建視圖菜單
				<extension point="org.eclipse.ui.menus"> <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"> <with variable="selection"> <iterate ifEmpty="true"operator="or"> <or> <instanceof value="com.free.menu.model.Person"> </instanceof>                                             </or> </iterate> </with> </visibleWhen>                 </command> </menuContribution> <menuContribution locationURI="toolbar:com.free.menu.view.MenuExplorer?after=additions"> <command commandId="com.free.menu.commands.jokeCommand"icon="icons/searchres.gif"style="push"> <visibleWhen checkEnabled="false"> <with variable="selection"> <iterate ifEmpty="true"operator="or"> <or> <instanceof value="com.free.menu.model.Person"> </instanceof>                                             </or> </iterate> </with> </visibleWhen>  </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> <command commandId="com.free.menu.commands.angryCommand"style="push"> </command>      </menuContribution> </extension> 

Commands 的實現類

如下代碼清單 9 所示擴展 org.eclipse.ui.handlers 為 Joke Command 和 Angry Command 創建事件處理類,其中 Joke Command 通過 enabledWhen 屬性控制該菜單項是否啟用,當我們同時選擇了兩個對象時 Joke Command 處于啟用狀態,否則為禁用。


清單 9. 擴展 org.eclipse.ui.handlers 為 Commands 創建實現類
				<extension point="org.eclipse.ui.handlers"> <handler class="com.free.menu.actions.JokeCommand"commandId="com.free.menu.commands.jokeCommand"> <enabledWhen> <count value="2"> </count> </enabledWhen> </handler> <handler class="com.free.menu.actions.AngryCommand"commandId="com.free.menu.commands.angryCommand"> </handler> </extension> 

創建 Action 并關聯到 Eclipse 的 Search 主菜單

采用 Actions 方式在 Eclipse 的主菜單 Search 中添加創建菜單項 SmileAction。擴展 org.eclipse.ui.actionSets 在 Eclipse 的主菜單 Search 中添加一個菜單項 Smile Action。如下代碼清單 10 所示創建該 action 并添加到 search 主菜單,只有當我們選擇至少一個對象時(設置 enablesFor 屬性為“+”),該菜單項才處于啟用狀態。


清單 10. 通過 Actions 方式創建菜單項
				<extension point="org.eclipse.ui.actionSets"> <actionSet id="com.free.menu.actionSet.MenuExample"label="Menu Example"visible="true"> <action class="com.free.menu.actions.SmileAction"enablesFor="+"icon="icons/searchres.gif"id="com.free.menu.actions.smileAction"label="Smile Action"menubarPath="org.eclipse.search.menu/dialogGroup"style="push"> </action> </actionSet> </extension> 

pupupMenus 方式創建 Action 并關聯到 IResource 資源的上下文菜單

擴展 org.eclipse.ui.popupMenus 創建菜單“Menu Example”,該菜單包含一個菜單項 HelloAction。當我們在 Eclipse 任何區域右擊 org.eclipse.core.resources.IResource 資源時彈出的上下文菜單中會出現“Menu Example”菜單。如下代碼清單 11 為創建該上下文菜單的 xml 代碼。


清單 11. popupMenus 方式創建上下文菜單
				<extension point="org.eclipse.ui.popupMenus"> <objectContribution adaptable="true"id="com.free.menu.popupMenu"objectClass="org.eclipse.core.resources.IResource"> <menu label="Menu Example"path="additions"id="com.free.menu.popupSubMenu"> <separator name="additions"> </separator> </menu> <action label="Hello Action"class="com.free.menu.popup.actions.HelloAction"menubarPath="com.free.menu.popupSubMenu/additions"enablesFor="1"id="com.free.menu.newAction"> </action> </objectContribution> </extension> 

pupupMenus 方式創建 Action 并關聯到 IResource 資源的上下文菜單

擴展 org.eclipse.ui.popupMenus 創建菜單項 GreetAction 和 CryAction,當我們右擊 Menu Example 視圖中的 TreeViewer 節點時彈出。如下代碼清單 12 所示擴展 org.eclipse.ui.popupMenus 為 Menu Example 視圖創建 GreetAction 和 CryAction 上下文菜單項。使用 visiblity 的 objectState 屬性控制菜單項的可見狀態,使用該屬性要求其選擇的對象實現了 org.eclipse.ui.IActionFilter 接口,具體可參見 Person 類的實現。


清單 12. 擴展 org.eclipse.ui.popupMenus 創建菜單
				<extension point="org.eclipse.ui.popupMenus"> <objectContribution adaptable="false"id="com.free.menu.views.popupMenu"objectClass="com.free.menu.model.Person"> <action class="com.free.menu.actions.GreetAction"enablesFor="+"id="com.free.menu.actions.greetAction"label="Greet Action"menubarPath="additions"> </action> <visibility> <objectState name="firstName"value="Dan"> </objectState> </visibility> </objectContribution> </extension> <extension point="org.eclipse.ui.popupMenus"> <objectContribution adaptable="false"id="com.free.menu.views.popupMenu2"objectClass="com.free.menu.model.Person"> <action class="com.free.menu.actions.CryAction"enablesFor="+"id="com.free.menu.actions.cryAction"label="Cry Action"menubarPath="additions"> <enablement> <objectState name="firstName"value="David"> </objectState> </enablement> </action> <visibility> <objectState name="lastName"value="Rubel"> </objectState> </visibility> </objectContribution> </extension> 

Menu Example 視圖的代碼實現類

如下代碼清單 13 所示為 Menu Example 視圖的代碼,該視圖中有一個 TreeViewer,并通過函數 hookContextMenu 把上下文菜單關聯到 TreeViewer。其中函數 viewMenuAction 用于更新菜單的狀態,它首先獲取視圖菜單,然后調用 IMenuManager 的 update 方法更新對應菜單項的狀態,從而達到控制菜單的目的。


清單 13. Menu Example 視圖代碼
				public class MenuExplorer extends ViewPart { private TreeViewer treeViewer; private MenuManager fMenuMgr; private Menu fMenu; private static MenuExplorer fInstance = null; public MenuExplorer() { fInstance = this; } public static MenuExplorer getInstance(){ return fInstance; } public void createPartControl(Composite parent) { treeViewer = new TreeViewer (parent, SWT.MULTI); treeViewer.setLabelProvider(new PersonListLabelProvider()); treeViewer.setContentProvider(new PersonTreeContentProvider()); treeViewer.setInput(Person.example()); this.getSite().setSelectionProvider(treeViewer); hookContextMenu(); fInstance = this; } public void setViewMenuActionState(boolean state){        JokeCommand.setState(state); viewMenuAction(); } private  void viewMenuAction() { IActionBars bars= getViewSite().getActionBars(); final IMenuManager menu= bars.getMenuManager();    UIOperation.asyncExecCommand(new Runnable(){ public void run() { menu.update("com.free.menu.commands.jokeAction"); }            });        } private void hookContextMenu() { fMenuMgr = new MenuManager("#PopupMenu"); fMenuMgr.setRemoveAllWhenShown(true); fMenuMgr.addMenuListener(new IMenuListener() { public void menuAboutToShow(IMenuManager manager) {                } }); fMenu = fMenuMgr.createContextMenu(treeViewer.getControl()); treeViewer.getControl().setMenu(fMenu); getSite().registerContextMenu(fMenuMgr, treeViewer);              }    public void setFocus() { treeViewer.getTree().setFocus(); } } 

Person 類的實現

如下代碼清單 14 為 Person 類的實現,用于表示 MenuExample 視圖中 TreeViewer 的一個節點,它實現了 IActionFilter 接口,通過 testAttribute 來確定是否顯示 / 隱藏菜單(其中 target 表示用戶選擇的對象,name/value 對應于 plugin.xml 文件中 objectState 的 name/value).


清單 14. Person 類實現
				public class Person implements IActionFilter { private String firstName = "John"; private String lastName = "Doe"; protected int age = 37; public Person[] children = new Person[0]; public Person parent = null; public Person(String firstName, String lastName, int age) { this.firstName = firstName; this.lastName = lastName; this.age = age; } public Person(String firstName, String lastName, int age, Person[] children) { this(firstName, lastName, age); this.children = children; for (int i = 0; i < children.length; i++) { children[i].parent = this; } } public String getFirstName() { return this.firstName; } public String getLastName() { return this.lastName; } public static Person[] example() { return new Person[] { new Person("Dan", "Rubel", 38, new Person[] { new Person("Beth", "Rubel", 8), new Person("David", "Rubel", 3) }), new Person("Eric", "Clayberg", 39, new Person[] { new Person("Lauren", "Clayberg", 6), new Person("Lee", "Clayberg", 4) }), new Person("Mike", "Taylor", 52) }; } public String toString() { return firstName + " " + lastName; } public boolean testAttribute(Object target, String name, String value) { if (target instanceof Person) { Person person = (Person) target; if (name.equals("firstName") && value.equals(person.getFirstName())) { return true; } if (name.equals("lastName") && value.equals(person.getLastName())) { return true; } } return false; } } 


總結

至此為止,已經把 Eclipse 菜單功能及其擴展點涉及到的類 / 接口 /API 進行了詳細的說明,相信讀者已經有清晰的認識了。對于前面提到 popupMenus 方式創建上下文菜單,要求選擇的對象實現 IActionFilter 接口,但是,如果開發人員正在使用 gmf 進行開發,那么我們可以不必要求選擇的對象實現 IActionFilter,我們可以通過擴展 org.eclipse.gmf.runtime.common.ui.services.action.actionFilterProviders 對菜單項進行控制,如下代碼清單 15 為擴展該 extension point 的 xml 代碼,我們可以定義多個屬性(<Attribute> … </Attribute),其中 Attribute 的 name 和 value 對應于 visibility 的 objectState 中的 name 和 value。


清單 15. 通過 actionFilterProviders 擴展點實現對菜單的控制
				<extension point="org.eclipse.gmf.runtime.common.ui.services.action.actionFilterProviders"> <ActionFilterProvider class="com.free.menu.PopupActionFilterProvider"> <Priority name="Medium"> </Priority> <Attribute name="com.ibm.bg.uml.search.isSupportedType"value="supported"> </Attribute> </ActionFilterProvider> </extension> 

如下代碼清單 16 所示 PopupActionFilterProvider 的實現,它繼承 AbstractActionFilterProvider,只需要實現其中的 testAttribute 和 provides 方法,當 testAttribute 返回 true 時,那么該菜單項被啟用,否則禁用。其中 target 對應于我們選擇的對象,name 和 value 參數對應于 visiblity 中 objectState 的 name 和 value 的指定值 ( 與前面提到的 Person 類中的 testAttribute 方法類似 )。


清單 16. actionFilterProviders 擴展點實現類
				public class PopupActionFilterProvider extends AbstractActionFilterProvider { public PopupActionFilterProvider() { } public boolean testAttribute(Object target, String name, String value) { } public boolean provides(IOperation operation) { return false; } } 

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/388126.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388126.shtml
英文地址,請注明出處:http://en.pswp.cn/news/388126.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

[翻譯 EF Core in Action 2.0] 查詢數據庫

Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的關于Entityframework Core 書籍。原版地址. 是除了官方文檔外另一個學習EF Core的不錯途徑, 書中由淺入深的講解的EF Core的相關知識。因為沒有中文版,所以本人對其進行翻譯。 預計每兩天…

hdu5692 Snacks dfs序+線段樹

題目傳送門 題目大意&#xff1a;給出一顆樹&#xff0c;根節點是0&#xff0c;有兩種操作&#xff0c;一是修改某個節點的value&#xff0c;二是查詢&#xff0c;從根節點出發&#xff0c;經過 x 節點的路徑的最大值。 思路&#xff1a;用樹狀數組寫發現還是有些麻煩&#xff…

python數據建模數據集_Python中的數據集

python數據建模數據集There are useful Python packages that allow loading publicly available datasets with just a few lines of code. In this post, we will look at 5 packages that give instant access to a range of datasets. For each package, we will look at h…

打開editor的接口討論

【打開editor的接口討論】 先來看一下workbench吧&#xff0c;workbench從靜態劃分應該大致如下&#xff1a; 從結構圖我們大致就可以猜測出來&#xff0c;workbench page作為一個IWorkbenchPart&#xff08;無論是eidtor part還是view part&#…

【三角函數】已知直角三角形的斜邊長度和一個銳角角度,求另外兩條直角邊的長度...

如圖,已知直角三角形ABC中,∠C90, ∠Aa ,ABc ,求直角邊AC、BC的長度. ∵ ∠C90,∠Aa ,ABc ,Cos∠AAC/AB ,Sin∠ABC/AB ,∴ ACAB*Cos∠Ac*Cosa ,BCAB*Sin∠Ac*Sina . 復制代碼

網絡攻防技術實驗五

2018-10-23 實驗五 學 號201521450005 中國人民公安大學 Chinese people’ public security university 網絡對抗技術 實驗報告 實驗五 綜合滲透 學生姓名 陳軍 年級 2015 區隊 五 指導教師 高見 信息技術與網絡安全學院 2018年10月23日 實驗任務總綱 2018—2019 …

usgs地震記錄如何下載_用大葉草繪制USGS地震數據

usgs地震記錄如何下載One of the many services provided by the US Geological Survey (USGS) is the monitoring and tracking of seismological events worldwide. I recently stumbled upon their earthquake datasets provided at the website below.美國地質調查局(USGS)…

Springboot 項目中 xml文件讀取yml 配置文件

2019獨角獸企業重金招聘Python工程師標準>>> 在xml文件中讀取yml文件即可&#xff0c;代碼如下&#xff1a; 現在spring-boot提倡零配置&#xff0c;但是的如果要集成老的spring的項目&#xff0c;涉及到的bean的配置。 <bean id"yamlProperties" clas…

eclipse 插件打包發布

如果想把調試好的插件打包發布&#xff0c;并且在ECLIPSE中可以使用. 1.File-->Export 2.選擇 PLug-in Development下 的 Deployable plug-ins and fragments 3.進入 Deployable plug-ins and fragments 頁面 4.把底下的 Destubatuib 的選項中選擇 Archive file 在這里添入要…

無法獲取 vmci 驅動程序版本: 句柄無效

https://jingyan.baidu.com/article/a3a3f811ea5d2a8da2eb8aa1.html 將 vmci0.present "TURE" 改為 “FALSE”; 轉載于:https://www.cnblogs.com/limanjihe/p/9868462.html

數據可視化 信息可視化_更好的數據可視化的8個技巧

數據可視化 信息可視化Ggplot is R’s premier data visualization package. Its popularity can likely be attributed to its ease of use — with just a few lines of code you are able to produce great visualizations. This is especially great for beginners who are…

分布式定時任務框架Elastic-Job的使用

為什么80%的碼農都做不了架構師&#xff1f;>>> 一、前言 Elastic-Job是一個優秀的分布式作業調度框架。 Elastic-Job是一個分布式調度解決方案&#xff0c;由兩個相互獨立的子項目Elastic-Job-Lite和Elastic-Job-Cloud組成。 Elastic-Job-Lite定位為輕量級無中心化…

Memcached和Redis

Memcached和Redis作為兩種Inmemory的key-value數據庫&#xff0c;在設計和思想方面有著很多共通的地方&#xff0c;功能和應用方面在很多場合下(作為分布式緩存服務器使用等) 也很相似&#xff0c;在這里把兩者放在一起做一下對比的介紹 基本架構和思想 首先簡單介紹一下兩者的…

第4章 springboot熱部署 4-1 SpringBoot 使用devtools進行熱部署

/imooc-springboot-starter/src/main/resources/application.properties #關閉緩存, 即時刷新 #spring.freemarker.cachefalse spring.thymeleaf.cachetrue#熱部署生效 spring.devtools.restart.enabledtrue #設置重啟的目錄,添加那個目錄的文件需要restart spring.devtools.r…

border-radius 漲知識的寫法

<div idapp></div>復制代碼#app{width:100%;height:80px;background:pink;border-radius:75%/20% 20% 0 0;}復制代碼僅供自己總結記憶轉載于:https://juejin.im/post/5c80afd66fb9a049f81a1217

ibm python db_使用IBM HR Analytics數據集中的示例的Python獨立性卡方檢驗

ibm python dbSuppose you are exploring a dataset and you want to examine if two categorical variables are dependent on each other.假設您正在探索一個數據集&#xff0c;并且想要檢查兩個分類變量是否相互依賴。 The motivation could be a better understanding of …

Oracle優化檢查表

分類檢查項目相關文件或結果狀態備注日志及文件Oracle Alert 日志bdump/udump下是否存在明顯的報警listener相關日志SQL* Net日志參數/參數文件listener.ora/tnsnames.ora操作系統操作系統版本檢查操作系統補丁節點名操作系統vmstat狀態操作系統I/O狀態操作系統進程情況操作系統…

spring分布式事務學習筆記(2)

此文已由作者夏昀授權網易云社區發布。歡迎訪問網易云社區&#xff0c;了解更多網易技術產品運營經驗。Model類如下&#xff1a;package com.xy.model1 package com.xy.model;2 3 /**4 * Created by helloworld on 2015/1/30.5 */6 public class NameQa {7 private long …

sql 左聯接 全聯接_通過了解自我聯接將您SQL技能提升到一個新的水平

sql 左聯接 全聯接The last couple of blogs that I have written have been great for beginners ( Data Concepts Without Learning To Code or Developing A Data Scientist’s Mindset). But, I would really like to push myself to create content for other members of …

如何查看linux中文件打開情況

如何查看linux中文件打開情況 前言 我們都知道&#xff0c;在linux下&#xff0c;“一切皆文件”&#xff0c;因此有時候查看文件的打開情況&#xff0c;就顯得格外重要&#xff0c;而這里有一個命令能夠在這件事上很好的幫助我們-它就是lsof。 linux下有哪些文件 在介紹lsof命…