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.mzph.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命…