如何在Eclipse E4应用程序中禁用或启用MMenu(而不是MMenuItem)

时间:2023-01-20 18:42:02

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.

我有一个带有MMenu的Eclipse E4应用程序(在应用程序的主菜单和不同部分的弹出菜单中),其中包含动态菜单贡献在运行时提供的项目。

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.

我想要实现的是禁用菜单元素,如果菜单贡献没有提供任何项目。像@CanExecute这样的命令或直接菜单项的处理程序类。

1 个解决方案

#1


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

您是否使用最新版本的eclipse并且有一个Application.e4xmi文件?如果是这样,对于“动态菜单贡献”,添加一个“动态菜单贡献”条目,该条目指向一个带有“@AboutToShow”注释方法的类,该方法将动态构建菜单条目并为每个项目定义一个hanlder

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...

添加一个“Imperative Expression”子项,使用带有“@Evaluate”表达式注释的方法将其链接到一个类,以决定显示/隐藏动态菜单,例如,如果菜单为空...

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

#1


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

您是否使用最新版本的eclipse并且有一个Application.e4xmi文件?如果是这样,对于“动态菜单贡献”,添加一个“动态菜单贡献”条目,该条目指向一个带有“@AboutToShow”注释方法的类,该方法将动态构建菜单条目并为每个项目定义一个hanlder

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...

添加一个“Imperative Expression”子项,使用带有“@Evaluate”表达式注释的方法将其链接到一个类,以决定显示/隐藏动态菜单,例如,如果菜单为空...

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