简体   繁体   English

Eclipse 插件开发在打开和关闭透视图时显示和隐藏菜单贡献

[英]Eclipse Plugin development showing and hiding menuContribution when opening and closing perspective

I am developing my Eclipse Plugin using E4 2020-09 version.我正在使用 E4 2020-09 版本开发我的 Eclipse 插件。 I created a Perspective and a menuContribution using Model Fragments.我使用模型片段创建了一个透视图和一个菜单贡献。 I have searched several tutorials but I have not seen any that showing how to make a menuContribution appear/disappear when opening/closing a Perspective in E4 during development.我已经搜索了几个教程,但我还没有看到任何显示在开发过程中打开/关闭 E4 中的透视图时如何使 menuContribution 出现/消失的教程。 What I found was these examples: https://github.com/vogellacompany/codeexamples-eclipse but this function is implemented for E3 and I want to implement it in E4.我发现的是这些例子: https : //github.com/vogellacompany/codeexamples-eclipse但这个功能是为 E3 实现的,我想在 E4 中实现它。 Can you give me some hints/advices about this technique and how it is called or where to start with it?你能给我一些关于这种技术的提示/建议,它是如何调用的,或者从哪里开始?

Thanks and best regards.谢谢和最好的问候。

You can do this in the 'Visible-When Expression' for the menu items.您可以在菜单项的“可见时表达式”中执行此操作。

Set the expression to be 'ImperativeExpression'.将表达式设置为“ImperativeExpression”。 And create a class to handle the expression.并创建一个类来处理表达式。 This class just has a single method annotated with @Evaluate which is called each time the menu item might be displayed:这个类只有一个用@Evaluate注释的方法,每次可能显示菜单项时都会调用它:

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

This class can then use the getActivePerspective method of EModelService to check if the menu item should be visible.这个类然后可以使用EModelServicegetActivePerspective方法来检查菜单项是否应该可见。

449,第449话

Thank you for you answer, I finally made it according to your instruction.谢谢你的回答,我终于按照你的指示做了。 I will leave my reference code here in case there are people asking about this:我会在这里留下我的参考代码,以防有人问这个:

  1. Create MenuContributionsHandler class创建 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. Add this class to the Imperative Expression to MenuContribution Class URI.将此类添加到 MenuContribution 类 URI 的命令式表达式。

Best regards.此致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM