工具欄(tool bar)增加下來選單
阿新 • • 發佈:2019-02-10
在開發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
-
<
- 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
- </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如下
- <?xmlversion="1.0"encoding="UTF-8"?>
- <?eclipseversion="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>