|Dynamically creating / deleting menu items in MPopupMenu doesn't work [message #937862]
||Tue, 09 October 2012 06:30
| Laura V
Registered: March 2012
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();
MDirectMenuItem menuItem = MMenuFactory.INSTANCE.createDirectMenuItem();
menuItem.setLabel("MyLabel"); // start value
final MCoreExpression coreExpression = MUiFactory.INSTANCE.createCoreExpression();
coreExpression.setCoreExpressionId("expression." + ModelIdConstants.POPUP_MENU);
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?
|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
| François POYER
Registered: December 2009
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:
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;
Powered by FUDForum
. Page generated in 0.01881 seconds