JFace Data Binding/TrackedGetter

From Eclipsepedia

< JFace Data Binding
Revision as of 14:58, 6 December 2007 by Boris Bokowski.ca.ibm.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Every getter on an observable object must make a call to ObservableTracker.getterCalled(). Why?

This enables abstractions like ComputedValue or ComputedList: All you have to write is a piece of code that computes a value, or a list. As long as this code only accesses other observables, the framework will manager listeners for you automatically.

For example, assume that your UI has text fields for the first name and the last name of a person, but you want to display a formatted version of the full name elsewhere in the UI. This is what you can write (see Snippet008 for a complete example):

final IObservableValue firstName = SWTObservables.observeText(firstNameField, SWT.Modify);
final IObservableValue lastName = SWTObservables.observeText(lastNameField, SWT.Modify);
IObservableValue formattedName = new ComputedValue() {
  protected Object calculate() {
    return lastName.getValue() + firstName.getValue();
  }
};

Note that without having to register any listeners from client code, the 'formattedName' observable will always change whenever one of the observables referenced in its calculate() method changes. We didn't even have to pass a list of observables to the framework - just by calling lastName.getValue(), or firstName.getValue(), it will figure out that it has to listen for changes to lastName and firstName for you.

Related Work

This idea is not new. It has been around for (at least) 10 years. Here are some pointers to related work.