Jump to: navigation, search

E4/EAS/Eventing System

< E4‎ | EAS
Revision as of 11:30, 26 October 2009 by Remysuen.ca.ibm.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Components should be able to listen to any kind of events they may be interested in. It should be possible for this pattern to be extended without too much trouble or code for both the event publisher and the subscriber. This should help reduce code bloat and consequently make the code more readable and accessible to new developers.

Eclipse 3.x API

In Eclipse 3.x, there are a lot of listeners. IPerspectiveListener alone has four permutations, IPerspectiveListener, IPerspectiveListener2, IPerspectiveListener3, and IPerspectiveListener4, and it's quite possible that there will be many more down the road.

Listening for part activation events

workbenchPage.addPartListener(new IPartListener() {
  public void partActivated(IWorkbenchPart part) {
    // do something
  }
 
  /* Other code not shown here */
});

Sending out part activation events

public void firePageActivated(final IWorkbenchPage page) {
  Object[] array = getListeners();
  for (int i = 0; i < array.length; i++) {
    final IPageListener l = (IPageListener) array[i];
    SafeRunner.run(new SafeRunnable() {
      public void run() {
        l.pageActivated(page);
      }
    });
  }
}

Posting asynchronous events

For posting asynchronous events, if it's for the SWT user interface, a user can use Display's asyncExec method for executing a Runnable asynchronously on the SWT Display's thread. These will be dispatched when the display's event loop is being spun.

Runnable runnable = new Runnable() {
  public void run() {
    // send out the event
  }
};
display.asyncExec(runnable);

If it doesn't have to be on the user interface thread, a developer could potentially just use a Job or spawn off their own Thread.

final Runnable runnable = new Runnable() {
  public void run() {
    // send out the event
  }
};
 
Job job = new Job("Event posting job") {
  protected IStatus run(IProgressMonitor monitor) {
    runnable.run();
    return Status.OK_STATUS;
  }
};
job.schedule();
Runnable runnable = new Runnable() {
  public void run() {
    // send out the event
  }
};
Thread thread = new Thread(runnable);
thread.start();

e4 (Java)

In e4, a new IEventBroker API has been introduced. See bug 288999 and the event processing wiki page for more information.

Listening for part activation events

IEventBroker eventBroker = (IEventBroker) eclipseContext.get(
    IEventBroker.class.getName());
eventBroker.subscribe(IUIEvents.ElementContainer.Topic, null,
    new EventHandler() {
        public void handleEvent(Event event) {
          if (event.getProperty(IUIEvents.EventTags.AttName)
              .equals(IUIEvents.ElementContainer.ActiveChild)) {
            Object newPart = event.getProperty(IUIEvents.EventTags.NewValue);
            if (newPart instanceof MPart) {
              // do something
            }
          }
        }
    }, /* headless */ false);

Sending out part activation events

Map data = new HashMap();
map.put(IUIEvents.EventTags.AttName, IUIEvents.ElementContainer.ActiveChild);
map.put(IUIEvents.EventTags.Element, partStack);
map.put(IUIEvents.EventTags.OldValue, oldPart);
map.put(IUIEvents.EventTags.NewValue, newPart);
map.put(IUIEvents.EventTags.Type, IUIEvents.EventTypes.Set);
 
IEventBroker eventBroker = (IEventBroker) eclipseContext.get(
    IEventBroker.class.getName());
eventBroker.send(IUIEvents.ElementContainer.Topic, data);

Posting out asynchronous events

Using the IEventBroker API, there is a post(String, Object) method to send events asynchronously and the implementation is expected to handle this so no action is required on the developer's part.

IEventBroker eventBroker = (IEventBroker) eclipseContext.get(
    IEventBroker.class.getName());
eventBroker.post(eventTopic, data);