繁体   English   中英

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

[英]Creating toolbar items pulldown menu-entries programmatically

我在网上搜索了所有内容,但找不到有效的解决方案,该方法如何以编程方式在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();

所有对象均有效,但它们都不包含以上定义的参数化命令之一。 我可以在命令中找到的所有参数仅包含定义,但未指定任何值。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM