最开始的时刻,都是使用org.eclipse.ui.popupMenus (上下文菜单)和org.eclipse.ui.actionSets(视图菜单,主菜单)来创建Eclipse菜单。
原来都是使用下面的方式来创建菜单:
<extension point="org.eclipse.ui.popupMenus"> <objectContribution id="com.xyz.C1" objectClass="org.eclipse.core.resources.IFile" nameFilter="*.java"> <menu id="com.xyz.xyzMenu" path="additions" label="&XYZ Java Tools"> <separator name="group1"/> </menu> <action id="com.xyz.runXYZ" label="&Run XYZ Tool" style="push" menubarPath="com.xyz.xyzMenu/group1" icon="icons/runXYZ.gif" helpContextId="com.xyz.run_action_context" class="com.xyz.actions.XYZToolActionDelegate" enablesFor="1" /> </objectContribution>
同样Eclipse也提供了Commands Framework框架来创建菜单,使用CommandsFramework需要用到3个扩展点:
org.eclipse.ui.commands
org.eclipse.ui.handlers
org.eclipse.ui.menus
org.eclipse.ui.commands
命令定义。
· id 该command的唯一标识,在handler和menu都会用到这个id。
· name 该命令的名字,可以起到一个翻译解释的作用。如果在menu的label属性没有定义时,在界面上会显示该name的值。
· defaultHandler 默认处理类,如果该commandId没有被绑定到handler扩展点,那么就会调用该defaultHandler处理类(这个属性还与IExecutableExtension有联系)。
<extension point="org.eclipse.ui.commands"> <command defaultHandler="com.winse.eclipse.examples.handler.HelloHandler" id="com.winse.eclipse.examples.command.hellocmdusedefault" name="Hello CMD use DefaultHandler"> </command> <command id="com.winse.eclipse.examples.command.hellocmd" name="Hello CMD"> </command> </extension>
当menus扩展点中没有定义label属性时,会在界面中显示command的name属性。
org.eclipse.ui.handlers
这个用于设置处理类。需要处理的对象可以通过HandleUtil获得。
· commandId 绑定到的command,既上面的org.eclipse.ui.commands扩展点设置的id。
· class 处理类
<extension point="org.eclipse.ui.handlers"> <handler class="com.winse.eclipse.examples.handler.HelloHandler" commandId="com.winse.eclipse.examples.command.hellocmd"> </handler> </extension>
public class HelloHandler extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { MessageDialog.openInformation( HandlerUtil.getActiveWorkbenchWindow(event).getShell(), "Hello.", "my first eclise command framework example."); return null; } }
org.eclipse.ui.menus
定义命令出现的位置。
· locationURI 定义menu节点的插入点。这个重点难点,这个参数请参考最后的链接。
· id 菜单(menu或command)的标识,可以用于locationURI的after与before来进行菜单的排序。
· label 菜单的显示名称(可选,如果没有填写,会使用命令的name属性,但推荐在这里定义)。
· commandId 需要执行的命令Id。
<extension point="org.eclipse.ui.menus"> <menuContribution allPopups="false" locationURI="menu:org.eclipse.ui.main.menu"> <menu id="fileMenu" label="file"> <command commandId="com.winse.eclipse.examples.command.hellocmd" label="Hello" style="push"> </command> <command commandId="com.winse.eclipse.examples.command.hellocmdusedefault" id="hellodefault" style="push"> </command> </menu> </menuContribution> </extension>
完成上面三个扩展点的定义后,就可以来进行测试了。
我们的命令放置在主菜单栏上(menu:org.eclipse.ui.main.menu),所以,我们可以看到如图效果:
传统的actionSets, editorAction这些扩展点的方式与命令框架的CommandFramework之间其实没有太多的比较可言了。但CommandFramework提供了菜单创建的统一方式locationURI,不需要去学习很多扩展点的使用;同时,我们Command可以复用,我们甚至可以无需知道命令是怎么实现的,例如退出eclipse的exit命令,我们只需要定义一个menus,然后复用这个command即可(我觉得这个是它最大的优势);还有就是可以很方便的绑定快捷键bindings。
<menuContribution locationURI="menu:org.eclipse.ui.main.menu"> <menu label="File"> <command commandId="org.eclipse.ui.file.exit" label="Exit"> </command> </menu> </menuContribution>
参考资料:
1 研读 http://wiki.eclipse.org/Menu_Contributions#Menu_URIs 一文,从中获取如何使用 menuContribution, locationURI 。
2 阅读 http://wiki.eclipse.org/Command_Core_Expressions 一文,理解 Eclipse Command 表达式的用途。
3 深入理解菜单(Menu)功能及其扩展点 http://www.ibm.com/developerworks/cn/opensource/os-cn-ecl-menuext/index.html
4 http://www.vogella.com/articles/EclipseCommandsAdvanced/article.html
5 http://www.vogella.com/articles/EclipseCommands/article.html
相关推荐
Eclipse Commands Framework教程,作者:Lars Vogel,司马先生(本人)亲自翻译。文章共两篇,详细介绍eclipse的command框架。标题如下: 1. Eclipse Commands Tutorial 2. Eclipse Commands Advanced 这是第一篇
Laravel开发-commands 这是为帮助开发工作中的每个人而创建的Laravel Artisan命令集合。
Linux Commands
ZPL II Commands for RFID
FreeBSD Commands It is useful!
Unix shell commands,Unix shell commands
Oracle DBA commands
此标准规定了SCSI Block Commands - 3(SBC-3)命令集的功能要求.SBC-3允许SCSI块逻辑单元(如刚性磁盘)连接到计算机并提供其使用的定义。 该标准与SCSI Block Commands(SBC-2)命令集INCITS 405-2005保持高度兼容...
使用想服务器上传jsp执行windows或者linux命令,来获取服务器的资源情况。 Commands with JSP.sjp
db2 commands to use when operate db2 database.zip
Trusted Platform Module Library Part 3: Commands This TPM 2.0 Part 3 of the Trusted Platform Module Library specification contains the definitions of the TPM commands. These commands make use of the ...
Kingkong AT Commands Spec V1.5
commom commands of latex
SCSI Block Commands – 3
ansys commands reference,学习ansys必备,可以熟练运用
语言:English 单击任何元素以获取xPath 该xPath 2插件由Tomasz Rembacz的xPath Finder插件派生而来,具有一些路径增强功能和Robot框架命令生成器支持。 单击任何元素以获取xPath插件以获取元素xPath。...
SCSI Primary Commands - 4 (SPC-4) spc4r25 This is an internal working document of T10, a Technical Committee of Accredited Standards Committee INCITS (InterNational Committee for Information ...
UNIX Commands for Beginners,向学习linux的同学下载学习。
SCSI Primary Commands - 4
摩托罗拉G24 GSM模块AT命令手册 This manual is intended for software developers who communicate with the G24 ...AT commands, and create applications to communicate with the G24 device using the AT commands