问题描述
我有一个带有MMenu的Eclipse E4应用程序(在应用程序的主菜单中以及在不同部分的弹出菜单中),其中包含在运行时由动态菜单贡献提供的项目。
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.
我要实现的是,如果菜单贡献不提供任何项目,则禁用菜单元素。像@CanExecute这样的命令或直接菜单项的处理程序类。
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.
推荐答案
您使用的是最新版本的eclipse吗? Application.e4xmi
文件?
如果是这样,对于您的动态菜单贡献
,添加一个动态菜单贡献
条目,该条目指向一个类,并带有一个以 @ AboutToShow
注释的方法动态构建菜单项并为每个项目定义一个处理程序。
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
}
}
添加一个即时表达式
子代,使用带有 @ Evaluate注解的方法将其链接到一个类
表达式决定显示/隐藏动态菜单,例如,如果菜单为空...
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
}
这篇关于如何在Eclipse E4应用程序中禁用或启用MMenu(不是MMenuItem)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!