Home » Eclipse Projects » Eclipse 4 » When are my (injected) POJO methods called?(dependency injection)
| | | | | | | | | | | | |
Re: When are my (injected) POJO methods called? [message #547675 is a reply to message #543666] |
Mon, 19 July 2010 06:41   |
Eclipse User |
|
|
|
Hi,
I had a little more time to dig into the problem and the equinox (both util and event) are started. That still didn't solve my problem.
However there is an exception to my problem.
Currently I am using a stack view with two viewparts in it. Viewpart B uses ViewPart A within and I am creating it like that:
(This is create control from viewPart B):
@Inject
public void createControl( final Composite parent ) {
Composite main = new Composite( parent, SWT.NONE );
main.setLayout( new FormLayout() );
canvas = new Canvas( main, SWT.BORDER | SWT.DOUBLE_BUFFERED);
FormData data = new FormData( COMPASS_RADIUS, COMPASS_RADIUS );
data.left = new FormAttachment( 0, 10 );
data.top = new FormAttachment( 0, 10 );
canvas.setLayoutData( data );
canvas.addPaintListener( this );
//Inserting the allready existing Robotview into this view to show additional information
//Need to create class like that to let Eclipse Injection now where to inject values
RobotView robotView = ContextInjectionFactory.make( RobotView.class, context );
}
As you can see the ContextInjectionFactory is used to create the seconds half of my view. The right composite gets injected in the RobotView so judging by the layout everything is fine (maybe at this point someone can explain to me how the InjectionFactory knows what composite to inject at what point - because I was expecting that the wrong Composite would have been injected there).
Now if I close my app and reopen it AND the ViewB was selected, so it's the one that is visible from the very beginning then everything works and the events in the injected RobotView that I created using the Factory get updated.
If however ViewA is visible and I switch tabs to ViewB in my stack the events don't get updated. However the initial event get injected ONCE when the Factory calls make and the Class gets created the first time.
It seems like the eventBus and and the Injection machanisms miss my created RobotView when I create it through the Factory.
I am sorry for the redundant information - I reread my earlier posts and realized that I did not really make myself too clear there. If there is any additional information needed I can provide it.
Sincerely,
Artur
|
|
|
Re: When are my (injected) POJO methods called? [message #547684 is a reply to message #547675] |
Mon, 19 July 2010 07:00   |
Eclipse User |
|
|
|
Hi,
Ideally you'd provide us a runnable snippet we can use to reproduce. If
you manage to get such a bundle, file a bugzilla so that we can take a look.
Am 19.07.10 12:41, schrieb Artur Kronenberg:
> Hi,
>
> I had a little more time to dig into the problem and the equinox (both
> util and event) are started. That still didn't solve my problem.
>
> However there is an exception to my problem.
>
> Currently I am using a stack view with two viewparts in it. Viewpart B
> uses ViewPart A within and I am creating it like that:
>
> (This is create control from viewPart B):
> @Inject
> public void createControl( final Composite parent ) {
> Composite main = new Composite( parent, SWT.NONE );
> main.setLayout( new FormLayout() );
> canvas = new Canvas( main, SWT.BORDER | SWT.DOUBLE_BUFFERED);
> FormData data = new FormData( COMPASS_RADIUS, COMPASS_RADIUS );
> data.left = new FormAttachment( 0, 10 );
> data.top = new FormAttachment( 0, 10 );
> canvas.setLayoutData( data );
> canvas.addPaintListener( this );
> //Inserting the allready existing Robotview into this view to show
> additional information
> //Need to create class like that to let Eclipse Injection now where
> to inject values
> RobotView robotView = ContextInjectionFactory.make( RobotView.class,
> context );
> }
>
> As you can see the ContextInjectionFactory is used to create the seconds
> half of my view. The right composite gets injected in the RobotView so
> judging by the layout everything is fine (maybe at this point someone
> can explain to me how the InjectionFactory knows what composite to
> inject at what point - because I was expecting that the wrong Composite
> would have been injected there).
Where's the context variable defined? It it is declared by
--------8<--------
@Inject
private IEclipseContext content;
--------8<--------
Then what you'll get as the Composite is the same you get in
--------8<--------
@Inject
public void createControl( final Composite parent ) {
}
--------8<--------
All in all I really have problems understanding what you are doing. Why
does ViewPartA use ViewPartB?
So please file a bug and provide a runnable example.
>
> Now if I close my app and reopen it AND the ViewB was selected, so it's
> the one that is visible from the very beginning then everything works
> and the events in the injected RobotView that I created using the
> Factory get updated.
> If however ViewA is visible and I switch tabs to ViewB in my stack the
> events don't get updated. However the initial event get injected ONCE
> when the Factory calls make and the Class gets created the first time.
>
> It seems like the eventBus and and the Injection machanisms miss my
> created RobotView when I create it through the Factory.
> I am sorry for the redundant information - I reread my earlier posts and
> realized that I did not really make myself too clear there. If there is
> any additional information needed I can provide it.
> Sincerely,
> Artur
|
|
| | |
Re: When are my (injected) POJO methods called? [message #577795 is a reply to message #540492] |
Wed, 16 June 2010 08:03  |
Eclipse User |
|
|
|
1- how are you creating your "MyClass" object ?
If you are creating your object like this : new MyClass()
It won't work, because "the dependency injection engine won't be called". The @Inject means that the dependency injection engine should do the job so you should try :
IEclipseContext context = EclipseContextFactory.create();
MyClass myClass = ContextInjectionFactory.make(MyClass.class,context);
This time all your @Inject annotations should work. When the platform is creating your object (in the case of a part for example) I'm sure it's done this way.
2- Execute and CanExecute are annotations for Handlers only. For example, a "SaveHandler" class which save the content of the current active part could look like this :
@CanExecute
boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart part) {
return part.isDirty();
}
@Execute
void execute(EPartService partService, @Named(IServiceConstants.ACTIVE_PART) MPart part) {
partService.savePart(part, false);
}
--
Stephane Begaudeau @ Obeo
|
|
|
Re: When are my (injected) POJO methods called? [message #577807 is a reply to message #540492] |
Wed, 16 June 2010 08:03  |
Eclipse User |
|
|
|
Hi,
I'm not sure what you are trying to do. What are you trying to display
when the window is opened? Do you contribute a Part? Do you open another
Window with informations?
e4 doesn't use extension points in the 3.x sense to contribute parts
(Views/Editors) but uses Model-Fragments to contribute to the
application which is backing the whole e4-Application-Instance.
My current guess is that you want to contribute to the addon-Feature of
the application model but I'm not sure yet.
Beside that: We've reworked the contribution system which was available
in M6 and you should use the latest I-Build.
Tom
Am 16.06.10 13:50, schrieb karins.spam@gmail.com:
> Hello Everyone,
>
> I am trying to write an RCP where I need to get the shell of the main
> application window AFTER it has been created. In 3.x I would override
> the WorkbenchWindowAdvisor .postWindowCreate() method, but as far as I
> could tell this is not used in e4.
>
> I tried to write a class (which is not a part, since all parts already
> have been created and should be children of the shell I get from
> shellProvider....) like this:
>
> public class MyClass {
>
> @Inject
> private IShellProvider shellProvider;
>
> @PostConstruct
> private Object myMethod() {
> System.out.println("Do something using the shell! ");
> return shellProvider.getShell();
> }
>
> }
>
>
> but myMethod() does not get called. I also tried to use @Execute and
> @CanExecute but they don´t help. (Is there a good page that explains
> exactly what these annotations do, btw?)
> Do I need to register MyClass through en extension point? Which one, and
> with what attributes?
> I am using e4 M6.
>
> Karin
|
|
| |
Re: When are my (injected) POJO methods called? [message #577894 is a reply to message #540496] |
Thu, 17 June 2010 03:18  |
Eclipse User |
|
|
|
Thanks for your answers. I guess I should explain my question better.
In eclipse 3.x I would have used the org.eclipse.core.runtime.applications extenstion point to register an Application class:
public class Application implements IApplication {
public Object start(IApplicationContext context) {
...
PlatformUI.createAndRunWorkbench(display, new ApplicationWorkbenchAdvisor());
...
}
...
}
And then define the ApplicationWorkbenchAdvisor, ApplicationWorkbenchWindowAdvisor classes as follows:
public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
IWorkbenchWindowConfigurer configurer) {
return new ApplicationWorkbenchWindowAdvisor(configurer);
}
...
}
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
...
@Override
public void postWindowCreate() {
// Do what I want
}
}
In the postWindowCreate Method I could then do what I want. But how do I get to this point in the application lifecycle in eclipse e4?
(I have already defined my own renderers and registered parts using Application.e4.xmi and components.xmi, so the window exists. The application runs fine as it is - the window is created and displayed as it should. But I would prefer if I could do something else to it before it is displayed. In the renderer code it is too early 'cause ALL widgets have not been created yet... )
Many thanks,
Karin
|
|
|
Re: When are my (injected) POJO methods called? [message #577922 is a reply to message #540712] |
Thu, 17 June 2010 08:10  |
Eclipse User |
|
|
|
You still didn't answer what you are trying to do in the "postWindowCreate".
I guess the main problem is that you are not able to plug yourself into
the EventBus without doing a contribution.
I guess the Application#addons could be used for this because they allow
you to register Contribution who are not creating UI-Structures.
You can take a look at the workbench.addons.swt who do exactly this kind
of thing to implement e.g. DnD, ... .
Tom
Am 17.06.10 09:18, schrieb karins.spam@gmail.com:
> Thanks for your answers. I guess I should explain my question better.
> In eclipse 3.x I would have used the
> org.eclipse.core.runtime.applications extenstion point to register an
> Application class:
>
> public class Application implements IApplication {
>
> public Object start(IApplicationContext context) {
> ..
> PlatformUI.createAndRunWorkbench(display, new
> ApplicationWorkbenchAdvisor());
> ..
> }
> ..
> }
>
> And then define the ApplicationWorkbenchAdvisor,
> ApplicationWorkbenchWindowAdvisor classes as follows:
>
> public class ApplicationWorkbenchAdvisor extends WorkbenchAdvisor {
>
> public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor(
> IWorkbenchWindowConfigurer configurer) {
> return new ApplicationWorkbenchWindowAdvisor(configurer);
> }
>
> ..
> }
>
> public class ApplicationWorkbenchWindowAdvisor extends
> WorkbenchWindowAdvisor {
> ..
> @Override
> public void postWindowCreate() {
> // Do what I want
> }
> }
>
> In the postWindowCreate Method I could then do what I want. But how do I
> get to this point in the application lifecycle in eclipse e4?
> (I have already defined my own renderers and registered parts using
> Application.e4.xmi and components.xmi, so the window exists. The
> application runs fine as it is - the window is created and displayed as
> it should. But I would prefer if I could do something else to it before
> it is displayed. In the renderer code it is too early 'cause ALL widgets
> have not been created yet... )
>
> Many thanks,
> Karin
|
|
|
Re: When are my (injected) POJO methods called? [message #578182 is a reply to message #540812] |
Mon, 28 June 2010 07:03  |
Eclipse User |
|
|
|
Hi Tom,
I am trying to do a similar thing and have problems with the Event bus:
I have a view that is created normally. I wrote this view so I can reuse the part by basically creating a new Instance of that view and using a different composite to be injected. This works so far.
I have the IEclipseContext injected to my second view and have The Factory create the first. It automatically injects the right composite into my first view so now both views are in the same view. (I hope this is understandable) :)
Now view No. 1 subscribes to an event topic and if it is created by Eclipse at startup (it is registered with the application.e4xmi) then the events are send correctly to that view.
However, in my second view, where I added View No 1 manually, the events aren't beeing send to.
I looked into the bundle you suggested (workbench.addons.swt) and saw that you are having the EventBroker injected and manually register for Event Topics.
However my viewclass is not an instance of the EventHandler.
So my question is:
How to manually create a view, that gets events that are registered with the @EventTopic annotation?
Kind regards,
Artur
|
|
|
Re: When are my (injected) POJO methods called? [message #578201 is a reply to message #578182] |
Mon, 28 June 2010 12:24  |
Eclipse User |
|
|
|
If I get you correct you are creating the instance your own which won't
work. You need to create your objects through DI
(ContextContributionFactory) but I've not used the event system my own
so I could be wrong.
Tom
Am 28.06.10 13:03, schrieb Artur Kronenberg:
> Hi Tom,
>
> I am trying to do a similar thing and have problems with the Event bus:
>
> I have a view that is created normally. I wrote this view so I can reuse
> the part by basically creating a new Instance of that view and using a
> different composite to be injected. This works so far.
>
> I have the IEclipseContext injected to my second view and have The
> Factory create the first. It automatically injects the right composite
> into my first view so now both views are in the same view. (I hope this
> is understandable) :)
> Now view No. 1 subscribes to an event topic and if it is created by
> Eclipse at startup (it is registered with the application.e4xmi) then
> the events are send correctly to that view.
>
> However, in my second view, where I added View No 1 manually, the events
> aren't beeing send to.
> I looked into the bundle you suggested (workbench.addons.swt) and saw
> that you are having the EventBroker injected and manually register for
> Event Topics.
>
> However my viewclass is not an instance of the EventHandler.
> So my question is:
>
> How to manually create a view, that gets events that are registered with
> the @EventTopic annotation?
>
> Kind regards,
> Artur
|
|
|
Re: When are my (injected) POJO methods called? [message #578225 is a reply to message #543194] |
Mon, 28 June 2010 14:39  |
Eclipse User |
|
|
|
Hi,
Do you mean the ContextInjectionFactory? - Cause I can not find the ContextContributionFactory.
However, ContextInjectionFactory doesn't work in my case.
It injects all the values I need and my class get constructed the right way, but the methods that are supposed to listen to the eventbus using the @EventTopic annotation don't get registered with the EventBroker it seems.
This method isn't called after the initial injection anymore.
Tom Schindl wrote on Mon, 28 June 2010 12:24
> If I get you correct you are creating the instance your own which won't
> work. You need to create your objects through DI
> (ContextContributionFactory) but I've not used the event system my own
> so I could be wrong.
>
> Tom
|
|
|
Re: When are my (injected) POJO methods called? [message #578246 is a reply to message #543237] |
Mon, 28 June 2010 15:32  |
Eclipse User |
|
|
|
Am 28.06.10 20:39, schrieb Artur Kronenberg:
> Hi,
>
> Do you mean the ContextInjectionFactory? - Cause I can not find the
> ContextContributionFactory.
Sure yes. Looking at the addon code it does exactly this (well it goes
through the reflection-factory which internally simply uses the
ContextInjectionFactory)
Tom
|
|
|
Re: When are my (injected) POJO methods called? [message #578274 is a reply to message #543252] |
Tue, 29 June 2010 14:02  |
Eclipse User |
|
|
|
Tom Schindl wrote on Mon, 28 June 2010 15:32
> Sure yes. Looking at the addon code it does exactly this (well it goes
> through the reflection-factory which internally simply uses the
> ContextInjectionFactory)
>
> Tom
Hey,
yep that was what I used. Injection is working, but the Annotations that are suppose to listen for the EventBus aren't called.
I was wondering if there was something else I have to do to maybe manually my annotations to the EventBus.
Sincerely,
-- artur
|
|
|
Re: When are my (injected) POJO methods called? [message #578286 is a reply to message #543548] |
Tue, 29 June 2010 14:48  |
Eclipse User |
|
|
|
See InjectionEventTest in the "org.eclipse.e4.core.tests" bundles. One thing
to check is to make sure that the "org.eclipse.equinox.event" bundle is
started. (See InjectionEventTest#ensureEventAdminStarted())
Sincerely,
Oleg Besedin
"Artur Kronenberg" <addur737@yahoo.de> wrote in message
news:i0dcf1$qs$1@build.eclipse.org...
> Tom Schindl wrote on Mon, 28 June 2010 15:32
>> Sure yes. Looking at the addon code it does exactly this (well it goes
>> through the reflection-factory which internally simply uses the
>> ContextInjectionFactory)
>>
>> Tom
>
>
> Hey,
>
> yep that was what I used. Injection is working, but the Annotations that
> are suppose to listen for the EventBus aren't called.
> I was wondering if there was something else I have to do to maybe manually
> my annotations to the EventBus.
>
> Sincerely,
> -- artur
|
|
|
Re: When are my (injected) POJO methods called? [message #578320 is a reply to message #543561] |
Wed, 30 June 2010 05:01  |
Eclipse User |
|
|
|
Oleg Besedin a écrit :
> See InjectionEventTest in the "org.eclipse.e4.core.tests" bundles. One thing
> to check is to make sure that the "org.eclipse.equinox.event" bundle is
> started. (See InjectionEventTest#ensureEventAdminStarted())
Also, that annotation is defined in org.eclipse.e4.core.di.extensions
(using declarative services), so you have to make sure this bundle and
equinox.ds / equinox.util are in your launch configuration (equinox.ds
must be @start with a runlevel lower than the default).
HTH,
Simon
|
|
|
Re: When are my (injected) POJO methods called? [message #578854 is a reply to message #543666] |
Mon, 19 July 2010 06:41  |
Eclipse User |
|
|
|
Hi,
I had a little more time to dig into the problem and the equinox (both util and event) are started. That still didn't solve my problem.
However there is an exception to my problem.
Currently I am using a stack view with two viewparts in it. Viewpart B uses ViewPart A within and I am creating it like that:
(This is create control from viewPart B):
@Inject
public void createControl( final Composite parent ) {
Composite main = new Composite( parent, SWT.NONE );
main.setLayout( new FormLayout() );
canvas = new Canvas( main, SWT.BORDER | SWT.DOUBLE_BUFFERED);
FormData data = new FormData( COMPASS_RADIUS, COMPASS_RADIUS );
data.left = new FormAttachment( 0, 10 );
data.top = new FormAttachment( 0, 10 );
canvas.setLayoutData( data );
canvas.addPaintListener( this );
//Inserting the allready existing Robotview into this view to show additional information
//Need to create class like that to let Eclipse Injection now where to inject values
RobotView robotView = ContextInjectionFactory.make( RobotView.class, context );
}
As you can see the ContextInjectionFactory is used to create the seconds half of my view. The right composite gets injected in the RobotView so judging by the layout everything is fine (maybe at this point someone can explain to me how the InjectionFactory knows what composite to inject at what point - because I was expecting that the wrong Composite would have been injected there).
Now if I close my app and reopen it AND the ViewB was selected, so it's the one that is visible from the very beginning then everything works and the events in the injected RobotView that I created using the Factory get updated.
If however ViewA is visible and I switch tabs to ViewB in my stack the events don't get updated. However the initial event get injected ONCE when the Factory calls make and the Class gets created the first time.
It seems like the eventBus and and the Injection machanisms miss my created RobotView when I create it through the Factory.
I am sorry for the redundant information - I reread my earlier posts and realized that I did not really make myself too clear there. If there is any additional information needed I can provide it.
Sincerely,
Artur
|
|
|
Re: When are my (injected) POJO methods called? [message #578863 is a reply to message #578854] |
Mon, 19 July 2010 07:00  |
Eclipse User |
|
|
|
Hi,
Ideally you'd provide us a runnable snippet we can use to reproduce. If
you manage to get such a bundle, file a bugzilla so that we can take a look.
Am 19.07.10 12:41, schrieb Artur Kronenberg:
> Hi,
>
> I had a little more time to dig into the problem and the equinox (both
> util and event) are started. That still didn't solve my problem.
>
> However there is an exception to my problem.
>
> Currently I am using a stack view with two viewparts in it. Viewpart B
> uses ViewPart A within and I am creating it like that:
>
> (This is create control from viewPart B):
> @Inject
> public void createControl( final Composite parent ) {
> Composite main = new Composite( parent, SWT.NONE );
> main.setLayout( new FormLayout() );
> canvas = new Canvas( main, SWT.BORDER | SWT.DOUBLE_BUFFERED);
> FormData data = new FormData( COMPASS_RADIUS, COMPASS_RADIUS );
> data.left = new FormAttachment( 0, 10 );
> data.top = new FormAttachment( 0, 10 );
> canvas.setLayoutData( data );
> canvas.addPaintListener( this );
> //Inserting the allready existing Robotview into this view to show
> additional information
> //Need to create class like that to let Eclipse Injection now where
> to inject values
> RobotView robotView = ContextInjectionFactory.make( RobotView.class,
> context );
> }
>
> As you can see the ContextInjectionFactory is used to create the seconds
> half of my view. The right composite gets injected in the RobotView so
> judging by the layout everything is fine (maybe at this point someone
> can explain to me how the InjectionFactory knows what composite to
> inject at what point - because I was expecting that the wrong Composite
> would have been injected there).
Where's the context variable defined? It it is declared by
--------8<--------
@Inject
private IEclipseContext content;
--------8<--------
Then what you'll get as the Composite is the same you get in
--------8<--------
@Inject
public void createControl( final Composite parent ) {
}
--------8<--------
All in all I really have problems understanding what you are doing. Why
does ViewPartA use ViewPartB?
So please file a bug and provide a runnable example.
>
> Now if I close my app and reopen it AND the ViewB was selected, so it's
> the one that is visible from the very beginning then everything works
> and the events in the injected RobotView that I created using the
> Factory get updated.
> If however ViewA is visible and I switch tabs to ViewB in my stack the
> events don't get updated. However the initial event get injected ONCE
> when the Factory calls make and the Class gets created the first time.
>
> It seems like the eventBus and and the Injection machanisms miss my
> created RobotView when I create it through the Factory.
> I am sorry for the redundant information - I reread my earlier posts and
> realized that I did not really make myself too clear there. If there is
> any additional information needed I can provide it.
> Sincerely,
> Artur
|
|
|
Re: When are my (injected) POJO methods called? [message #581350 is a reply to message #540492] |
Tue, 31 August 2010 08:26  |
Eclipse User |
|
|
|
I have another questions concerning this topic. As you told, DI is only executed when the DI engine is called.
But when is the DI engine called? Only for classes that are defined within an e4 Application Model?
In my case, I have a Navigator View using the Common Navigation Framework, where I have to specify a custom ContentProvider. Within this ContentProvider, I need a service that I have declared within another plugin. But the instance of the ContentProvider is created by the Extension Registry. So the service is not injected. I can call the DI engine by myself by invoking
final IEclipseContext ctx = EclipseContextFactory.getServiceContext( FrameworkUtil.getBundle( getClass() ).getBundleContext() );
ContextInjectionFactory.inject( this, ctx );
But this context does not know any org.eclipse.e4.core.services.log.Logger, so I get the error when I do not declare this injected value as optional.
In summary, it is not possible to use DI for the whole application, right?
|
|
| |
Goto Forum:
Current Time: Wed Jul 23 13:46:10 EDT 2025
Powered by FUDForum. Page generated in 0.73518 seconds
|