java - 如何以编程方式在我的 java 类 -Eclipse RCP 中添加enableWhen条件

标签 java eclipse-rcp

首先,我想我必须澄清与我的问题相关的一些要点: 我想重写 RCP 项目上按钮的行为以添加特定处理。所以我做了一些步骤:

  1. 我在这个现有插件上创建了一个新片段,但我无权访问该片段。

  2. 我覆盖新片段上的现有处理程序。

  3. 我添加了一个 fragement.xml,在其中定义了一个新的上下文,覆盖处理程序如下面的代码所示:

<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?><?eclipse version="3.4"?>
<fragment>
    <extension point="org.eclipse.ui.contexts">
        <context id="myContext" name="myContext">
        </context>
    </extension>
    <extension point="org.eclipse.ui.handlers">
        <handler class="myHandler" commandId="myCommand">
            <enabledWhen>
                <and>
                    <reference definitionId="object_selected">
                    </reference>
                    <reference definitionId="operation_allowed">
                    </reference>
                </and>
            </enabledWhen>
            <activeWhen>
                <with variable="activeContexts">
                </with>
                <iterate ifEmpty="false" operator="or">
                    <equals value="myContext">
                    </equals>
                </iterate>
            </activeWhen>
        </handler>
    </extension>
</fragment>

但我的上下文始终未定义(我不知道为什么??),因此我以编程方式定义了上下文、覆盖处理程序,并将其链接到 myCommand,如以下代码所示:

//Define myContext
IContextService contextService = (IContextService) PlatformUI.getWorkbench().getService(IContextService.class);
Context myContext = contextService.getContext("myContext");

if (!myContext.isDefined()) {
  myContext.define("myContext", "To allow the consumption of my overrided Handler", "org.eclipse.ui.contexts.window");
} 
contextService.activateContext(myContext.getId());

//link handler to myContext

//Command
ICommandService iCommandService = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
Command myCommand = iCommandService.getCommand("myCommand");

//Handler
IHandlerService   iHandlerService = (IHandlerService) PlatformUI.getWorkbench().getService(IHandlerService.class);
 MyHandler myHandler = new MyHandler();
myCommand.setHandler(handler);

//set activation conditions
if(myContext!= null && contextService.getActiveContextIds().contains(myContext.getId())) {
    iHandlerService.activateHandler("myCommand", myHandler);
    // I'm stuck on this step, i need to know how to declare an enableWhen //condition like: myHandler.setEnabled(evaluationContext);
}

现在,我陷入了这一步:我不知道如何以编程方式为我的覆盖处理程序添加启用条件(例如 myHandler.setEnabled(evaluationContext))。

最佳答案

您可以使用org.eclipse.core.expressions.ExpressionConverter将XML DOM或IConfigurationElement(来自插件xml)转换为可以已评估。

ExpressionConverter converter = ExpressionConverter.getDefault();

Expression expression = converter.perform(element);

其中 elementIConfigurationElementorg.w3c.dom.Element

一旦你有了表达式,你就可以评估它:

EvaluationResult result = expression.evaluate(context);

boolean isEnabled = result == EvaluationResult.TRUE;

其中context是一个IEvaluationContext

对于上下文,您可以使用org.eclipse.e4.core.commands.ExpressionContext

IEclipseContext eclipseContext = ... current Eclipse context

IEvaluationContext context = new ExpressionContext(eclipseContext);

关于java - 如何以编程方式在我的 java 类 -Eclipse RCP 中添加enableWhen条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57207492/

相关文章:

java - 使用 JPA/ORM 生成数据库模式是个坏主意吗?

java - 从 Eclipse 内部运行的 RCP 应用程序的 .log 文件在哪里?

eclipse - 用户安装 Eclipse 功能后,如何在首次启动时打开我的视角

java - 使用哪个监听器来通知遏制中的更改?

java - 如何通过API取消paypal支付

java - Hibernate 保存返回错误的生成 ID

java - 呈现的 JSP 标记有乱码的 UTF-8 字符

java - Metro 的 Eclipse RCP 类加载问题

eclipse - Eclipse 4.12-java.lang.ClassNotFoundException:com.sun.xml.internal.bind.v2.ContextFactory

java - 在已构建的表上设置 TableModel