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

< E4
Revision as of 06:56, 30 April 2010 by Laluz.gateway23.de (Talk | contribs) (Execute Command/Handler manually)


Parts

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="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>

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.

<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 desc : descriptorList){
	if( partDesc.getId().equals( PartToOpen.ID ) ){
		MPart part = servicePart.showPart( partDesc.getId() );
                break;
        }
}

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.

<variables>input</variables>

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

@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" );


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 {
 
	@Execute
	public void execute() {
		System.out.println("execute()");
	}
 
	@CanExecute
	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 )
     eventBroker.post( 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:

@Override
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( "org.eclipse.e4.data" );
 
   ...
}

Back to the top