Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

E4/Snippets

Here you can find snippets and practical tips covering Eclipse e4.

Parts

Open a part dynamically

Define a part descriptor and open the part with the help of the EPartService.

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

<?xml version="1.0" encoding="ASCII"?>
<application:ModelComponents 
	xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:application="http://www.eclipse.org/ui/2008/UIModel"
	xsi:schemaLocation="http://www.eclipse.org/ui/2008/UIModel ../../org.eclipse.e4.ui.model.workbench/model/UIElements.ecore " xmi:id="abc">
		<components 
			parentID="sampleApp">
				<descriptors 
					URI="platform:/plugin/de.sampleapp.data.ui/de.sampleapp.data.ui.TestView "
					label="TestView"
					category="viewStack"/>
		</components>
</application:ModelComponents>

Register the components.e4xmi definition through an extension.

<extension 
	id="SampleApp"
	name="SampleApp"
	point="org.eclipse.e4.workbench.model">
		<snippet
			uri="xmi/components.e4xmi">
		</snippet>
</extension>

Get the part descriptor at runtime and open it. For this step you need the IEclipseContext. You can get it either through dependency injection or passing it from another class. You can also try to get the MApplication through dependency injection, but in my case that didn't worked. This is lso the right point to set an input for the part.

MWindow window = (MWindow) context.get( EPartService.PART_SERVICE_ROOT );
MApplication app = (MApplication) (MUIElement) window.getParent();
EList<MPartDescriptor> descriptorList = app.getDescriptors();
for( MPartDescriptor descriptorList : desc ){
	if( partDesc.getId().equals( PartToOpen.ID ) ){
		MPart part = servicePart.showPart( partDesc.getId() );
                break;
        }
}

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.

<variables>input</variables>

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

@Inject
@Optional
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.

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

Back to the top