Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Dynamically creating / deleting menu items in MPopupMenu doesn't work
Dynamically creating / deleting menu items in MPopupMenu doesn't work [message #937862] Tue, 09 October 2012 10:30 Go to next message
Laura V is currently offline Laura VFriend
Messages: 32
Registered: March 2012
Member
Dear Sirs
we have a context menu in our e4 application that has to be different in every cell of a nebula grid tree viewer.
The menu won't be defined in the fragment as we would normally do, but is defined programmatically as follows:

MPopupMenu contextMenu = MMenuFactory.INSTANCE.createPopupMenu();
contextMenu.setElementId(ModelIdConstants.POPUP_MENU);

MDirectMenuItem menuItem = MMenuFactory.INSTANCE.createDirectMenuItem();
menuItem.setLabel("MyLabel"); // start value
menuItem.setContributionURI(CONTRIBUTION_URI);

final MCoreExpression coreExpression = MUiFactory.INSTANCE.createCoreExpression();
coreExpression.setCoreExpressionId("expression." + ModelIdConstants.POPUP_MENU);
menuItem.setVisibleWhen(coreExpression);
contextMenu.getChildren().add(menuItem);
part.getMenus().add(contextMenu);

On the cell we have a mouse listener that on right click looks at which cell we are in, and dynamically changes the menu.
That means, we go through the contextMenu.getChildren() and find the existing MDirectMenuItem then assigns the right label / handler to the menu item.
This works. But I'm not happy with this solution...the problem is, we will have menus with more menu items and some with less, so we tried to delete the context menu children, then dynamically create a new MDirectMenuItem and put it in the context menu children, but this didn't work. An empty menu is displayed. Is this a bug?

Thanks

Laura
Re: Dynamically creating / deleting menu items in MPopupMenu doesn't work [message #937877 is a reply to message #937862] Tue, 09 October 2012 10:41 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Am 09.10.12 12:30, schrieb Laura V:
> Dear Sirs
> we have a context menu in our e4 application that has to be different in
> every cell of a nebula grid tree viewer.
> The menu won't be defined in the fragment as we would normally do, but
> is defined programmatically as follows:
>
> MPopupMenu contextMenu = MMenuFactory.INSTANCE.createPopupMenu();
> contextMenu.setElementId(ModelIdConstants.POPUP_MENU);
>
> MDirectMenuItem menuItem =
> MMenuFactory.INSTANCE.createDirectMenuItem();
> menuItem.setLabel("MyLabel"); // start value
> menuItem.setContributionURI(CONTRIBUTION_URI);
>
> final MCoreExpression coreExpression =
> MUiFactory.INSTANCE.createCoreExpression();
> coreExpression.setCoreExpressionId("expression." +
> ModelIdConstants.POPUP_MENU);
> menuItem.setVisibleWhen(coreExpression);
> contextMenu.getChildren().add(menuItem);
> part.getMenus().add(contextMenu);
>
> On the cell we have a mouse listener that on right click looks at which
> cell we are in, and dynamically changes the menu. That means, we go
> through the contextMenu.getChildren() and find the existing
> MDirectMenuItem then assigns the right label / handler to the menu item.
> This works. But I'm not happy with this solution...the problem is, we
> will have menus with more menu items and some with less, so we tried to
> delete the context menu children, then dynamically create a new
> MDirectMenuItem and put it in the context menu children, but this didn't
> work. An empty menu is displayed. Is this a bug?
>

Yes.

Tom

> Thanks
>
> Laura
>
Re: Dynamically creating / deleting menu items in MPopupMenu doesn't work [message #937941 is a reply to message #937877] Tue, 09 October 2012 11:55 Go to previous messageGo to next message
Laura V is currently offline Laura VFriend
Messages: 32
Registered: March 2012
Member
OK thanks. Filed in the bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=391430
Laura
Re: Dynamically creating / deleting menu items in MPopupMenu doesn't work [message #946590 is a reply to message #937941] Tue, 16 October 2012 10:15 Go to previous message
François POYER is currently offline François POYERFriend
Messages: 6
Registered: December 2009
Junior Member
Not sure if that solves your exact problem, but you can use the "setVisible" and "setToBeRendered" methods to change what entries should show or not in your contextual menu (calling these methods from a selection listener or the like). This only works if you have a definite number of entries that can appear in your menu and you defined these entries in your e4 model.
If your really need to go fully programmatic (for example if your entries are read at runtime from a user-provided file or whatever prevents you from using a static e4xmi file), then you can try to force the renderer to read the menu again with this:
IEclipseContext context;
IRendererFactory rendererFactory = context.get(IRendererFactory.class);
AbstractPartRenderer menuMngrRenderer = rendererFactory.getRenderer(popupMenu, null);
{
    // ugly unsafe cast through Object, but no other choice (it is done the same in e4 code)
    Object castObject = menuToRender;
    menuMngrRenderer.processContents((MElementContainer<MUIElement>) castObject);
}

Previous Topic:Views not loaded automatically
Next Topic:Questions regarding @Creatable
Goto Forum:
  


Current Time: Tue Apr 16 14:33:00 GMT 2024

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

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

Back to the top