`

使用Commands Framework创建菜单

阅读更多

最开始的时刻,都是使用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="&amp;XYZ Java Tools"> 
            <separator name="group1"/> 
         </menu> 
         <action
            id="com.xyz.runXYZ" 
            label="&amp;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

 

 

  • 大小: 2.8 KB
  • 大小: 28.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics