强制重新评估命令启用 [英] Force Re-Evaluation of Command Enablement

查看:260
本文介绍了强制重新评估命令启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < 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说


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



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.

这篇关于强制重新评估命令启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆