capint
1/2/2017 - 1:42 PM

Eclipse >> E4 >> Handlers that execute an injected POJO

Eclipse >> E4 >> Handlers that execute an injected POJO

//*** Source: http://eclipsesource.com/blogs/tutorials/eclipse-4-e4-tutorial-soft-migration-from-3-x-to-eclipse-4-e4/

public class DIHandler extends AbstractHandler {

private Class clazz;
private C component;

//*** clazz is the class of the POJO with injections
public DIHandler(Class clazz) {
  this.clazz = clazz;
  //*** Create an instanceof clazz and inject it with data from the active context
  component = ContextInjectionFactory.make(clazz, getActiveContext());
}

private static IEclipseContext getActiveContext() {
  IEclipseContext parentContext = (IEclipseContext) PlatformUI.getWorkbench().getService(
  IEclipseContext.class);
  return parentContext.getActiveLeaf();
}

public Object execute(ExecutionEvent event) throws ExecutionException {
  //*** Invoke the method annotated with @Execute of the POJO class while injecting with data from the active context
  return ContextInjectionFactory.invoke(component, Execute.class, getActiveContext());
}