以编程方式创建工具栏项下拉菜单项
我在网上进行了搜索,但找不到一个有效的解决方案,如何以编程方式为 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();
所有对象都是有效的,但它们都不包含上述定义的参数化命令之一。我能找到的命令中的所有参数仅包含定义,但没有指定值。
I searched all over the net but couldn't find a working solution how to create pull-down menu entries for a menu item in a toolbar in Eclipse programmatically. To create them using plugin.xml
is smooth, but is there some way to do it from code? Why to do that?
I want to create a little plugin which offers the user the possibility to create a random number of entries which should be accessible thru a single menu item (button) with a pull-down menu in the main toolbar.
I'm quite new to Eclipse plugin development. As I already said doing in plugin.xml is no problem :
<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>
I tried to find the information about this commands in the following objects but couldn't find any. Don't bother me using getWorkbenchWindows()[0]
this code is executed on plugin startup and there is no active window available.
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();
All objects are valid but they contain neither one of the above defined parameterized commands. All parameters in the commands I could find are only containing the definition but no value is specified.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看
menuContribution
元素的class
属性。通过 this,您可以编写一个 Java 类(扩展 org.eclipse.ui.menus.ExtensionContributionFactory),该类将动态提供所需的菜单条目。在这种情况下,menuContribution
的所有子元素都将被忽略。Have a look at the
class
attribute of themenuContribution
element. Via a this you can write a Java class (extendingorg.eclipse.ui.menus.ExtensionContributionFactory
) that will contribute the wanted menu entries dynamically. In this case all sub-elements of themenuContribution
will be ignored.作为提供整个
ExtensionsContributionFactory
(可以正常工作)的替代方案,您可以在现有 XML 中添加dynamic
元素,然后提供CompoundContributionItem
> 创建工具项下拉列表的动态部分。As an alternative to providing an entire
ExtensionsContributionFactory
(which would work fine), you could add thedynamic
element in your existing XML and then supply aCompoundContributionItem
to create the dynamic part of your toolitem dropdown.