Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse 4 » Dynamically creating Parts (updated from the E4/Snippets) / Passing around IEclipseContext
Dynamically creating Parts (updated from the E4/Snippets) / Passing around IEclipseContext [message #636111] Fri, 29 October 2010 10:57 Go to next message
Ned Twigg is currently offline Ned TwiggFriend
Messages: 3
Registered: October 2010
Junior Member
First off, I'm a huge fan of SWT and JFace, but until now I had shied away from Eclipse RCP because I thought it made component testing too hard. Dependency injection on POJOs has totally fixed that for me, and I'm really excited to bring Eclipse 4 into my work. Really looking forward to the first 4 book.

I have a very simple Application.e4xmi, which has only the following insertions:
-an Addon implemented by MyAddon
-a Toolbar button whose Handler is implemented by OpenHandler
-a Trimmed Window whose only Control is a PartStack whose ID is "partstack"

I am trying to programmatically open a new Part in the PartStack every time the open button is pressed. I have it working, but I know that I am misusing parts of the API.

First off, I was having problems passing IEclipseContext around. To fix this, I created MyAddon, whose sole purpose is to get a static reference to the top level IEclipseContext. I know I should inject the IEclipseContext where it is used, but in more complex stuff that I have tried I have found myself passing the IEclipseContext all over the place, so I made this instead. Is this passable, or a terrible mistake?

public class MyAddon {
	@Inject
	private	IEclipseContext context;
			
	@PostConstruct
	void establishStaticContext() {
		staticContext = context;
	}
	
	private static IEclipseContext staticContext;
	
	public static IEclipseContext getContext(){		
		return staticContext;
	}
}


Then, in my OpenHandler, I do the following:

public class OpenHandler{
	@Execute
	public void execute(){
		IEclipseContext context = MyAddon.getContext(); 
		IPresentationEngine renderer = (IPresentationEngine) context.get(IPresentationEngine.class.getName());

		MPart part = MBasicFactory.INSTANCE.createPart();
		part.setLabel("New Part");
		part.setContributionURI("platform:/plugin/com.twigg.e4test/com.twigg.e4test.TestPart");
				

		//This is what I thought I should do, but it returns null
		//MUIElement partStack = (MUIElement) context.get("partstack");				
		
		//I know this is not right, but it works...
		MApplication app = (MApplication) context.get(MApplication.class.getName());
		MUIElement partStack = app.getChildren().get(0).getChildren().get(0);
		part.setParent((MElementContainer<MUIElement>) partStack);
		
		renderer.createGui(part);
		 
		CTabFolder tabs = (CTabFolder) ((MPartStack) partStack).getWidget();
		tabs.setSelection(tabs.getItemCount()-1);
	}
}


Why can't I find the "partstack" object by asking for it directly from the IEclipseContext? I know the e4 way would be to do something like

@Inject
@Named("partstack")
private MPartStack partStack;


but I'd like to do something more programmatic than that...

Thanks!
-Ned

[Updated on: Fri, 29 October 2010 11:00]

Report message to a moderator

Re: Dynamically creating Parts (updated from the E4/Snippets) / Passing around IEclipseContext [message #636147 is a reply to message #636111] Fri, 29 October 2010 13:21 Go to previous messageGo to next message
Remy Suen is currently offline Remy SuenFriend
Messages: 462
Registered: July 2009
Senior Member
Ned Twigg wrote on Fri, 29 October 2010 06:57
I am trying to programmatically open a new Part in the PartStack every time the open button is pressed. I have it working, but I know that I am misusing parts of the API.

Ideally, you should be using the EPartService if you want to perform part-related functions.

Quote:
First off, I was having problems passing IEclipseContext around. To fix this, I created MyAddon, whose sole purpose is to get a static reference to the top level IEclipseContext. I know I should inject the IEclipseContext where it is used, but in more complex stuff that I have tried I have found myself passing the IEclipseContext all over the place, so I made this instead. Is this passable, or a terrible mistake?

Your way would only provide you with some arbitrary context. You may not necessarily have information local to what you actually care about, say, the active part, or the active window.

Quote:
Why can't I find the "partstack" object by asking for it directly from the IEclipseContext?

Stuff is only in the context if someone actually put something in there. Did someone actually call context.set("partStack", partStack)? I am guessing no.
Re: Dynamically creating Parts (updated from the E4/Snippets) / Passing around IEclipseContext [message #636488 is a reply to message #636111] Mon, 01 November 2010 14:41 Go to previous message
Lars Vogel is currently offline Lars VogelFriend
Messages: 1098
Registered: July 2009
Senior Member

Hi Ned,

I think this handler implementation should work for you:

@Execute
public void execute(EPartService partService, MApplication application,
EModelService modelService) {
// MPart createPart = partService.createPart("newpart");

MPart part = MBasicFactory.INSTANCE.createPart();
part.setLabel("New Part");

part.setContributionURI(" platform:/plugin/de.vogella.e4.modelservice/de.vogella.e4.mo delservice.part.NewPart ");
List<MPartStack> stacks = modelService.findElements(application, null,
MPartStack.class, null);
stacks.get(0).getChildren().add(part);
partService.showPart(part, PartState.ACTIVATE);
}

@Remy: I hope you approve. :-)

Best regards, Lars
Previous Topic:Prototype of Eclipse 3 plug-ins reverse engineering
Next Topic:XWT: DataContext change --> Change UI
Goto Forum:
  


Current Time: Fri Mar 29 04:54:27 GMT 2024

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

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

Back to the top