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

Difference between revisions of "E4/Snippets"

< E4
(Parts)
Line 108: Line 108:
 
part.getContext().modify( "input", partInput );
 
part.getContext().modify( "input", partInput );
 
part.getContext().declareModifiable( "input" );
 
part.getContext().declareModifiable( "input" );
 +
</source>
 +
 +
 +
= 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:
 +
 +
<source lang="java">
 +
IEventBroker eventBroker = (IEventBroker) context.get( IEventBroker.class.getName() );
 +
if ( eventBroker != null )
 +
    eventBroker.post( EVENT_NAME, new String( "Update_Table" ) );
 +
</source>
 +
 +
=== Receive Events ===
 +
 +
Register your receiver with the event:
 +
 +
<source lang="java">
 +
IEventBroker eventBroker = ( IEventBroker) context.get(IEventBroker.class.getName() );
 +
eventBroker.subscribe( EVENT_NAME, this );
 +
</source>
 +
 +
Implement the EventHandler interface, to handle incoming events:
 +
 +
<source lang="java">
 +
@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" );
 +
 +
  ...
 +
}
 
</source>
 
</source>

Revision as of 12:40, 15 February 2010


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>

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;
        }
}

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>

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


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