Skip to main content
Jump to: navigation, search

Difference between revisions of "Eclipse4/RCP/Modeled UI/Listening to Model Changes"

< Eclipse4‎ | RCP‎ | Modeled UI
(Retrieving information from the events)
(Subscribing to model changes)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
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 [[E4/RCP/Event Model|global listener pattern]] so all handler subscriptions are done through the event broker.
+
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 [[Eclipse4/RCP/Event Model|global listener pattern]] so all handler subscriptions are done through the event broker.
  
 
==Subscribing to model changes==
 
==Subscribing to model changes==
Line 6: Line 6:
 
<source lang="java">
 
<source lang="java">
 
// subscribe to all events generated from MUILabels
 
// subscribe to all events generated from MUILabels
eventBroker.subscribe(UIEvents.buildTopic(UIEvents.UILabel.TOPIC), eventHandler);
+
eventBroker.subscribe(UIEvents.buildTopic(UIEvents.UILabel.TOPIC),
 +
    labelEventHandler);
  
 
// subscribe only to events generated from MItems's selected attribute being switched
 
// subscribe only to events generated from MItems's selected attribute being switched
eventBroker.subscribe(UIEvents.buildTopic(UIEvents.Item.TOPIC, UIEvents,Item.SELECTED), eventHandler);
+
eventBroker.subscribe(UIEvents.buildTopic(UIEvents.Item.TOPIC, UIEvents,Item.SELECTED),
 +
    itemSelectionEventHandler);
 
</source>
 
</source>
  
 
==Retrieving information from the events==
 
==Retrieving information from the events==
 
<source lang="java">
 
<source lang="java">
// subscribe to all events generated from MUILabels
+
// subscribe to events generated from MUILabels having their labels changed
 
eventBroker.subscribe(UIEvents.buildTopic(UIEvents.UILabel.TOPIC, UIEvents.UILabel.LABEL),
 
eventBroker.subscribe(UIEvents.buildTopic(UIEvents.UILabel.TOPIC, UIEvents.UILabel.LABEL),
        new EventHandler() {
+
    new EventHandler() {
    public void handleEvent(Event event) {
+
      public void handleEvent(Event event) {
      Object element = event.getProperty(UIEvents.EventTags.ELEMENT);
+
        Object element = event.getProperty(UIEvents.EventTags.ELEMENT);
      if (element instanceof MToolItem) {
+
        if (element instanceof MToolItem) {
        ToolItem item = (ToolItem) ((MToolItem) element).getWidget();
+
          ToolItem item = (ToolItem) ((MToolItem) element).getWidget();
        String value = (String) event.getProperty(UIEvents.EventTags.NEW_VALUE);
+
          String value = (String) event.getProperty(UIEvents.EventTags.NEW_VALUE);
        if (value == null) {
+
          if (value == null) {
          item.setText(""); //$NON-NLS-1$
+
            item.setText(""); //$NON-NLS-1$
        } else {
+
          } else {
          item.setText(value);
+
            item.setText(value);
 +
          }
 
         }
 
         }
 
       }
 
       }

Latest revision as of 14:07, 14 April 2011

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),
    labelEventHandler);
 
// subscribe only to events generated from MItems's selected attribute being switched
eventBroker.subscribe(UIEvents.buildTopic(UIEvents.Item.TOPIC, UIEvents,Item.SELECTED),
    itemSelectionEventHandler);

Retrieving information from the events

// subscribe to events generated from MUILabels having their labels changed
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