1. 程式人生 > >工具欄(tool bar)增加下來選單

工具欄(tool bar)增加下來選單

在開發Eclipse 外掛時, 想在工具欄中增加一個按鈕圖示是非常容易, 但是想在圖示上增加子選單就會比較麻煩, 例如想實現如下效果: 


具體步驟如下:

  1. 首先在擴充套件點org.eclipse.ui.commands中增加三個指令

    1. <extension
    2.     point="org.eclipse.ui.commands">
    3.     <command
    4.         name="Reference Command"
    5.         id="top.itart.plugin.smartboot.referenceCommand">
    6.     </command
      >
    7.     <command
    8.         name="Sub1 Command"
    9.         id="top.itart.plugin.smartboot.sub1Command">
    10.     </command>
    11.     <command
    12.         name="Sub2 Command"
    13.         id="top.itart.plugin.smartboot.sub2Command">
    14.     </command>
    15. </extension>


  2. 其次在擴充套件點org.eclipse.ui.menus 增加兩個menuContribution

    1. <
      menuContribution
    2.     locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions">
    3.     <toolbar
    4.        id="top.itart.plugin.smartboot.toolbars">
    5.         <command
    6.               commandId="top.itart.plugin.smartboot.referenceCommand"
    7.               icon="icons/sample.gif"
    8.               tooltip
      ="Pull down"style="pulldown">
    9.         </command>
    10.     </toolbar>
    11. </menuContribution>

    這個是定義顯示在toolbar上的選單. Command 中的commandId 為第一個指令的ID, 型別 type 為pulldown, 並且放在toolbar標籤中

    1. <menuContribution
    2.     locationURI="menu:top.itart.plugin.smartboot.referenceCommand">
    3.     <command
    4.               commandId="top.itart.plugin.smartboot.sub1Command"
    5.               icon="icons/sample.gif"
    6.               label = "Sub 1"
    7.               tooltip="Sub 1">
    8.     </command>
    9.     <command
    10.         commandId="top.itart.plugin.smartboot.sub2Command"
    11.         icon="icons/sample.gif"
    12.         tooltip="Sub2"
    13.         label = "Sub 2">
    14.     </command>
    15. </menuContribution>

    這個menuContribution是定義下拉的子選單. 注意locationURI的值是menu: <Toolbar上的選單ID>

  • 最終的plugin.xml如下
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <?eclipseversion="3.4"?>
  3. <plugin>
  4.     <extension
  5.         point="org.eclipse.ui.commands">
  6.         <command
  7.             name="Reference Command"
  8.             id="top.itart.plugin.smartboot.referenceCommand">
  9.         </command>
  10.         <command
  11.             name="Sub1 Command"
  12.             id="top.itart.plugin.smartboot.sub1Command">
  13.         </command>
  14.         <command
  15.             name="Sub2 Command"
  16.             id="top.itart.plugin.smartboot.sub2Command">
  17.         </command>
  18.     </extension>
  19.    <extension
  20.          point="org.eclipse.ui.menus">
  21.         <menuContribution
  22.             locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions">
  23.             <toolbar
  24.                id="top.itart.plugin.smartboot.toolbars">
  25.                 <command
  26.                       commandId="top.itart.plugin.smartboot.referenceCommand"
  27.                       icon="icons/sample.gif"
  28.                       tooltip="Pull down"style="pulldown">
  29.                 </command>
  30.             </toolbar>
  31.         </menuContribution>
  32.         <menuContribution
  33.             locationURI="menu:top.itart.plugin.smartboot.referenceCommand">
  34.             <command
  35.                       commandId="top.itart.plugin.smartboot.sub1Command"
  36.                       icon="icons/sample.gif"
  37.                       label = "Sub 1"
  38.                       tooltip="Sub 1">
  39.             </command>
  40.             <command
  41.                 commandId="top.itart.plugin.smartboot.sub2Command"
  42.                 icon="icons/sample.gif"
  43.                 tooltip="Sub2"
  44.                 label = "Sub 2">
  45.             </command>
  46.         </menuContribution>
  47.     </extension>
  48. </plugin>