Dependency injection in Views/Editors running with the compatibility layer [message #820234] |
Tue, 13 March 2012 18:12  |
Eclipse User |
|
|
|
Hello,
We are migrating an RCP application to 4.x. Our editor is using GEF so we cannot migrate it to be a pure e4 part.
As we understand dependency injection does not work for views/editors running through the compatibility layer.
What is the reason for that?
We would like to use dependency injection to easily use services, such as the Logger in the same manner as if it was a pure view/editor.
We have looked at the "forward compatibility layer" by Tom Schindl, but it seems more appropriate when single-sourcing for running both 3.x and 4.x.
Regards,
Anders
|
|
|
|
|
Re: Dependency injection in Views/Editors running with the compatibility layer [message #820966 is a reply to message #820958] |
Wed, 14 March 2012 15:56  |
Eclipse User |
|
|
|
Hi,
No the parent context is not good because e.g. when parts are reparented
the MPart-Context gets reparented as well so you need to use the one
from the MPart.
Tom
Am 14.03.12 20:46, schrieb Anders Forsell:
> Ok thanks, I got it to work.
>
> A question about the "hack" to get the MPart context: In my test I am
> getting the same IEclipseContext instance as the parentContext. Do I
> need to do the MPart.getContext or can I skip that and just return the
> parent context (see snippet below from your code) ?
>
> private IEclipseContext getEclipseContext(WorkbenchPart part) throws
> PartInitException {
> IWorkbenchPartSite site = part.getSite();
> IEclipseContext parentContext = (IEclipseContext)
> site.getService(IEclipseContext.class);
>
> // Check if running in 4.x
> if(
> parentContext.get("org.eclipse.e4.ui.workbench.IPresentationEngine") !=
> null ) {
> // Hack to get the MPart-Context
> try {
> Class<?> clazz =
> getBundle("org.eclipse.e4.ui.model.workbench").loadClass("org.eclipse.e4.ui.model.application.ui.basic.MPart");
>
> Object instance = site.getService(clazz);
> Method m = clazz.getMethod("getContext", new Class[0]);
> IEclipseContext ctx = (IEclipseContext) m.invoke(instance);
> return ctx;
> } catch (Exception e) {
> throw new PartInitException("Could not create context",e);
> }
> } else {
> return parentContext;
> }
> }
|
|
|
Powered by
FUDForum. Page generated in 0.10670 seconds