问题
This question stems from How to disable or enable a MMenu (not MMenuItem) in an Eclipse E4 application
I have been attempting to grey-out/disable an entire Dynamic Menu Contribution in Eclipse 4 when a condition is met in the application. The Dynamic Menu Contribution is itself in the File Menu Model Element. My workaround has been to remove all options so the menu does not show anything, but is still active (not-grey) when the condition is met with the code below for clearing the menu.
items.clear();
if (checkMenuEnabled()) {
Fillthemenu();
}
This code below doesn't seem to disable the dynamic menu contribution like I want it to.
MenuImpl menu = (MenuImpl) modelService.find("menuID", application.getChildren().get(0).getMainMenu());
menu.setEnabled(checkMenuEnabled());
Here is an image of the model xmi UI items. The File->Submenu is what I am trying to grey out. Not the individual Dynamic Menu Contribution Items.
Model XMI
Thanks
回答1:
So in your e4xmi file, you have a "Menu" with a "Dynamic Menu Contribution" and you want to gray out some items in the menu on some application condition, right?
The "Dynamic Menu Contribution" is attached to some "class", right?
In this class, when you generate a disabled "menu":
public class <the class referenced in e4xml> {
@Inject private EModelService modelService;
@AboutToShow
public void aboutToShow(List<MMenuElement> items, {...}) {
MDirectMenuItem dynamicItem = modelService.createModelElement(MDirectMenuItem.class);
dynamicItem.setLabel(<some label>);
dynamicItem.setIconURI(<some icon URI>);
dynamicItem.setContributorURI("platform:/plugin/platform:/plugin/<nom plugin>");
dynamicItem.setContributionURI(<menu item handler> "bundleclass://<plugin name>/<menu item handler class>");
--> dynamicItem.setEnabled(true/false); to enable/grey out the menu
--> dynamicItem.setvisible(true/false); to show/hide the menu
// add one or many MDirectMenuItems ...
items.add(dynamicItem);
}
}
In the menu item handler ("setContributionURI" class) where you implement the logic of the menu item, you can also show/hide/enable/disable the menu item:
public class <menu item handler class> {
@Execute
public void execute({...}) {
<code linked to the menu item selection here>
}
@CanExecute
public boolean canExecute(@Optional MMenuItem menuItem, {...}) {
// implement the logic to show/hide, enable/disable the menu item
menuItem.setVisible(true/false); // show/hide the menu item
return true/false; // enable/grey out the menu item
}
}
来源:https://stackoverflow.com/questions/55032850/how-to-disable-a-dynamic-menu-contribution-parent-in-eclipse-4-rcp-application