Eclipse e4 – Creating a new part dynamically

Eclipse e4 allows to create application elements dynamically. The following is a small example how to create a part dynamically in a handler. This assumes that you have a class “de.vogella.e4.modelservice.part.NewPart” defined in plugin “de.vogella.e4.modelservice”.



	@Execute
	public void execute(EPartService partService, MApplication application,
			EModelService modelService) {
		MPart part = MBasicFactory.INSTANCE.createPart();
		part.setLabel("New Part");
		part.setContributionURI("platform:/plugin/de.vogella.e4.modelservice/de.vogella.e4.modelservice.part.NewPart");
		 List<MPartStack> stacks = modelService.findElements(application, null,
				MPartStack.class, null);
		 stacks.get(0).getChildren().add(part);
		 partService.showPart(part, PartState.ACTIVATE);
	}

The nice thing of this example is that it demonstrates how you can use the dynamic creating of application elements together with the part and the model service. Via the model service with use for a stack and this part is assigned to the first stack found.

You find more information in my Eclipse e4 tutorial.

About Lars Vogel

Lars Vogel is the founder and CEO of the vogella GmbH and works as Eclipse and Android consultant, trainer and book author. He is a regular speaker at international conferences, With more than one million visitors per month his website vogella.com is one of the central sources for Eclipse and Android programming information.
This entry was posted in Eclipse and tagged , , . Bookmark the permalink.

2 Responses to Eclipse e4 – Creating a new part dynamically

  1. Maxime Jeanmart says:

    When I see this sample, I have the feeling that e4 replaced something cumbersome by something… just as cumbersome, but different. Now it’s EMF based. Is it supposed to be good because modelling is nice?

    I’m playing the devil’s advocate,here, but I’m still not totally convinced that the new way to manage the workbench is really better.

  2. Lars Vogel says:

    @Maxime: I think the extension mechanism is indeed a bit to complex (which is not part of this blog entry). I hope that this can be simplified in the future.

Comments are closed.