Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Injection of extened classes
Injection of extened classes [message #995903] Mon, 31 December 2012 08:17 Go to next message
Aljoscha Steffens is currently offline Aljoscha Steffens
Messages: 302
Registered: November 2012
Senior Member
Hi,
is there a reason why this
@Creatable
public class FileListViewer extends ListViewer{

	FileNameList fileList; 
	
	@Inject
	public FileListViewer(Composite parent) {
		super(parent, SWT.NONE);
		System.out.println("aaaa");

doesn't work if I try to inject the FileListViewer Class somewhere?

If I remove the extension and the 'super'-call it prints 'aaaa'.
Re: Injection of extened classes [message #995912 is a reply to message #995903] Mon, 31 December 2012 08:50 Go to previous messageGo to next message
Aljoscha Steffens is currently offline Aljoscha Steffens
Messages: 302
Registered: November 2012
Senior Member
hmm it appears that it's because of the composite. This code doesn't work either, only if I remove the composite injection

@Creatable
public class FileListViewer{

	FileNameObjectList fileList;
	ListViewer listViewer;
	
	@Inject
	public FileListViewer(Composite parent, FileNameObjectList fileList) {

		listViewer = new ListViewer(parent, SWT.NONE);

		this.fileList = fileList;

	}


Now I'm really confused since I never had a problem with injecting composites..
Re: Injection of extened classes [message #995923 is a reply to message #995912] Mon, 31 December 2012 09:30 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5257
Registered: July 2009
Senior Member
Well it depends on the IEclipseContext you are using. This context has
to have a key Composite.class.getName()

Tom

Am 31.12.12 14:50, schrieb Aljoscha Steffens:
> hmm it appears that it's because of the composite. This code doesn't
> work either, only if I remove the composite injection
>
>
> @Creatable
> public class FileListViewer{
>
> FileNameObjectList fileList;
> ListViewer listViewer;
>
> @Inject
> public FileListViewer(Composite parent, FileNameObjectList fileList) {
>
> listViewer = new ListViewer(parent, SWT.NONE);
>
> this.fileList = fileList;
>
> }
>
>
> Now I'm really confused since I never had a problem with injecting
> composites..
Re: Injection of extened classes [message #995957 is a reply to message #995923] Mon, 31 December 2012 11:29 Go to previous messageGo to next message
Aljoscha Steffens is currently offline Aljoscha Steffens
Messages: 302
Registered: November 2012
Senior Member
hmm not sure what you are talking about.
Do I need to state where objects of the class 'FileListViewer' will be created?
I want to use them in a couple of different wizards
Re: Injection of extened classes [message #995971 is a reply to message #995957] Mon, 31 December 2012 12:21 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5257
Registered: July 2009
Senior Member
What I tried to tell you is the the IEclipseContext you are using to
create the instance does not have a Composite in it so the look up fails
and you get an exception and even if there's a composite the chances are
99.99% that it is the wrong one, so you need to register the Composite
before creating the instance.

Tom

Am 31.12.12 17:29, schrieb Aljoscha Steffens:
> hmm not sure what you are talking about.
> Do I need to state where objects of the class 'FileListViewer' will be
> created?
> I want to use them in a couple of different wizards
Re: Injection of extened classes [message #996634 is a reply to message #995971] Wed, 02 January 2013 10:05 Go to previous messageGo to next message
Aljoscha Steffens is currently offline Aljoscha Steffens
Messages: 302
Registered: November 2012
Senior Member
Hmm okay. Would I register the Composite like this:
MyWizard wizard = new MyWizard();
IEquinoxContext context = ContextFactory.createContext();

context.addObject("MyWizard", wizard);


This would obviously not works since I need to add the wizard to the context before I create it.
So how would I do it?

And if I add an Object to the context, can I refere to this certain object via @Named("MyWizard")?
Re: Injection of extened classes [message #996646 is a reply to message #996634] Wed, 02 January 2013 10:39 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5257
Registered: July 2009
Senior Member
Hi,

MyWizard is not composite is it? I don't know what MyWizard is so I
can't tell you of this would be correct.

Also you should not create your own root context but let the framework
provide you the context and from there you'd create a child-context.

-------8<-------
@Inject
IEclipsContext ctx;

IEclipseContext lCtx = ctx.createChild();
lCtx.set(Composite.class,...)
-------8<-------

I have the gut feeling that you are abusing @Creatable instead of
creating an instance at the appropriate time using
ContextInjectionFactory.make().

Tom

Am 02.01.13 16:05, schrieb Aljoscha Steffens:
> Hmm okay. Would I register the Composite like this:
>
> MyWizard wizard = new MyWizard();
> IEquinoxContext context = ContextFactory.createContext();
>
> context.addObject("MyWizard", wizard);
>
>
> This would obviously not works since I need to add the wizard to the
> context before I create it.
> So how would I do it?
>
> And if I add an Object to the context, can I refere to this certain
> object via @Named("MyWizard")?
Re: Injection of extened classes [message #996671 is a reply to message #996646] Wed, 02 January 2013 11:42 Go to previous messageGo to next message
Aljoscha Steffens is currently offline Aljoscha Steffens
Messages: 302
Registered: November 2012
Senior Member
No you are right. I have a Composite in the wizard to which the FileListViewer should be assigned to.
@Override
	public void createControl(Composite parent ) {
		container = new Composite(parent, SWT.NONE);


(to this one). I didnt think of this before. So this is probably not possible... I guess I can't extend the ListViewer class but have to add it as a field to FileListViewer.

Your guts are right. Actually I was quite sure that what I did wasn't 100% correct.
So far I wrote @Creatable in front of nearly every class.
If I wanted to use the class somewhere I just Injected it via the constructor.
If I wanted to have just one instance of the class, I wrote @Singleton in front of @Creatable

I would be really really glad if you had some good examples or tutorials on how to use the DI correctly. I think it's quite a nice pattern, but obviously I'm using it wrong




Re: Injection of extened classes [message #997063 is a reply to message #996646] Thu, 03 January 2013 09:14 Go to previous messageGo to next message
Aljoscha Steffens is currently offline Aljoscha Steffens
Messages: 302
Registered: November 2012
Senior Member
Here is an example of how I use DI. How would you change this approach? And what is the 'programmatical' flaw?
So far it worked (except for the problem I have right now). I'm really willing to learn this concept and I read a couple of articles again today but I think some detailled examples would help me a lot more.


// ------------------------  some handler class ------------------------  

@Execute
	public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell, MyWizard wizard) {
		WizardDialog wizardDialog = new WizardDialog(shell, wizard);

		wizardDialog.open();


	}
 
//----------------------------------------------------------------------





// ------------------------  MyWizard ------------------------  



@Creatable
public class MyWizard extends Wizard {


	@Inject
	public ProjectWizard(MyWizardPage page){
		super();
		this.page = page;
		
	}


//-------------------------------------------------------------







// ------------------------  MyWizardPage ------------------------ 


@Creatable
public class MyWizardPage extends WizardPage {
	
	private Composite container;
	private FileListViewer fileListViewer;
	private Shell shell;

	@Inject
	public ProjectWizardPage(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell, 
			FileListViewer fileListViewer){		
		super("MyWizard");
		this.shell = shell;
		this.fileListViewer = fileListViewer;

	}


	@Override
	public void createControl(Composite parent ) {
		container = new Composite(parent, SWT.NONE);
		container.setLayout(new GridLayout(3, false));

	...
	}

// -----------------------------------------------------------------







// ------------------------  FileListViewer ------------------------  

@Creatable
public class FileListViewer extends ListViewer{

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

	}


//------------------------------------------------------------------
Re: Injection of extened classes [message #997512 is a reply to message #997063] Mon, 07 January 2013 17:41 Go to previous messageGo to next message
Aljoscha Steffens is currently offline Aljoscha Steffens
Messages: 302
Registered: November 2012
Senior Member
I found a couple of sites which describe the usage of the IEclipseContext (http://www.toedter.com/blog/?p=194 http://www.heise.de/developer/artikel/Services-und-Dependency-Injection-in-der-Eclipse-4-0-Application-Platform-1048518.html?artikelseite=2 ...)
but I don't want to go on if I did't understand the pattern completly. I will probably make it work, but the way I use it right now works as well (so far) and is still wrong.. So does someone has some advice? Smile
Re: Injection of extened classes [message #999685 is a reply to message #997512] Sat, 12 January 2013 16:47 Go to previous messageGo to next message
Aljoscha Steffens is currently offline Aljoscha Steffens
Messages: 302
Registered: November 2012
Senior Member
Okay, maybe I got it now:
Say there is a Class MyClass which needs and Instance of NeededClass:
Class MyClass

@Inject
    public void MyClass(NeededClass neededClass){
     ...
    }


So if an instance of MyClass should be created, the frameworks looks in the context of MyClass if it finds an instance of NeededClass.
The way I did it was to mark the class NeededClass with @Creatable : the context would not find an instance of NeededClass, so it would create one.
But I should rather create an context for the instance of MyClass where an instance of NeededClass is available.
Like this
////.... in some other class I want to create an instance of MyClass

@Inject
public void foo(IEclipseContext ctx){
   NeededClass myNeededClass = ContextInjectionFactory.make(NeededClass.class, ctx);
   ctnx.set(NeededClass.class, myNeededClass);
   MyClass myClass = ContextInjectionFactory.make(MyClass.class, ctx);

   ...
}



Is this the correct way?
Re: Injection of extened classes [message #999866 is a reply to message #999685] Sun, 13 January 2013 03:42 Go to previous message
Thomas Schindl is currently offline Thomas Schindl
Messages: 5257
Registered: July 2009
Senior Member
Yes, you got it! The only thing you need to make sure now it to dispose
the child context once, once MyClass is not needed anymore.

Tom

Am 12.01.13 22:47, schrieb Aljoscha Steffens:
> Okay, maybe I got it now:
> Say there is a Class MyClass which needs and Instance of NeededClass:
>
> Class MyClass
>
> @Inject
> public void MyClass(NeededClass neededClass){
> ...
> }
>
>
> So if an instance of MyClass should be created, the frameworks looks in
> the context of MyClass if it finds an instance of NeededClass.
> The way I did it was to mark the class NeededClass with @Creatable : the
> context would not find an instance of NeededClass, so it would create one.
> But I should rather create an context for the instance of MyClass where
> an instance of NeededClass is available.
> Like this
>
> ////.... in some other class I want to create an instance of MyClass
>
> @Inject
> public void foo(IEclipseContext ctx){
> NeededClass myNeededClass =
> ContextInjectionFactory.make(NeededClass.class, ctx);
> ctnx.set(NeededClass.class, myNeededClass);
> MyClass myClass = ContextInjectionFactory.make(MyClass.class, ctx);
>
> ...
> }
>
>
>
> Is this the correct way?
Previous Topic:How to show upper-left corner icon for a E4 APP
Next Topic:Update / refresh Part
Goto Forum:
  


Current Time: Wed Jul 30 23:31:14 EDT 2014

Powered by FUDForum. Page generated in 0.02902 seconds