Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Cannot replace menu items at runtime
Cannot replace menu items at runtime [message #909161] Thu, 06 September 2012 12:44 Go to next message
Markus Wiederkehr is currently offline Markus Wiederkehr
Messages: 17
Registered: August 2012
Junior Member
I managed to add a few menu items to my application's main menu from within a processor. (What I want to do is implement a recently used files list in the file menu.)

So when the application starts up the file menu gets populated as it should. But I fail to figure out how to replace elements in that list later on. My suspicion is that the menu no longer gets updated after it has been initially created from the model.

What I mean is that I can remove elements from the MMenu and add new ones to replace them. It looks like the MMenu object hierarchy is okay but the newly created menu items just don't show up in the real (rendered) menu.

Do I have to trigger that update somehow?

Here's my code:
private void updateMenuItems() {
	List<MMenuElement> elements = findRecentMenuElements();
	elements.clear();

	int index = 0;
	for (ProfileInfo profileInfo : recentProfileList) {
		String label = buildLabel(index++, profileInfo.shortName);
		addOpenRecentItem(elements, label, profileInfo.id);
	}

	// debug output seem to be okay
	for (MMenuElement element : fileMenu.getChildren()) {
		System.out.println(element);
	}
	System.out.println();
}

private void addOpenRecentItem(List<MMenuElement> elements, String label, String profileId) {
	MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
	menuItem.setLabel(label);
	menuItem.setCommand(openRecentCommand);

	MParameter parameter = MCommandsFactory.INSTANCE.createParameter();
	parameter.setName(OpenRecentHandler.PARAM_ID_PROFILE_ID);
	parameter.setValue(profileId);
	menuItem.getParameters().add(parameter);

	elements.add(menuItem);
}
Re: Cannot replace menu items at runtime [message #909545 is a reply to message #909161] Fri, 07 September 2012 06:00 Go to previous messageGo to next message
Marco Descher is currently offline Marco Descher
Messages: 79
Registered: October 2010
Location: Austria
Member
Hey there, take a look at http://www.eclipse.org/forums/index.php/t/358388/ seems your problem is answered there!
Re: Cannot replace menu items at runtime [message #909599 is a reply to message #909545] Fri, 07 September 2012 07:49 Go to previous messageGo to next message
Markus Wiederkehr is currently offline Markus Wiederkehr
Messages: 17
Registered: August 2012
Junior Member
Hi Marco, thanks, I have already seen that thread but I don't think it helps with my problem. In my understanding the thread is about menu items not being saved to the workspace after they have been removed by the handler's cleanup method.

My problem is about menu items not being affected when I modify the corresponding MMenu hierarchy in the workbench model at runtime.

Perhaps the following is related to the problem: open the Application Model Editor in your Eclipse IDE (Shift-Alt-F9) and try to manipulate the model. For example when you change the orientation of a PartSashContainer from horizontal to vertical you immediately see the change taking effect. But when you try to change properties of the main menu the changes have no apparent effect.

It looks like something else is needed to let the framework know that the workbench model has changed and the menus have to be rendered again.
Re: Cannot replace menu items at runtime [message #909606 is a reply to message #909599] Fri, 07 September 2012 08:00 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5293
Registered: July 2009
Senior Member
There should not - if this is not working it is a bug. The bad thing
about the menu and toolbar stuff is that the renderers are overly
complex because the compat story got mixed in there.

If you are in a pure e4 app you could maybe simply replace the renderers
for the menus through home grown ones. Should not be very hard for them.
I've just written a JavaFX-Renderer and Menus are the simplest of all :-)

Tom

Am 07.09.12 13:49, schrieb Markus Wiederkehr:
> Hi Marco, thanks, I have already seen that thread but I don't think it
> helps with my problem. In my understanding the thread is about menu
> items not being saved to the workspace after they have been removed by
> the handler's cleanup method.
>
> My problem is about menu items not being affected when I modify the
> corresponding MMenu hierarchy in the workbench model at runtime.
>
> Perhaps the following is related to the problem: open the Application
> Model Editor in your Eclipse IDE (Shift-Alt-F9) and try to manipulate
> the model. For example when you change the orientation of a
> PartSashContainer from horizontal to vertical you immediately see the
> change taking effect. But when you try to change properties of the main
> menu the changes have no apparent effect.
>
> It looks like something else is needed to let the framework know that
> the workbench model has changed and the menus have to be rendered again.
Re: Cannot replace menu items at runtime [message #909674 is a reply to message #909161] Fri, 07 September 2012 10:51 Go to previous messageGo to next message
Joseph Carroll is currently offline Joseph Carroll
Messages: 174
Registered: May 2012
Location: Milwaukee, WI
Senior Member

Have you tried inspecting your running application model?
(Include 'org.eclipse.e4.tools.emf.liveeditor' in your launch config and its associated dependencies)

That'll atleast give you a little insight into what's there. Also, maybe instead of replacing the menu items, you replace their content. You could keep a separate stack of "most recent" files and just push new ones on to the stack.

When something new is pushed onto the stack, it triggers an update of the associated menu items. Thus, the menu items never change only their name and location property. You would even be able to keep the same handlers that just read the location.

Hopefully some of that helps.

Take care,

JD


helps if i include the plugin name Wink

[Updated on: Fri, 07 September 2012 10:52]

Report message to a moderator

Re: Cannot replace menu items at runtime [message #909811 is a reply to message #909606] Fri, 07 September 2012 17:00 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5293
Registered: July 2009
Senior Member
Can you show the code you use to add items at runtime? Could it be that
you simply forgot to set toBeRendered?

Tom

Am 07.09.12 14:00, schrieb Tom Schindl:
> There should not - if this is not working it is a bug. The bad thing
> about the menu and toolbar stuff is that the renderers are overly
> complex because the compat story got mixed in there.
>
> If you are in a pure e4 app you could maybe simply replace the renderers
> for the menus through home grown ones. Should not be very hard for them.
> I've just written a JavaFX-Renderer and Menus are the simplest of all :-)
>
> Tom
>
> Am 07.09.12 13:49, schrieb Markus Wiederkehr:
>> Hi Marco, thanks, I have already seen that thread but I don't think it
>> helps with my problem. In my understanding the thread is about menu
>> items not being saved to the workspace after they have been removed by
>> the handler's cleanup method.
>>
>> My problem is about menu items not being affected when I modify the
>> corresponding MMenu hierarchy in the workbench model at runtime.
>>
>> Perhaps the following is related to the problem: open the Application
>> Model Editor in your Eclipse IDE (Shift-Alt-F9) and try to manipulate
>> the model. For example when you change the orientation of a
>> PartSashContainer from horizontal to vertical you immediately see the
>> change taking effect. But when you try to change properties of the main
>> menu the changes have no apparent effect.
>>
>> It looks like something else is needed to let the framework know that
>> the workbench model has changed and the menus have to be rendered again.
>
Re: Cannot replace menu items at runtime [message #910446 is a reply to message #909606] Sun, 09 September 2012 13:50 Go to previous messageGo to next message
Sopot Cela is currently offline Sopot Cela
Messages: 597
Registered: December 2010
Senior Member

Thomas Schindl wrote on Fri, 07 September 2012 14:00
There should not - if this is not working it is a bug. The bad thing
about the menu and toolbar stuff is that the renderers are overly
complex because the compat story got mixed in there.

If you are in a pure e4 app you could maybe simply replace the renderers
for the menus through home grown ones. Should not be very hard for them.
I've just written a JavaFX-Renderer and Menus are the simplest of all Smile

Tom

Am 07.09.12 13:49, schrieb Markus Wiederkehr:
> Hi Marco, thanks, I have already seen that thread but I don't think it
> helps with my problem. In my understanding the thread is about menu
> items not being saved to the workspace after they have been removed by
> the handler's cleanup method.
>
> My problem is about menu items not being affected when I modify the
> corresponding MMenu hierarchy in the workbench model at runtime.
>
> Perhaps the following is related to the problem: open the Application
> Model Editor in your Eclipse IDE (Shift-Alt-F9) and try to manipulate
> the model. For example when you change the orientation of a
> PartSashContainer from horizontal to vertical you immediately see the
> change taking effect. But when you try to change properties of the main
> menu the changes have no apparent effect.
>
> It looks like something else is needed to let the framework know that
> the workbench model has changed and the menus have to be rendered again.


Yeah I got really shocked to see the menu renderers complexity. Don't know if to blame SWT or 3x legacy.

Re: Cannot replace menu items at runtime [message #910681 is a reply to message #909811] Mon, 10 September 2012 03:58 Go to previous message
Markus Wiederkehr is currently offline Markus Wiederkehr
Messages: 17
Registered: August 2012
Junior Member
Code is included in my original posting.

menuItem.setToBeRendered(true) does not make a difference, true seems to be the default value for toBeRendered anyway. (UIElementImpl.TO_BE_RENDERED_EDEFAULT == true)
Previous Topic:org.eclipse.ui.views.properties error
Next Topic:Loose coupling of objects with E4
Goto Forum:
  


Current Time: Thu Aug 28 09:15:25 EDT 2014

Powered by FUDForum. Page generated in 0.05878 seconds