Force Re-Evaluation of Command Enablement

292 Views Asked by At

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);
1

There are 1 best solutions below

0
On BEST ANSWER

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.