2012-02-04 94 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下拉菜单的动态部分。