2012-02-04 43 views
2

我找遍了網,但無法找到工作解決方案如何在Eclipse工具欄以編程方式來創建一個菜單項下拉菜單項。使用plugin.xml創建它們很流暢,但是有什麼方法可以通過代碼實現?爲什麼要這樣做?創建工具欄項下拉菜單條目編程

我想創建一個小插件,它爲用戶提供了創建這應該是通與主工具欄下拉菜單中的單個菜單項(按鈕)訪問條目的隨機數的可能性。

我是很新的Eclipse插件開發。正如我已經說過plugin.xml中這樣做是沒有問題的:

<extension point="org.eclipse.ui.menus"> 
     <menuContribution  locationURI="toolbar:org.eclipse.ui.main.toolbar?after=additions"> 
     <toolbar id="pulldown.items.toolbars.sampleToolbar"> 
      <command 
        commandId="pulldown.items.commands.sampleCommand" 
        icon="icons/sample.gif" 
        tooltip="Say hello world" 
        id="pulldown.items.toolbars.sampleCommand" 
        style="pulldown"> 
      </command> 
     </toolbar> 
     </menuContribution> 
     <menuContribution locationURI="menu:pulldown.items.toolbars.sampleCommand"> 
      <command 
       commandId="pulldown.items.commands.sampleCommand" 
       label="Message 1" style="push"> 
        <parameter name="pulldown.items.msg" value="Some message"/> 
      </command> 
      <separator name="nothing" visible="false"/> 
      <command 
       commandId="pulldown.items.commands.sampleCommand" 
       label="Message 2" style="push"> 
       <parameter name="pulldown.items.msg" value="Some other message"/> 
      </command> 
     </menuContribution> 
</extension> 

我試圖找到有關這個中的下列對象命令,但找不到任何。不要打擾我使用getWorkbenchWindows()[0]此代碼在插件啓動時執行,並且沒有可用的活動窗口。

Activator act = Activator.getDefault(); 
IWorkbench workbench = act.getWorkbench(); 
WorkbenchWindow window = (WorkbenchWindow)workbench.getWorkbenchWindows()[0]; 
CoolBarManager cbm = window.getCoolBarManager(); 
ToolBarContributionItem item =   
    (ToolBarContributionItem)cbm.find("pulldown.items.toolbars.SampleToolbar"); 
IToolBarManager tbm = item.getToolBarManager(); 
CommandContributionItem citem = 
    (CommandContributionItem)tbm.find("pulldown.items.toolbars.sampleCommand"); 
ParameterizedCommand cmd = citem.getCommand(); 

所有對象都有效,但它們都不包含上述定義的參數化命令之一。我能找到的命令中的所有參數都只包含定義,但沒有指定值。

+0

對不起,weired代碼,這只是調試和瀏覽通過這個所有對象,以查找上述我。 – jdc 2012-02-04 01:25:46

回答

1

看一看在menuContribution元素的class屬性。通過這個,您可以編寫一個Java類(擴展爲org.eclipse.ui.menus.ExtensionContributionFactory),它將動態地提供所需的菜單條目。在這種情況下,menuContribution的所有子元素都將被忽略。

0

作爲替代提供一個完整的ExtensionsContributionFactory(這將正常工作),你可以在你現有的XML添加dynamic元素,然後提供一個​​創建您toolitem下拉菜單的動態部分。