Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Injecting selected project into view
Injecting selected project into view [message #1332832] Sun, 04 May 2014 21:16 Go to next message
Grzegorz Abramczyk is currently offline Grzegorz AbramczykFriend
Messages: 10
Registered: April 2011
Junior Member
Hi I'm trying to get my view injected with reference to project selected in project explorer and I'm always getting null injected.

My code is

@Inject
public void setProject(@Optional @Active @Named(IServiceConstants.ACTIVE_SELECTION) final IProject project)throws CoreException {
	if (project != null) {
	this.viewer.setInput(this.projectStoreService.getConnection(project));
	}
}


I traced problem to part of org.eclipse.e4.core.internal.di.InjectorImpl
if (!descriptorsClass.isAssignableFrom(actualArgs[i].getClass()))
    actualArgs[i] = IInjector.NOT_A_VALUE;

Where selection is TreeSelection containing only one element, but even if it is project the check fails and I get null.

Is there an elegant way to get type safe injection like I'm trying to get or should I simply inject IStructuredSelection and decide if it is project in my code?

I'm using Eclipse PDE 4.3.2 with e4 tools bridge (my part is wrapped with DIViewPart).

[Updated on: Sun, 04 May 2014 21:16]

Report message to a moderator

Re: @Inject @Named(IServiceConstants.ACTIVE_SELECTION) IProject [message #1333732 is a reply to message #1332832] Mon, 05 May 2014 07:41 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi,

If the value pushed is a TreeSelection you need to get ISelection
injected. Please also note that it looks like you didn't not really
understand the diff between @Active & IServiceConstants.ACTIVE_SELECTION.

See http://tomsondev.bestsolution.at/2013/01/30/active-in-e4/

The only other option I see is to implement an ExtendedObjectSupplier
who is able to convert the type into your target e.g. using the adapter
system of eclipse.

Tom

On 04.05.14 23:16, Grzegorz Abramczyk wrote:
> Hi I'm trying to get my view injected with reference to project selected
> in project explorer and I'm always getting null injected.
>
> My code is
> @Inject
> public void setProject(@Optional @Active
> @Named(IServiceConstants.ACTIVE_SELECTION) final IProject project)throws
> CoreException {
> if (project != null) {
> this.viewer.setInput(this.projectStoreService.getConnection(project));
> }
> }
>
>
> I traced problem to part of org.eclipse.e4.core.internal.di.InjectorImpl
>
> if (!descriptorsClass.isAssignableFrom(actualArgs[i].getClass()))
> actualArgs[i] = IInjector.NOT_A_VALUE;
>
> Where selection is TreeSelection containing only one element, but even
> if it is project the check fails and I get null.
>
> Is there an elegant way to get type safe injection like I'm trying to
> get or should I simply inject IStructuredSelection and decide if it is
> project in my code?
>
> I'm using Eclipse PDE 4.3.2 with e4 tools bridge (my part is wrapped
> with DIViewPart).
Re: @Inject @Named(IServiceConstants.ACTIVE_SELECTION) IProject [message #1334895 is a reply to message #1333732] Mon, 05 May 2014 20:10 Go to previous messageGo to next message
Grzegorz Abramczyk is currently offline Grzegorz AbramczykFriend
Messages: 10
Registered: April 2011
Junior Member
Thanks. The @Active was blind shoot after I spotted it influences the way the injection is resolved.

I settled for injecting IStructuredSelection. This code did the work:

	@Inject
	public void setSelection(
			@Optional @Named(IServiceConstants.ACTIVE_SELECTION) final IStructuredSelection selection)
			throws CoreException {
		if (this.viewer != null && selection != null && !selection.isEmpty()) {
			final Object project = selection.getFirstElement();
			if (project instanceof IProject) {
				this.viewer.setInput(this.projectStoreService
						.getConnection((IProject) project));
			}
		}
	}
Re: @Inject @Named(IServiceConstants.ACTIVE_SELECTION) IProject [message #1336101 is a reply to message #1334895] Tue, 06 May 2014 08:26 Go to previous message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi,

After having thought about your question I've came up with a generic
solution to your problem - this solution is not part of e4 itself but
available from the e(fx)clipse project but does not depend upon JavaFX
so it can be used in any e4 DI application.

What I did was to define an @Adapt annotation and contribute an
ExtendObjectSupplier who is able to consult an adapter system to convert
the value in the DI-Context into the target type.

You can write now:

@Inject
public void setSelection(@Optional @Adapt
@Named(IServiceConstants.ACTIVE_SELECTION) IProject project)

If the adapter system has a way to convert from IStructuredSelection =>
IProject

You can read more about the features implemented in e(fx)clipse when it
comes to adapters and DI extensions at
https://wiki.eclipse.org/Efxclipse/Runtime/Recipes

Tom

On 05.05.14 22:10, Grzegorz Abramczyk wrote:
> Thanks. The @Active was blind shoot after I spotted it influences the
> way the injection is resolved.
>
> I settled for injecting IStructuredSelection. This code did the work:
>
>
> @Inject
> public void setSelection(
> @Optional @Named(IServiceConstants.ACTIVE_SELECTION) final
> IStructuredSelection selection)
> throws CoreException {
> if (this.viewer != null && selection != null &&
> !selection.isEmpty()) {
> final Object project = selection.getFirstElement();
> if (project instanceof IProject) {
> this.viewer.setInput(this.projectStoreService
> .getConnection((IProject) project));
> }
> }
> }
>
Previous Topic:Cannot create a part at APP_STARTUP_COMPLETE in Luna M6
Next Topic:Installation Problem Eclipse e4 Tools Bridge for 3.x
Goto Forum:
  


Current Time: Fri Apr 19 20:01:55 GMT 2024

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

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

Back to the top