以编程方式创建工具栏项下拉菜单项

时间:2012-02-04 01:23:55

标签: menu eclipse-plugin toolbar

我在网上搜索但是找不到一个可行的解决方案如何以编程方式在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();

所有对象都有效,但它们既不包含上述定义的参数化命令之一。我能找到的命令中的所有参数只包含定义但没有指定值。

2 个答案:

答案 0 :(得分:1)

查看class元素的menuContribution属性。通过这个,您可以编写一个Java类(扩展org.eclipse.ui.menus.ExtensionContributionFactory),它将动态地提供所需的菜单项。在这种情况下,menuContribution的所有子元素都将被忽略。

答案 1 :(得分:0)

作为提供整个ExtensionsContributionFactory(可以正常工作)的替代方法,您可以在现有XML中添加dynamic元素,然后提供CompoundContributionItem来创建动态部分你的toolitem下拉列表。

相关问题