强制重新评估命令启用

Force Re-Evaluation of Command Enablement

我为这样的 eclipse 打印命令添加了一个处理程序:

  <handler
        class="org.acme.PrintHandler"
        commandId="org.eclipse.ui.file.print">
     <activeWhen>
        <with variable="activePart">
           <test property="org.acme.printable" />
        </with>
     </activeWhen>
  </handler>

效果非常好。但有时即使活动部分没有,命令的启用也会发生变化。所以我想强制重新评估 activeWhen 部分。我该怎么做?

我试过这样的事情:

    ICommandService service = (ICommandService) PlatformUI.getWorkbench().getService(ICommandService.class);
    Command printCommand = service.getCommand("org.eclipse.ui.file.print");
    EvaluationContext context = new EvaluationContext(null, lastEditor);
    context.addVariable(ISources.ACTIVE_PART_NAME, lastEditor);
    ExecutionEvent event = new ExecutionEvent(printCommand, Collections.EMPTY_MAP, null, context);

    // this does nothing
    printCommand.setEnabled(context);
    // this does nothing as well
    service.refreshElements("org.eclipse.ui.file.print", null);
    // this executes the command 
    // (but at least re-evaluates it's enablement before)
    printCommand.executeWithChecks(event);

我认为 IEvaluationService.requestEvaluation 方法可以满足您的要求:

IEvaluationService evalService = (IEvaluationService) PlatformUI.getWorkbench().getService(IEvaluationService.class);

evalService.requestEvaluation("org.acme.printable");

JavaDoc 说

Request that this service re-evaluate all registered core expressions that contain a property tester for the given property name.