capint
11/21/2016 - 10:21 AM

Eclipse >> E4 >> Dependency injection >> Common uses

Eclipse >> E4 >> Dependency injection >> Common uses

//*** Source: http://eclipsesource.com/blogs/tutorials/eclipse-4-e4-tutorial-part-4-dependency-injection-basics/
//*** Inject the active shell
@ Inject
@ Named (IServiceConstants.ACTIVE_SHELL)
Shell shell;

//*** Inject the active part as a parameter
@Inject
public void save (@Active MPart part) {
  partService.save (part);
}

//*** Inject the part (as a composite parent) to the constructor of a View
@ Inject
public void MyView (Composite parent) {
  //Implement the View placed on the Parent
}

//*** Inject the selection service as a field
@ Inject
ESelectionService service;
...
service.setSelection (mySelection);

//*** Inject the actively selected element as parameter of a method
//*** The Optional annotation makes sure that there's no exception when there is no active selection (e.g. application just launched)
@ Inject
public void setSelection (@ Named (IServiceConstants.ACTIVE_SELECTION) @ Optional MyObject myObject) {
  //Process Selection
}