Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Eclipse 4 RCP getting parts via EPartService
Eclipse 4 RCP getting parts via EPartService [message #989891] Sun, 09 December 2012 10:27 Go to next message
Daniel Stingl is currently offline Daniel Stingl
Messages: 11
Registered: November 2012
Junior Member
Greetings,
I have another problem with my RCP Project. I am trying to hide/show some parts, and I tried to find those parts by calling EPartService.findPart("partID").

package fzi.activemq.rcp.launcher.Composites.lowerleft;

import javax.inject.Inject;

import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.workbench.UIEvents.Part;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;

import fzi.activemq.rcp.launcher.Composites.MainComposite;

public class SelectComposite2 extends Composite {

	@Inject private EPartService service;
	
	Button aMQButton;
	Button dBButton;
	
	public SelectComposite2(Composite parent, int style){
		super(parent, style);
				
		
		MPart part = service.findPart("fzi.activemq.rcp.launcher.part.2");
		part.setVisible(true);
...





When findPart gets called, I get this NullPointerException:


java.lang.NullPointerException
	at fzi.activemq.rcp.launcher.ui.parts.LowerLeft.configurationSelected(LowerLeft.java:40)
	at fzi.activemq.rcp.launcher.Composites.upperleft.SelectComposite$1.widgetSelected(SelectComposite.java:45)
	at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
	at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
	at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1029)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
	at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:923)
	at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
	at org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java:150)
	at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
	at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
	at org.eclipse.equinox.launcher.Main.main(Main.java:1414)


Would be great if u could help me out.
thx,
Daniel
Re: Eclipse 4 RCP getting parts via EPartService [message #989931 is a reply to message #989891] Mon, 10 December 2012 02:05 Go to previous messageGo to next message
sumit singh is currently offline sumit singh
Messages: 121
Registered: October 2012
Location: Bangalore
Senior Member

The problem is
@Inject private EPartService service;
is not injected .

Why..? Because you are creating object of your SelectComposite2 class so its your responsibility to inject all necessary injection to your class. Like following.
SelectComposite2 cim = new SelectComposite2();
ContextInjectionFactory.inject(cim,iEclipseContext);


Where iEclipseContext is IEclipseContext variable. IEclipseContext automatically inject when eclipse framework will create your object . Like in case of handler.
But if you are creating your object then its your responsibility to inject it.

[Updated on: Mon, 10 December 2012 02:06]

Report message to a moderator

Re: Eclipse 4 RCP getting parts via EPartService [message #990002 is a reply to message #989931] Mon, 10 December 2012 07:23 Go to previous messageGo to next message
Daniel Stingl is currently offline Daniel Stingl
Messages: 11
Registered: November 2012
Junior Member
Thx for your reply. Is it possible to use injection similar in Listeners as in Handlers (it didnt work when I tried it but I wouldnt be that surprised if I messed that up..)?
Otherwise, how can i get my hands on an instance of IEclipseContext? Rly tried to figure that out myselve, but no matter how many sites I browse, it seems that this is only used with injection...

[Updated on: Mon, 10 December 2012 10:02]

Report message to a moderator

Re: Eclipse 4 RCP getting parts via EPartService [message #990374 is a reply to message #990002] Wed, 12 December 2012 03:35 Go to previous messageGo to next message
sumit singh is currently offline sumit singh
Messages: 121
Registered: October 2012
Location: Bangalore
Senior Member

No. In case of listeners you have to do it your self. take a look of following code . you can get your EPartService like following in your class.

Bundle bundle = FrameworkUtil.getBundle(EPartService.class);
BundleContext bundleContext = bundle.getBundleContext();
IEclipseContext eclipseCtx =
EclipseContextFactory.getServiceContext(bundleContext);
EPartService  partService  = (EPartService) eclipseCtx.get(EPartService.class.getName());

[Updated on: Wed, 12 December 2012 03:36]

Report message to a moderator

Re: Eclipse 4 RCP getting parts via EPartService [message #990437 is a reply to message #990374] Wed, 12 December 2012 08:55 Go to previous messageGo to next message
Daniel Stingl is currently offline Daniel Stingl
Messages: 11
Registered: November 2012
Junior Member
Thank u so much, everything is working fine now!
Re: Eclipse 4 RCP getting parts via EPartService [message #990552 is a reply to message #990437] Thu, 13 December 2012 00:45 Go to previous messageGo to next message
sumit singh is currently offline sumit singh
Messages: 121
Registered: October 2012
Location: Bangalore
Senior Member

Your Welcome Smile
Re: Eclipse 4 RCP getting parts via EPartService [message #990955 is a reply to message #990552] Sat, 15 December 2012 06:39 Go to previous messageGo to next message
Brian de Alwis is currently offline Brian de Alwis
Messages: 127
Registered: January 2012
Senior Member

Just to elaborate on Sumit's answer -- you need to obtain a context from somewhere. Sumit's code uses the top-level OSGi context, which works in this case as the EPartService is effectively a singleton registered as an OSGi service. But you'll find that this won't work for other services as they will be registered at other points in the context tree. Generally you should do lookups from the closest context that you can find, and many of the UI classes carry a context (see the subclasses of MContext). In Eclipse 3.x compat layer, you can obtain the IEC for a part using:

getSite().getService(IEclipseContext.class);


Brian.
Re: Eclipse 4 RCP getting parts via EPartService [message #991020 is a reply to message #989891] Mon, 17 December 2012 00:17 Go to previous message
sumit singh is currently offline sumit singh
Messages: 121
Registered: October 2012
Location: Bangalore
Senior Member

Thanks Brian to make it more clear.
Previous Topic:Disable text search
Next Topic:Error launching Eclipse Application from Vogella Tutorial
Goto Forum:
  


Current Time: Fri Apr 18 19:21:46 EDT 2014

Powered by FUDForum. Page generated in 0.01720 seconds