e4动态菜单贡献

col*_*nic 5 eclipse e4

此功能已在Kepler M4中实现,有关用法的详细信息,请参阅我的博客

我想实现对位于视图工具栏中的处理程序菜单的完全动态菜单贡献.在Eclipse 3中,可以将"dynamic"添加为org.eclipse.ui.menus对Menu的贡献!

我已经了解了www.vogella.com/blog/2010/10/26/processors-e4-model,解释了如何通过处理器模型扩展动态地为菜单做出贡献,但我说的是一个完全动态的菜单实现,它改变了在每次呼叫时.子菜单.如前所述,通过动态菜单贡献和isDynamic()集合在Eclipse 3.x中实现这一点没有问题.

我已经尝试了几种方法:

开放,未经验证的解决方案

  • 插入ToolControl以尝试SWT方法 - >非常复杂但可能有效

我现在已经敲打了一段时间,但似乎无法理解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
        }
Run Code Online (Sandbox Code Playgroud)

可以看出,一旦创建菜单,就会查询此代码,以确定该命令是否可执行.1 - 2的所有代码都是找到正确的MMenu元素.2 - 3的代码创建一个MenuItem并在字段中递增计数器.

但是在3,我第一次打开菜单时遇到java.util.ConcurrentModificationException!我假设在这一点上菜单迭代了elemMenu.getChildren()并且我不允许启用!

所以关于整个e4模型的所有模糊都是可以改变的;)(就像孩子'我知道这是一个baaaad hack !!!)

事情是:我真的认为添加完全动态菜单部分的可能性是最好的可用性工具之一,如果不能像E3那样在E4中实现它,这是一种非常严重的可能性降级!

- 更新

已经为此https://bugs.eclipse.org/bugs/show_bug.cgi?id=389063提交了一个Eclipse Bug

Pau*_*ter 2

正确的动态模型更新应该在您打开的错误中进行处理。作为 Juno 中 Eclipse4 中的解决方法,可以在 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);
Run Code Online (Sandbox Code Playgroud)

这有效地直接向 Eclipse4 菜单渲染器提供了一个CompoundContributionItem。