capint
6/9/2016 - 7:45 AM

Eclipse >> E4 >> Execute a command programmatically

Eclipse >> E4 >> Execute a command programmatically

public class AHandler extends AbstractHandler {
  @Override
  public Object execute(ExecutionEvent event) throws ExecutionException {
    Shell shell = (Shell) ((IEvaluationContext) event.getApplicationContext()).getVariable("activeShell"); //$NON-NLS-1$
    ISelection selection = (ISelection) ((IEvaluationContext) event.getApplicationContext()).getVariable("selection"); //$NON-NLS-1$
    //...
  }
}
LegacyHandlerService.executeCommandInContext
  -> HandlerServiceImpl.executeHandler(ParameterizedCommand command, IEclipseContext staticContext)
  
HandlerServiceHandler.isHandled
  -> contexts = HandlerServiceImpl.peek(); <- returns the context get from executeHandler
// Prepare command
ICommandService commandService = PlatformUI.getWorkbench().getService(ICommandService.class);
Command command = commandService.getCommand(COMMAND_ID);
ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, null);

// Prepare context
IEclipseContext eclipseContext = PlatformUI.getWorkbench().getService(IEclipseContext.class);
eclipseContext.set(VARIABLE, variableObj);
IHandlerService handlerService = eclipseContext.get(IHandlerService.class);
IEvaluationContext evaluationContext = handlerService.getCurrentState();

//Execute command
handlerService.executeCommandInContext(parameterizedCommand, null, evaluationContext);