Dynamically creating / deleting menu items in MPopupMenu doesn't work [message #937862] |
Tue, 09 October 2012 06:30  |
Eclipse User |
|
|
|
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 #946590 is a reply to message #937941] |
Tue, 16 October 2012 06:15  |
Eclipse User |
|
|
|
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);
}
|
|
|
Powered by
FUDForum. Page generated in 0.03799 seconds