Skip to content
qzw edited this page May 25, 2014 · 10 revisions
  • 这是一篇我个人对Menu_Tool Bar理解之后所写的文档
  • 在看代码之前我们先简单了解三个概念:menu/tool commands handle
  • menu/tool:可以直观的理解为eclipse Bar当中的某一个图标按钮(eg:file)
  • commands:描述这个按钮要实现什么功能
  • handle方法:是按钮实现其功能的具体代码

  • menu
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="menu:org.eclipse.ui.examples.contributions.view?after=additions">
         <command
               commandId="org.eclipse.ui.examples.contributions.view.count"
               mnemonic="%contributions.view.count.mnemonic">
         </command>
         <command
               commandId="org.eclipse.ui.examples.contributions.view.edit"
               mnemonic="%contributions.view.edit.mnemonic">
         </command>
         <command
               commandId="org.eclipse.ui.file.refresh"
               mnemonic="%contributions.view.refresh.mnemonic">
         </command>
      </menuContribution>
…

Here we are adding 3 commands to the InfoView dropdown menu: Count, Edit, and Refresh. 上段代码当中涉及到两个概念locationURI* , commandId locationURL:menu将要存放的位置 commandId:标记了某一个特定的menu


  • commands
<extension
	point="org.eclipse.ui.commands">
	...
      <command
            categoryId="org.eclipse.ui.examples.contributions.commands.category"
            id="org.eclipse.ui.examples.contributions.view.count"
            description="%contributions.view.count.desc"
            name="%contributions.view.count.name">
      </command>
	…

The command definition specifies a name, description, and id for the behaviour. It also specifies the id of a category for the command, which is used to group commands in the preferences dialog.


  • handler方法
public class SampleHandler extends AbstractHandler {
	/**
	 * The constructor.
	 */
	public SampleHandler() {
	}

	/**
	 * the command has been executed, so extract extract the needed information
	 * from the application context.
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
		MessageDialog.openInformation(
				window.getShell(),
				"Hello",
				"Hello, Eclipse world");
		return null;
	}
}
  • A handler must implement org.eclipse.core.commands.IHandler although in most cases it is easier to subclass org.eclipse.core.commands.AbstractHandler.
  • AbstractHandler抽象类可以通过eclipse查看其declaration

  • 可以参照menu来看下面这段tool代码
<menuContribution
            locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions">
         <toolbar
               id="com.packtpub.e4.hello.ui.toolbars.sampleToolbar">
            <command
                  commandId="com.packtpub.e4.hello.ui.commands.sampleCommand"
                  icon="icons/sample.gif"
                  tooltip="Say hello world"
                  id="com.packtpub.e4.hello.ui.toolbars.sampleCommand">
            </command>
         </toolbar>
</menuContribution>

  • 总结:以上全部内容可通过以下方式进行深入了解
    • eclipse help 文档
    • 自己创建eclipse项目,看其代码
Clone this wiki locally