强制重新评估命令启用(Force Re-Evaluation of Command Enablement)

159 IT屋

I added a handler for the eclipse print command like that:

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

Which works really nicely. But sometimes the enablement of the command changes even though the active part did not. So I want to force a re-evaluation of the activeWhen part. How do I do this?

I tried something like this:

    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);
解决方案

I think the IEvaluationService.requestEvaluation method may do what you want:

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

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

The JavaDoc says

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



 < 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); 
命令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);

//这不执行
printCommand.setEnabled(context);
//这样做也不行
service.refreshElements("org.eclipse.ui.file.print",null);
//执行命令
//(但至少重新评估它的启用)
printCommand.executeWithChecks(event);

解决方案

我认为 IEvaluationService。 requestEvaluation 方法可以做你想要的:



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

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


JavaDoc说




请求此服务重新评估包含给定属性名称的属性测试器的所有注册核心表达式



本文地址:IT屋 » 强制重新评估命令启用