Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » How to programmatically add menus to the main menu bar?
How to programmatically add menus to the main menu bar? [message #1786257] Sat, 28 April 2018 00:15 Go to next message
PK Mising name is currently offline PK Mising nameFriend
Messages: 44
Registered: July 2009
Member
Hello,
I am creating parts from a PartDescriptor defined in the e4xmi file. After the creation of the part, I want to add menus under a menu called "Mine" which I have added to the "Window" in the main menu bar. The menus show up correctly under "Mine". The goal is to create menus that when clicked will take the user to the part.

However, clicking on the menu does not invoke the handler? Why is this? Basically I want all new parts to be available under a menu in the menubar. When a part is added a new menu should be added. Selecting the menu will activate the part.

The following is the code I am using:
  // Create the command
		MCommand command = (MCommand) modelService
				.findElements(app, "commandid", MCommand.class, null)
				.get(0);

		// Find the menu to which the programmatically created menu item will be added
		MMenu menu = (MMenu) modelService.find("Mine", window.getMainMenu());

		// Create the menu item
		MHandledMenuItem dynamicItem = modelService.createModelElement(MHandledMenuItem.class);
		dynamicItem.setLabel("my_id");
		dynamicItem.setContributorURI(
				"bundleclass://plugin.blah/MyHandler");

		// Create the menu parameters
		MParameter commandParam = modelService.createModelElement(MParameter.class);
		commandParam.setName("my_id");
		commandParam.setElementId("commandParam");
		commandParam.setValue("my_id");

		// Set the command parameters
		dynamicItem.getParameters().add(commandParam);

		// Set the command
		dynamicItem.setCommand(command);

		// Add the menu to the menu item
		menu.getChildren().add(dynamicItem);


I see this warning in the log:
!MESSAGE Unable to generate parameterized command for org.eclipse.e4.ui.model.application.ui.menu.impl.HandledMenuItemImpl@24068086 (elementId: null, tags: null, contributorURI: bundleclass://plugin.blah/MyHandler) (widget: null, renderer: org.eclipse.e4.ui.workbench.renderers.swt.MenuManagerRenderer@664db2ca, toBeRendered: true, onTop: false, visible: true, containerData: null, accessibilityPhrase: null) (label: My_id, iconURI: null, tooltip: null, enabled: true, selected: false, type: Push) (mnemonics: null) (wbCommand: null) with {My_id=124}

[Updated on: Sat, 28 April 2018 02:49]

Report message to a moderator

Re: How to programmatically add menus to the main menu bar? [message #1786288 is a reply to message #1786257] Sun, 29 April 2018 21:55 Go to previous messageGo to next message
ILIE STEFAN is currently offline ILIE STEFANFriend
Messages: 1
Registered: April 2018
Junior Member
I was struggling for about 1 day with the same problem. I tried to play with all sorts of controls and injection and for nothing...

In the end, I figured out that the issue is at MParameter creation

Quote:
MParameter commandParam = modelService.createModelElement(MParameter.class);
commandParam.setName("my_id");
commandParam.setElementId("commandParam");
commandParam.setValue("my_id");


1. Remove the line with "setElementId"
2. Use the param ID (as is defined in the e4xmi file) at "setName"

This worked for me!
Re: How to programmatically add menus to the main menu bar? [message #1786292 is a reply to message #1786257] Mon, 30 April 2018 02:20 Go to previous messageGo to next message
Eclipse UserFriend
It's weird, but a parameter's name in a command-reference is the command-parameter's ID in the command-definition.

// Create the menu parameters
MParameter commandParam = modelService.createModelElement(MParameter.class);
commandParam.setName("my_id");
commandParam.setElementId("commandParam");
commandParam.setValue("my_id");
Re: How to programmatically add menus to the main menu bar? [message #1786331 is a reply to message #1786292] Tue, 01 May 2018 12:17 Go to previous message
PK Mising name is currently offline PK Mising nameFriend
Messages: 44
Registered: July 2009
Member
Sorry about the mistake in the code. I made that mistake while cleaning up the code for the posting. I figured out that a HandledMenuItem has a command and a wbCommand. The wbCommand needs to be populated correctly in order for a menu to successfully execute a parametrized command. I could not successfully create a wbCommand that worked...ran into private classes and lack of simple constructors. I gave up on this idea.

Thanks for trying to help.
Previous Topic:Is it possible to choose where minimized parts are located
Next Topic:RCP App freezes in Windows while showing splash-screen
Goto Forum:
  


Current Time: Fri Apr 26 20:49:48 GMT 2024

Powered by FUDForum. Page generated in 0.03030 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top