在开发Eclipse 插件时, 想在工具栏中增加一个按钮图标是非常容易, 但是想在图标上增加子菜单就会比较麻烦, 例如想实现如下效果:
具体步骤如下:
- 首先在扩展点org.eclipse.ui.commands中增加三个指令
- <extension
- point="org.eclipse.ui.commands">
- <command
- name="Reference Command"
- id="top.itart.plugin.smartboot.referenceCommand">
- </command>
- <command
- name="Sub1 Command"
- id="top.itart.plugin.smartboot.sub1Command">
- </command>
- <command
- name="Sub2 Command"
- id="top.itart.plugin.smartboot.sub2Command">
- </command>
- </extension>
- 其次在扩展点org.eclipse.ui.menus 增加两个menuContribution
- <menuContribution
- locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions">
- <toolbar
- id="top.itart.plugin.smartboot.toolbars">
- <command
- commandId="top.itart.plugin.smartboot.referenceCommand"
- icon="icons/sample.gif"
- tooltip="Pull down" style="pulldown">
- </command>
- </toolbar>
- </menuContribution>
这个是定义显示在toolbar上的菜单. Command 中的commandId 为第一个指令的ID, 类型 type 为pulldown, 并且放在toolbar标签中
- <menuContribution
- locationURI="menu:top.itart.plugin.smartboot.referenceCommand">
- <command
- commandId="top.itart.plugin.smartboot.sub1Command"
- icon="icons/sample.gif"
- label = "Sub 1"
- tooltip="Sub 1">
- </command>
- <command
- commandId="top.itart.plugin.smartboot.sub2Command"
- icon="icons/sample.gif"
- tooltip="Sub2"
- label = "Sub 2">
- </command>
- </menuContribution>
这个menuContribution是定义下拉的子菜单. 注意locationURI的值是menu: <Toolbar上的菜单ID>
- 最终的plugin.xml如下
- <?xml version="1.0" encoding="UTF-8"?>
- <?eclipse version="3.4"?>
- <plugin>
- <extension
- point="org.eclipse.ui.commands">
- <command
- name="Reference Command"
- id="top.itart.plugin.smartboot.referenceCommand">
- </command>
- <command
- name="Sub1 Command"
- id="top.itart.plugin.smartboot.sub1Command">
- </command>
- <command
- name="Sub2 Command"
- id="top.itart.plugin.smartboot.sub2Command">
- </command>
- </extension>
- <extension
- point="org.eclipse.ui.menus">
- <menuContribution
- locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions">
- <toolbar
- id="top.itart.plugin.smartboot.toolbars">
- <command
- commandId="top.itart.plugin.smartboot.referenceCommand"
- icon="icons/sample.gif"
- tooltip="Pull down" style="pulldown">
- </command>
- </toolbar>
- </menuContribution>
- <menuContribution
- locationURI="menu:top.itart.plugin.smartboot.referenceCommand">
- <command
- commandId="top.itart.plugin.smartboot.sub1Command"
- icon="icons/sample.gif"
- label = "Sub 1"
- tooltip="Sub 1">
- </command>
- <command
- commandId="top.itart.plugin.smartboot.sub2Command"
- icon="icons/sample.gif"
- tooltip="Sub2"
- label = "Sub 2">
- </command>
- </menuContribution>
- </extension>
- </plugin>