Eclipse Plugin开发显示和隐藏菜单打开和关闭透视图时的贡献

问题描述

我正在使用E4 2020-09版本开发我的Eclipse插件。我使用模型片段创建了一个Perspective和一个menuContribution。我搜索了一些教程,但是没有看到任何显示在开发过程中在E4中打开/关闭Perspective时如何使menuContribution出现/消失的信息。我发现的是以下示例:https://github.com/vogellacompany/codeexamples-eclipse,但是此功能已针对E3实现,我想在E4中实现。 您能否给我一些有关这项技术的提示/建议,以及如何称呼它或从何处开始?

感谢和问候。

解决方法

您可以在菜单项的“可见时显示”中执行此操作。

将表达式设置为“ ImperativeExpression”。并创建一个处理表达式的类。该类只有一个带有@Evaluate注释的方法,每次显示菜单项时都会调用该方法:

@Evaluate
public boolean evaluate(EModelService modelService,.... other parameters)
{
  // TODO determine if menu item should be visible
}

然后,此类可以使用getActivePerspective的{​​{1}}方法来检查菜单项是否应可见。

,

449,

谢谢您的回答,我终于按照您的指示做了。如果有人问这个,我将在这里保留我的参考代码:

  1. 创建MenuContributionsHandler类
package com.catto.ide.dev.handlers;

import javax.inject.Inject;

import org.eclipse.e4.core.di.annotations.Evaluate;
import org.eclipse.e4.ui.model.application.ui.advanced.MPerspective;
import org.eclipse.e4.ui.model.application.ui.basic.MWindow;
import org.eclipse.e4.ui.workbench.modeling.EModelService;

public class MenuContributionsHandler 
{
    @Inject MWindow window;
    
    @Evaluate
    public boolean evaluate(EModelService modelService)
    {
        // TODO determine if menu item should be visible (return true)
        MPerspective currentPerspective = modelService.getActivePerspective(window);
        
        if (null != currentPerspective)
        {
            return currentPerspective.getLabel().equals("SnowCatto");
        }
        
        return false;
    }
}
  1. 将此类添加到MenuContribution类URI的命令式表达式中。

最诚挚的问候。