Eclipse Platform
Release 3.3

org.eclipse.core.databinding.observable
Class ObservableTracker

java.lang.Object
  extended byorg.eclipse.core.databinding.observable.ObservableTracker

public class ObservableTracker
extends Object

This class makes it possible to monitor whenever an IObservable is read from. This can be used to automatically attach and remove listeners. How to use it:

If you are implementing an IObservable, invoke getterCalled(this) whenever a getter is called - that is, whenever your observable is read from. You only need to do this once per method call. If one getter delegates to another, the outer getter doesn't need to call the method since the inner one will.

If you want to determine what observables were used in a particular block of code, call runAndMonitor(Runnable). This will execute the given runnable and return the set of observables that were read from.

This can be used to automatically attach listeners. For example, imagine you have a block of code that updates some widget by reading from a bunch of observables. Whenever one of those observables changes, you want to re-run the code and cause the widget to be refreshed. You could do this in the traditional manner by attaching one listener to each observable and re-running your widget update code whenever one of them changes, but this code is repetitive and requires updating the listener code whenever you refactor the widget updating code.

Alternatively, you could use a utility class that runs the code in a runAndMonitor block and automatically attach listeners to any observable used in updating the widget. The advantage of the latter approach is that it, eliminates the code for attaching and detaching listeners and will always stay in synch with changes to the widget update logic.

Since:
1.0

Constructor Summary
ObservableTracker()
           
 
Method Summary
static void getterCalled(IObservable observable)
          Notifies the ObservableTracker that an observable was read from.
static IObservable[] runAndMonitor(Runnable runnable, IChangeListener changeListener, IStaleListener staleListener)
          Invokes the given runnable, and returns the set of IObservables that were read by the runnable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObservableTracker

public ObservableTracker()
Method Detail

runAndMonitor

public static IObservable[] runAndMonitor(Runnable runnable,
                                          IChangeListener changeListener,
                                          IStaleListener staleListener)
Invokes the given runnable, and returns the set of IObservables that were read by the runnable. If the runnable calls this method recursively, the result will not contain IObservables that were used within the inner runnable.

Parameters:
runnable - runnable to execute
changeListener - listener to register with all accessed observables
staleListener - listener to register with all accessed observables, or null if no stale listener is to be registered
Returns:
an array of unique observable objects

getterCalled

public static void getterCalled(IObservable observable)
Notifies the ObservableTracker that an observable was read from. The JavaDoc for methods that invoke this method should include the following tag: "@TrackedGetter This method will notify ObservableTracker that the receiver has been read from". This lets callers know that they can rely on automatic updates from the object without explicitly attaching a listener.

Parameters:
observable -

Eclipse Platform
Release 3.3

Guidelines for using Eclipse APIs.

Copyright (c) IBM Corp. and others 2000, 2007. All rights reserved.