Search code examples
javarcpe4

RCP - Application does not have an active window


What is the best way of getting non-stale EPartService/EModelService/MApplication references inside of a custom service? I know it can be done inside a Handler via the @Execute method, but I don't see a way of doing it in a custom service class. Any help would be appreciated.

@Singleton
@Creatable
public class MyService {

    @Inject
    private EPartService partService;
    @Inject
    private EModelService modelService;
    @Inject
    private MApplication application;

    @Inject
    public MyService(final IEclipseContext context) {
        context.set(MyService.class.getName(), this);
    }

    public void doWork(){
     // Application does not have an active window
    }
}

Solution

  • Use the active leaf context:

    IEclipseContext activeContext = application.getContext().getActiveLeaf();
    
    EPartService partService = activeContext.get(EPartService.class);
    

    I don't think the model and part service actually change so just using application.getContext() should also be OK.

    The application object does not change.