繁体   English   中英

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

[英]How to programmatically add an enableWhen condition in my java class -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 (来自Plug-xml)转换为可以求值的表达式。

ExpressionConverter converter = ExpressionConverter.getDefault();

Expression expression = converter.perform(element);

其中elementIConfigurationElementorg.w3c.dom.Element

获得表达式后,就可以对其进行评估:

EvaluationResult result = expression.evaluate(context);

boolean isEnabled = result == EvaluationResult.TRUE;

contextIEvaluationContext

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

IEclipseContext eclipseContext = ... current Eclipse context

IEvaluationContext context = new ExpressionContext(eclipseContext);

暂无
暂无

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

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