此功能已在开普勒M4中实现,有关使用的详细信息请参阅我的博客。
我想要实现完全动态菜单对位于视图工具栏中的处理程序菜单的贡献。在Eclipse3中,可以添加"dynamic“作为org.eclipse.ui.menus对菜单的贡献!
我已经了解了www.vogella.com/blog/2010/10/26/processors-e4-model,解释了如何通过处理器模型扩展对菜单进行动态贡献,但我正在讨论的是一个完全动态的菜单实现,它在resp的每个调用上都会发生变化。子菜单。如前所述,在Eclipse3.x中通过动态菜单贡献和isDynamic()集实现这一点是没有问题的。
我已经尝试过几种方法:
开放的,未经尝试的解决方案
我已经努力了一段时间,但似乎无法理解E4中这个问题的正确实现。
--这个问题也是在Eclipse论坛-动态菜单贡献上提出的
-更新
到目前为止,我尝试了一种不同的方法:
我在菜单中添加了一个HandledToolItem (请看下面的图片)

对于下面的代码,我试图干扰菜单的构建方式,在这里,代码由resp调用。在图像中引用的命令手柄。
@CanExecute
public boolean canExecute(@Optional MApplication application) {
System.out.println("CanExecute Counter="+counter);
// --- 1 ---
// Find the required MMenu Entry in the Application Model
if(application == null) return true;
EModelService modelService = (EModelService) application.getContext().get(EModelService.class.getName());
MPart part = (MPart) modelService.find("at.medevit.emr.contacts.ui.contactselector", application);
List<MToolBarElement> lmte = part.getToolbar().getChildren();
HandledToolItemImpl htil = null;
for (MToolBarElement mToolBarElement : lmte) {
if(mToolBarElement.getElementId().equals("at.medevit.emr.contacts.ui.contactselector.toolbar.handledtoolitem.filter")) htil = (HandledToolItemImpl) mToolBarElement;
}
if(htil != null) {
MMenu elemMenu = htil.getMenu();
// --- 2 ---
// Found it hopefully, let's start the real work, simply add a new item
MDirectMenuItem mdi = MMenuFactory.INSTANCE.createDirectMenuItem();
mdi.setLabel("Counter "+counter);
counter++;
// --- 3 ---
elemMenu.getChildren().add(mdi); // ConcurrentModificationException
}可以看到,一旦创建了菜单,就会查询此代码,以确定该命令是否可执行。1-2中的所有代码都是为了找到要处理的正确的MMenu元素。来自2-3的代码创建一个MenuItem,并在字段中增加一个计数器。
但是在第一次打开菜单的时候,我在3点面对一个java.util.ConcurrentModificationException!我假设此时菜单正在elemMenu.getChildren()上迭代,因此不允许启用!
那么,关于整个e4模型一直在变化的所有模糊的东西是什么?)(开玩笑的是,我知道这是一个baaaad的黑客!)
事情是这样的:我真的认为添加完全动态菜单部件的可能性是最好的可用性工具之一,如果在E4中不可能像在E3中那样实现它,这是非常严重的可能性退化!
--更新
已经为这个bug.cgi?id=389063提交了一个Eclipse
发布于 2012-09-10 14:41:05
应该在您打开的bug中处理正确的动态模型更新。作为Eclipse4在Juno中的一种解决方案,可以在Eclipse4中创建一个MRenderedMenuItem,以向动态元素提供等效的功能(尽管如果您使用的是4.2,您只需使用org.eclipse.ui.menus)。
例:
ContextFunction generator = new ContextFunction() {
@Override
public Object compute(IEclipseContext context) {
return new MyCompoundContributionItem(context);
}
};
MRenderedMenuItem menuItem = MenuFactoryImpl.eINSTANCE.createRenderedMenuItem();
menuItem.setElementId(id);
menuItem.setContributionItem(generator);
container.getChildren().add(menuItem);这有效地将CompoundContributionItem直接提供给Eclipse4菜单呈现器。
https://stackoverflow.com/questions/12315749
复制相似问题