How to disable or enable a MMenu (not MMenuItem) in an Eclipse E4 application

ぐ巨炮叔叔 提交于 2019-12-11 14:46:58

问题


I have an Eclipse E4 application with a MMenu (in the main menu of the application and in popup menus of different parts) that contains items provided at runtime by a dynamic menu contribution.

What I want to achieve is to disable the menu element, if the menu contribution does not provide any item. Something like @CanExecute for handler classes for commands or direct menu items.


回答1:


Do you use the latest version of eclipse and you have an Application.e4xmi file?
If so, for your "Dynamic Menu Contribution", add a"Dynamic Menu Contribution"entry that points to a class with a method annotated with "@AboutToShow" that will dynamically build the menu entries and define an hanlder for each item

public class XXX {
   @Inject private EModelService modelService;
   @AboutToShow 
   public void aboutToShow(List<MMenuElement> items, ...) {

      // insert your logic here to add an entry or not...
      // maybe with a loop of some sort...
      MDirectMenuItem dynamicItem = modelService.createModelElement(MDirectMenuItem.class);
      dynamicItem.setLabel(<;abel>);
      dynamicItem.setIconURI(<icon>);
      dynamicItem.setContributorURI("platform:/plugin/<your plugin name>");
      dynamicItem.setContributionURI("bundleclass://<your plugin name>/<class handler>");
      dynamicItem.getTransientData().put(<name>, <value>); // To pass parameters to the handler

      items.add(dynamicItem);
  }

}

public class <class handler> {
   @Execute
   public void execute(MMenuItem menuItem, ...) {
      String param = (<Type>) menuItem.getTransientData().get(<name>); // Get parameter back
      // Put your logic here linked to the menu entry
   }
}

Add an"Imperative Expression"child, link it to a class with a method annotated with "@Evaluate" expression to decide to show/hide the dynamic menu, for example if the menu is empty...

@Evaluate
public boolean showXXX(...) {
   return true/false; -> display/hide the whole menu
}



回答2:


If I understand. You want the whole Dynamic Menu Contribution item disabled/grayed-out. I think @titou10 's answer will toggle the enable/disable for each item in the Dynamic Menu Contribution but not that parent item itself. I hope someone finds a better answer. I am experimenting with finding and disabling the menu item with

    MenuImpl menu = (MenuImpl) modelService.find("menuID", application.getChildren().get(0).getMainMenu());
    menu.setEnabled(checkMenuEnabled());

But that doesn't seem to work for me. The other option is to clear all of the dynamic menu items so that nothing happens when you scroll over that parent menu item as it will have no items.

    items.clear();
    if (checkMenuEnabled()) {
        Fillthemenu();
    }


来源:https://stackoverflow.com/questions/49657032/how-to-disable-or-enable-a-mmenu-not-mmenuitem-in-an-eclipse-e4-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!