wiki:E4_Using_Event_Bus

Communicating between E4 Parts

Here are some of the ways listed that are commonly used to communicate between e4 parts.

Communication between E4 Parts using Event Bus

For communicating between two different parts, you can use eclipse event bus to send (or post for asynchronous publishing) messages and subscribe to messages for listening to messages posted by other plugins. This tutorial provides example of using event bus with documentation.

The general idea is to post an event on event bus (using IEventBroker.class) with some information on that topic. All the interested parts listen to this topic and can make changes based on the information. All you you need to do is to inject IEventBroker.

	@Inject IEventBroker eventBroker;

	@Optional
	@Inject
	void listenToSomeTopic(@UIEventTopic
			(TOPIC_NAME) Object expectedObject)
	{
		// change something based on the received object
	}

	void sendAMessageWithTopic()
	{
		// do something and when changed something send a message
		eventBroker.send(TOPIC_VALUE_MODIFIED, modifiedObject);
	}

Communication between E4 Parts using Service

Another frequently used way to communicate between e4 parts is using ESelectionService. When the current ESelectionService object is changed all the methods injecting this object gets notified. The part listening to this service can use this service to update its information based on current value. To get the last selection of the ElectionService, one uses the name of the injected variable which is IServiceConstants.ACTIVE_SELECTION.

The part that is setting the object in the ESelectionService should get this service using injection and set its selection.

	@Inject
	public void someMethod(ESelectionService selectionService)
	{
		// other code
		selectionService.setSelection(selection);
	}

Part using the selection service current selection object.

	@Inject
	public void someOtherMethod(
		@Named(IServiceConstants.ACTIVE_SELECTION) Object currentSelection)
	{
		// use this current selection
	}

In Grits, the IGritsUIService.class in the core plugin uses this service to select the most recent entry that is being opened in the editor. The editor being opened uses this ESelectionService to get the last selection entry.

An example of editor initializing its entry while being opened.

	@Inject
	public ProjectEntryPart(@Named(IServiceConstants.ACTIVE_SELECTION) Entry entry,
			@Named(IServiceConstants.ACTIVE_PART) MPart part, 
			EPartService partService...)
	{
		if(entry != null && ProjectExplorerPart.PART_ID.equals(part.getElementId()))
		{
			// other code
		}
	}

Here is also an helpful detailed tutorial on using this service for communication between parts.

Eclipse Documentation on Using Event Bus
E4 Event Bus
Eclipse Workbench: Using the Selection Service

Last modified 5 years ago Last modified on 09/19/2016 09:34:52 PM