【问题标题】:Eclipse Plugin development showing and hiding menuContribution when opening and closing perspective打开和关闭透视图时显示和隐藏 menuContribution 的 Eclipse 插件开发
【发布时间】:2021-02-11 02:37:40
【问题描述】:
我正在使用 E4 2020-09 版本开发我的 Eclipse 插件。我使用模型片段创建了一个 Perspective 和一个 menuContribution。我已经搜索了几个教程,但我没有看到任何显示在开发过程中打开/关闭 E4 中的透视图时如何使 menuContribution 出现/消失的教程。我发现的是这些例子:https://github.com/vogellacompany/codeexamples-eclipse 但这个功能是为 E3 实现的,我想在 E4 中实现它。
你能给我一些关于这种技术的提示/建议吗?它是如何被调用的或者从哪里开始的?
谢谢和最好的问候。
【问题讨论】:
标签:
menu
eclipse-plugin
eclipse-rcp
e4
【解决方案1】:
您可以在菜单项的“Visible-When Expression”中执行此操作。
将表达式设置为“ImperativeExpression”。并创建一个类来处理表达式。这个类只有一个用@Evaluate注解的方法,每次显示菜单项时都会调用它:
@Evaluate
public boolean evaluate(EModelService modelService, .... other parameters)
{
// TODO determine if menu item should be visible
}
然后这个类可以使用EModelService 的getActivePerspective 方法来检查菜单项是否应该可见。
【解决方案2】:
449,
谢谢你的回答,我终于按照你的指示做了。如果有人问这个问题,我会在这里留下我的参考代码:
- 创建 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;
}
}
- 将此类添加到 MenuContribution 类 URI 的命令式表达式。
最好的问候。