© Markus Duft 2018
Markus DuftEclipse TEA Revealedhttps://doi.org/10.1007/978-1-4842-4093-9_11

11. E4 Events

Markus Duft1 
(1)
Peggau, Steiermark, Austria
 

TEA provides a bridge from its own TaskingLifeCycleListener mechanism to the E41 event bus mechanism. This allows for some advanced integration in a more Eclipse-ish fashion. Actually, the Tasking Live View is implemented based on this mechanism. It is a plain E4 view part that requests TEA events from the Eclipse event bus.

The Event Bridge

The bridge is implemented in org.eclipse.tea.core.ui.internal.listeners.EventBrokerBridge. It is quite straightforward since it broadcasts an event for every TaskingLifeCycleListener method possible to the Eclipse IEventBroker. TEA uses the org/eclipse/tea/* topic namespace for this.

Note

You can spy on these events using Eclipse’s E4 tools, which can be found in the marketplace.2 Figure 11-1 shows an example of this. Once installed (in the TEA workspace, not the runtime workspace), the event spy can be accessed from (in the runtime workspace, when using the TEA-Book-Samples launch configuration) Window ➤ Spies ➤ Event Spy.

../images/470772_1_En_11_Chapter/470772_1_En_11_Fig1_HTML.jpg
Figure 11-1

Using the E4 Event Spy to see TEA events on the Eclipse event bus

Every event sent by the bridge component has the currently active IEclipseContext used by TEA for dependency injection of TEA components attached as data object. This allows extracting anything in the TEA DI context as well as “resuming” the DI chain when receiving an event, as shown in Listing 11-1.
@Component(immediate = true)
public class SampleE4EventListener
                    implements EventHandler {
      @Activate
      public void activate() {
             IEventBroker broker = E4Workbench
                   .getServiceContext()
                   .get(IEventBroker.class);
             broker.subscribe(
                   EventBrokerBridge.EVENT_TOPIC_BASE
                   + "*", null,
                   SampleE4EventListener.this, true);
      }
      @Override
      public void handleEvent(Event event) {
             Object data = event
                   .getProperty(IEventBroker.DATA);
             if (!(data instanceof IEclipseContext)) {
                   return;
             }
             IEclipseContext eventContext =
                   (IEclipseContext) data;
             // show events for this
             // sample project only.
             if (!eventContext.get(TaskChain.class)
                          .getClass().getName()
                          .contains("ch11.s01")) {
                   return;
             }
             IEclipseContext child = eventContext
                   .createChild(
                          "My Processing Context");
             child.set(Event.class, event);
             ContextInjectionFactory
                   .invoke(this, Execute.class, child);
      }
      @Execute
      public void process(
                          TaskingLog log, Event event) {
             log.info("received " + event);
      }
}
Listing 11-1

Subscribing to and Processing TEA Events

This code shows how to quickly and dirtily attach a listener to the events. To see this live, run TEA ➤ Samples ➤ CH11-S01: E4 Events in the runtime workspace of TEA-Book-Samples, as demonstrated in Figure 11-2.
../images/470772_1_En_11_Chapter/470772_1_En_11_Fig2_HTML.jpg
Figure 11-2

Custom component to capture TEA events in action

In a typical E4 application (or Eclipse plug-in), you will likely implement E4 objects that can use DI to obtain the IEventBroker for the application. In this example, we’re using an OSGi service that is activated on plug-in activation (due to the immediate attribute). Right there in the @Activate method (another OSGi declarative service annotation), we subscribe to all TEA events (providing the service itself as event handler).

The handleEvent method is now called for every event that matches the filter given to the IEventBroker when calling the subscribe method . We briefly check whether the Event instance looks like we expect it. Then we create a child context of the associated context. (This is the current IEclipseContext in use by TEA. Depending on the event, this can be either of the previously described IEclipseContext levels in the context stack used by TEA.) We need this context to be able to add things to it without interfering with TEA itself. Now we can stuff the current Event instance in the child context and use E4’s DI mechanism to invoke the process method by telling the framework to invoke a method annotated with @Execute on this.

This trick allows us to get back to TEA’s dependency injection mechanism because we’re using the actual context (as parent of our child context). This will allow us to inject whatever is injectable with TEA as well as the current Event instance (as we added that to the context).

With the conclusion of this chapter, we have covered all technical concepts of TEA core (and core UI) as seen in Figure 3-2 back in Chapter 3. We’ll now move on to the TEA library, which contains TEA component implementations for advanced use cases.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.27.244