Skip to main content
Jump to: navigation, search

Eclipse4/RCP/Modeled UI/Listening to Model Changes

< Eclipse4‎ | RCP‎ | Modeled UI
Revision as of 14:26, 11 April 2011 by Remysuen.ca.ibm.com (Talk | contribs) (Retrieving information from the events)

When the model changes and it is not a no-op (for example, setting its label to its current label), an event will be dispatched to all interested event handlers. Eclipse 4 uses a global listener pattern so all handler subscriptions are done through the event broker.

Subscribing to model changes

Topics for event subscriptions can be generated from the UIEvents class.

// subscribe to all events generated from MUILabels
eventBroker.subscribe(UIEvents.buildTopic(UIEvents.UILabel.TOPIC), eventHandler);
 
// subscribe only to events generated from MItems's selected attribute being switched
eventBroker.subscribe(UIEvents.buildTopic(UIEvents.Item.TOPIC, UIEvents,Item.SELECTED), eventHandler);

Retrieving information from the events

// subscribe to all events generated from MUILabels
eventBroker.subscribe(UIEvents.buildTopic(UIEvents.UILabel.TOPIC, UIEvents.UILabel.LABEL),
        new EventHandler() {
    public void handleEvent(Event event) {
      Object element = event.getProperty(UIEvents.EventTags.ELEMENT);
      if (element instanceof MToolItem) {
        ToolItem item = (ToolItem) ((MToolItem) element).getWidget();
        String value = (String) event.getProperty(UIEvents.EventTags.NEW_VALUE);
        if (value == null) {
          item.setText(""); //$NON-NLS-1$
        } else {
          item.setText(value);
        }
      }
    }
);

Back to the top