Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » DI : Define custom parent Composite for inject
DI : Define custom parent Composite for inject [message #1003443] Mon, 21 January 2013 03:59 Go to next message
Filipp A. is currently offline Filipp A.
Messages: 49
Registered: February 2010
Member
Hi @All,
how can I define own Composite object for injection ?

for example, if I do

Part
...
@Inject CompositeOne cmp;
...


Composite 1
@Creatable
public class CompositeOne extends Composite {

@Inject CompositeTwo cmp;

@Inject
public CompositeOne(Composite parent){
    super(parent, SWT.NONE;)
}

@PostConstruct
public void postConstruct(){
    cmp.setText("text");
}
...
}


Composite 2
@Creatable
public class CompositeTwo extends Composite {

@Inject
public CompositeTwo(Composite parent){
    super(parent, SWT.NONE;)
}
...
}


Both CompositeOne & CompositeTwo have the same parent Composite - Part
Part -> Composite 1
Part -> Composite 2

and I want to have:
Part -> Composite 1 -> Composite 2

thanks in advance!
Re: DI : Define custom parent Composite for inject [message #1003446 is a reply to message #1003443] Mon, 21 January 2013 04:06 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk Fauth
Messages: 1067
Registered: July 2012
Senior Member
Your Composite 2 shouldn't be created by DI. It needs to be created by yourself as it is not part of the Application Model.

The Application Model defines your UI only to the parts of an application. Custom controls that are UI toolkit dependent need to be created in your part implementation.

This is true unless you enhance the Application Model. But regarding to your question this seems not to be the case.

Greez,
Dirk
Re: DI : Define custom parent Composite for inject [message #1003478 is a reply to message #1003446] Mon, 21 January 2013 05:31 Go to previous messageGo to next message
Filipp A. is currently offline Filipp A.
Messages: 49
Registered: February 2010
Member
hmmmm.... thanks for info!

i think, i have a solution -
@Creatable
public class CompositeOne extends Composite {

private CompositeTwo cmp;

@Inject
public CompositeOne(Composite parent, IEclipseContext context){
    super(parent, SWT.NONE;)
    context.set("parent", this);
}

@PostConstruct
public void postConstruct(CompositeTwo cmp){
  this.cmp = cmp;
    cmp.setText("text");
}
...
}


@Creatable
public class CompositeTwo extends Composite {

@Inject
public CompositeTwo(@Named("parent") Composite parent){
    super(parent, SWT.NONE;)
}
...
}


what do you think about it ? or is it not so good ?
Re: DI : Define custom parent Composite for inject [message #1003483 is a reply to message #1003478] Mon, 21 January 2013 05:41 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk Fauth
Messages: 1067
Registered: July 2012
Senior Member
I still don't get it what kind of Composites you are creating.

Are they SWT Composites?
Should they be part of the Application Model?
Why is it necessary for you to use DI on creating instances of those classes?
Do you need the created instances at various places within your application or just at the code you are implementing it?

Although DI is very cool and I like it much, it is not always the best solution to use it. Especially if it doesn't bring you additional value. And in the above case I don't see the additional value compared to default object creation.

Greez,
Dirk
Re: DI : Define custom parent Composite for inject [message #1003489 is a reply to message #1003483] Mon, 21 January 2013 05:55 Go to previous messageGo to next message
Filipp A. is currently offline Filipp A.
Messages: 49
Registered: February 2010
Member
yes, i create swt composite with access to DI components.
i have RCP based application with complex ui components - for example:
Part (View) has a CFolder (Composite), to that added tabs over extension points , that have many other composites, and if they a not injected, i don't have access to ECommandService, EHandlerService - they are allways null ... (i saw topic with same problem http://www.eclipse.org/forums/index.php/m/898681/)
and yet i'm looking for good solution to do this...
Re: DI : Define custom parent Composite for inject [message #1003494 is a reply to message #1003489] Mon, 21 January 2013 06:12 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk Fauth
Messages: 1067
Registered: July 2012
Senior Member
OK, now I got what you are trying to do. Well still IMO creating SWT Composites shouldn't be done automatically with @Creatable and DI. But that's just my oppinion. I would rather suggest to use ContextInjectionFactory.inject().

//method in your part implementation
@PostConstruct
public void createLayout(Composite parent, IEclipseContext context) {
    CompositeOne comp = new CompositeOne(parent);
    ContextInjectionFactoryl.inject(comp, context);
}


To get access to the services you need, you can use field injection then.

Hope that helps,
Dirk
Re: DI : Define custom parent Composite for inject [message #1003500 is a reply to message #1003494] Mon, 21 January 2013 06:27 Go to previous message
Filipp A. is currently offline Filipp A.
Messages: 49
Registered: February 2010
Member
thank you!
Previous Topic:Image Registry / Icons
Next Topic:How to clear persisted state if application model has changed?
Goto Forum:
  


Current Time: Wed Apr 23 02:54:34 EDT 2014

Powered by FUDForum. Page generated in 0.01809 seconds