Skip to main content
Jump to: navigation, search


< E4


Open a part dynamically

with a Part Descriptor

This approach defines a part descriptor and opens the part with the help of the EPartService. You must include the org.eclipse.equinox.event plugin to your run configuration.

Create a components.e4xmi file to define the part descriptor.

<?xml version="1.0" encoding="ASCII"?>
<application:ModelComponents xmi:version="2.0"
	xmlns:xmi="" xmlns:application=""
	<components xmi:id="_TqGKQExtEd-JN6r-4dl3-A" parentID="application">
		<descriptors xmi:id="_n1GVsFOZEd-e5P7xr5S20g" label="View"
			tooltip="ToolTip from XMI" elementId="elementIdDeclaredInModel"
			category="tagOfTarget" closeable="true"
			contributionURI="platform:/plugin/org.eclipse.e4.demo.View" />

Notice (since 1.0M4): The category attribute of the <descriptors> tag is used, to find the desired container for the part on creation via EPartService. The category MUST match any <tag> added to the target container (for example in Application.e4xmi) to make it possible finding the desired container, at all.

Register the components.e4xmi definition through an extension.


Open the part via elementId defined in model using the EPartService, which you can retrieve via dependency injection or via IEclipseContext.

public class ViewTest {
    private EPartService injectedPartService;
    public ViewTest() {
    public void postConstruct() {
    public void openPart(String elementIdDeclaredInModel) {
        EPartService partService = eclipseCtx.get(EPartService.class);
        MPart part = partService.showPart(elementIdDeclaredInModel);

with the MApplicationFactory

This uses the MApplicationFactory to create a part. With this method you have more control over the part at runtime and can call methods defined in the part (for example to set an input). Make sure to call the MPart.setURI() method before the MPart.setParent() method, otherwise you get a NullPointerException.

IPresentationEngine renderer = (IPresentationEngine)context.get( IPresentationEngine.class.getName() );
MApplicationFactory factory = ApplicationFactoryImpl.init();
MPart view = factory.createPart();
view.setLabel( "TestView" );
view.setURI( "platform:/plugin/de.sampleapp.client/" + ViewContainer.class.getName() );
MUIElement tableStack = ( MUIElement ) context.get( "tableStack" ); // tableStack is the id of the PartStack where the new part should be shown
view.setParent( (MElementContainer<MUIElement>) tableStack );
renderer.createGui( view );
ETabFolder w = (ETabFolder) ((MPartStack) tableStack).getWidget();
w.setSelection( w.getItemCount() - 1 );
Bundle bundle = Activator.getDefault().getBundle();
ViewContainer viewContainer = (ViewContainer)view.getObject();
ITestView testView;
testView = (ITestView)bundle.loadClass( "de.sampleapp.client.TestView" ).newInstance();
testView.setInput( partInput );
viewContainer.setViewPart( testView );

Set the input on a part at runtime

This approach uses a variable defined in the Application.e4xmi and dependency injection to pass the input to a part.

Create the variable in the Application.e4xmi.


Update: Declaration of variable in Application.e4xmi not neccessary, anymore (see here).

Define a method in the part which gets the input using dependency injection

public void setPartInput( @Named( "input" ) Object partInput ) { ... }

Set the input with the help of the IEclipseContext of the part. You need to declare the variable as modifiable to change it multiple times.

Note: we don't use this facility anymore

part.getContext().modify( "input", partInput );
part.getContext().declareModifiable( "input" );

Execute Command/Handler manually

To execute an command/handler, which is defined in application model can be invoked manually using the services ECommandService and EHandlerService. The code could look like this:

ECommandService commandService = eclipseCtx.get(ECommandService.class);
EHandlerService handlerService = eclipseCtx.get(EHandlerService.class);
ParameterizedCommand myCommand = commandService.createCommand(MY_COMMAND_ID, ARGUMENTS_MAP);
Object result = handlerService.executeHandler(myCommand);

Please notice, that the methods in the handler class must be annotated:

import org.eclipse.e4.core.di.annotations.CanExecute;
import org.eclipse.e4.core.di.annotations.Execute;
public class MyCommandHandler {
	public void execute() {
	public boolean canExecute() {
                boolean mayExecute = checkConditions();
		return true;

Event Processing

Event Broker

Send Events

Use the IEclipseContext to get the EventBroker and send the event using an event name and the event data:

IEventBroker eventBroker = (IEventBroker) context.get( IEventBroker.class.getName() );
if ( eventBroker != null ) EVENT_NAME, new String( "Update_Table" ) );

Receive Events

Register your receiver with the event:

IEventBroker eventBroker = ( IEventBroker) context.get(IEventBroker.class.getName() );
eventBroker.subscribe( EVENT_NAME, this );

Implement the EventHandler interface, to handle incoming events:

public void handleEvent( Event event ) {
   // get the event topic ( which is the EVENT_NAME in this example )
   String eventTopic = event.getTopic();
   // get the event data ( which is the String "Update_Table" in this example )
   Object eventData = event.getProperty( "" );

Back to the top