【发布时间】:2012-09-01 04:45:28
【问题描述】:
此功能自 Kepler M4 起已实现,有关使用的详细信息请参阅my blog
我想为位于视图工具栏中的处理程序的菜单实现一个完全动态的菜单。在 Eclipse 3 中,可以添加“动态”作为 org.eclipse.ui.menus 对菜单的贡献!
我已经发现 www.vogella.com/blog/2010/10/26/processors-e4-model 解释了如何通过处理器模型扩展来动态地为菜单做出贡献,但我正在谈论的是一个完全动态的菜单实现,它在每次调用相应的调用时都会发生变化。子菜单。如前所述,在 Eclipse 3.x 中通过动态菜单贡献和将 isDynamic() 设置为 true 来实现这一点是没有问题的。
我已经尝试了几种方法:
- 注册与菜单挂钩的处理器 => 不可能动态添加(新元素根本没有显示,也在Eclipse Forum - Cannot replace menu items at runtime 中讨论过)
- 侦听 UIEventTopic 以获取创建菜单的事件,获取用于修改的小部件 => 对收集的 swt.Menu 的修改被简单地忽略(每个侦听器、元素等)(用于信息 RCP Event Model
开放的、未经尝试的解决方案
- 插入 ToolControl 以尝试 SWT 方法 -> 相当复杂但可能有效
我一直在摸索一段时间,但似乎无法理解 E4 中这个问题的正确实现。
--Eclipse Forum - Dynamic menu contributions 也有人问过这个问题
----- 更新
到目前为止,我尝试了另一种方法:
我在菜单中添加了一个 HandledToolItem(请看下图)
并使用以下代码,我试图干扰菜单的构建方式,其中代码由相应的调用。图像中引用的命令处理程序。
@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 hack !!!)
问题是:我真的认为添加完全动态菜单部分的可能性是最好的可用性工具之一,如果在 E4 中无法像在 E3 中那样实现它,这将是一种非常严重的可能性退化!!!
-- 更新
已为此 https://bugs.eclipse.org/bugs/show_bug.cgi?id=389063 提交了一个 Eclipse 错误
【问题讨论】: