Class ObservableTracker

java.lang.Object
org.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 Details

    • ObservableTracker

      public ObservableTracker()
  • Method Details

    • 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, or null if no change listener is to be registered
      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
    • runAndCollect

      public static IObservable[] runAndCollect(Runnable runnable)
      Invokes the given runnable, and returns the set of IObservables that were created by the runnable. If the runnable calls this method recursively, the result will not contain IObservables that were created within the inner runnable.

      NOTE: As of 1.2 (Eclipse 3.5), there are unresolved problems with this API, see bug 278550. If we cannot find a way to make this API work, it will be deprecated as of 3.6.

      Parameters:
      runnable - runnable to execute
      Returns:
      an array of unique observable objects
      Since:
      1.2
    • setIgnore

      public static void setIgnore(boolean ignore)
      If the argument is true, causes subsequent calls to getterCalled(IObservable) and observableCreated(IObservable) to be ignored on the current thread. When the flag is set to false, calls to getterCalled(IObservable) and observableCreated(IObservable) will resume gathering observables. Nested calls to this method are stacked.
      Parameters:
      ignore - the new ignore state
      Throws:
      IllegalStateException - if ignore is false and the ignore count is already zero.
      Since:
      1.3
      See Also:
    • runAndIgnore

      public static void runAndIgnore(Runnable runnable)
      Runs the given runnable without tracking dependencies.
      Parameters:
      runnable - the runnable to execute
      Since:
      1.1
      See Also:
    • 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 - the observable whose getter was called
    • observableCreated

      public static void observableCreated(IObservable observable)
      Notifies the ObservableTracker that an observable was created.
      Parameters:
      observable - the observable that was created
      Since:
      1.2