Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Dependency injection in Views/Editors running with the compatibility layer
Dependency injection in Views/Editors running with the compatibility layer [message #820234] Tue, 13 March 2012 22:12 Go to next message
Anders Forsell is currently offline Anders Forsell
Messages: 120
Registered: July 2009
Senior Member
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 #820501 is a reply to message #820234] Wed, 14 March 2012 07:52 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5403
Registered: July 2009
Senior Member
Hi,

I think the compat layer could allow usage of DI quite easily injecting
values after the instance creation through the extension registry is
finished.

I think you can probably mimic this yourself for the time being by
getting access to the IEclipseContext similar to how my e4 bridge does
it and invoke the injection yourself, if you put it into a baseclass
your subclasses won't notice it.

Tom

Am 13.03.12 23:12, schrieb Anders Forsell:
> 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 #820958 is a reply to message #820501] Wed, 14 March 2012 19:46 Go to previous messageGo to next message
Anders Forsell is currently offline Anders Forsell
Messages: 120
Registered: July 2009
Senior Member
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;	
		}
	}
Re: Dependency injection in Views/Editors running with the compatibility layer [message #820966 is a reply to message #820958] Wed, 14 March 2012 19:56 Go to previous message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5403
Registered: July 2009
Senior Member
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;
> }
> }
Previous Topic:Can ESelectionService propagate selection to 3.x legacy views?
Next Topic:Following the rcp tutorial
Goto Forum:
  


Current Time: Wed Oct 22 03:26:29 GMT 2014

Powered by FUDForum. Page generated in 0.01662 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software