Skip to main content

Notice: This Wiki is now read only and edits are no longer 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/EAS/Eventing System"

< E4‎ | EAS
(New page: 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 eve...)
 
Line 13: Line 13:
 
   /* Other code not shown here */
 
   /* Other code not shown here */
 
});
 
});
 +
</source>
 +
 +
===Sending out part activation events===
 +
<source lang="java">
 +
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);
 +
      }
 +
    });
 +
  }
 +
}
 
</source>
 
</source>
  
Line 34: Line 49:
 
         }
 
         }
 
     }, /* headless */ false);
 
     }, /* headless */ false);
 +
</source>
 +
 +
===Sending out part activation events===
 +
<source lang="java">
 +
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.post("org/eclipse/e4/ui/model/ElementContainer", data);
 
</source>
 
</source>

Revision as of 10:59, 26 October 2009

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

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.post("org/eclipse/e4/ui/model/ElementContainer", data);

Back to the top