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.
Have a look at the class
attribute of the menuContribution
element. Via a this you can write a Java class (extending org.eclipse.ui.menus.ExtensionContributionFactory
) that will contribute the wanted menu entries dynamically. In this case all sub-elements of the menuContribution
will be ignored.
作为提供整个ExtensionsContributionFactory
的替代方法(可以正常工作),您可以在现有XML中添加dynamic
元素,然后提供CompoundContributionItem
来创建工具项下拉列表的动态部分。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.