Showing posts with label selection. Show all posts
Showing posts with label selection. Show all posts

Tuesday, August 21, 2007

Observe Multiple Selections of Viewers

Thanks to Peter Centgraf and Boris we now have the ability to observe multiple selections in Viewers. To take it for a test drive check out org.eclipse.jface.databinding from HEAD and take a look at ViewersObservables.observeMultiSelection(ISelectionProvider selectionProvider). Technically you can observe the selection of any ISelectionProvider with the API, not just Viewers.

bug 124683

Tuesday, January 09, 2007

Master Detail - Selection

I find myself answering quite a few questions on the newsgroup hinting that the solution is the master detail APIs. I figured that a verbose explanation couldn't hurt the cause. The stereotypical master detail use case contains a list and upon selection in the list details will be displayed in a separate set of widgets. I'll attempt to explain the core concepts involved.

To explain we'll use the master detail snippet available in the data binding examples project. The snippet displays an SWT List filled with Persons. The Person model has one property, name. Upon selection in the List the name of the currently selected person is displayed in a Text widget.



The relevant data binding code is copied and pasted below...
// 1. Observe changes in selection.
IObservableValue selectionObservable = ViewersObservables
.observeSingleSelection(viewer);

// 2. Observe the name property of the current selection.
IObservableValue detailObservable = BeansObservables
.observeDetailValue(Realm.getDefault(), selectionObservable, "name",
String.class);

// 3. Bind the Text widget to the name detail (selection's name).
new DataBindingContext().bindValue(SWTObservables.observeText(name,
SWT.None), detailObservable, new BindSpec().setUpdateModel(false));


If you follow the comments in the snippet you'll see that we need to perform 3 steps:

  1. Observe changes of the viewer's selection.

  2. Observe the name property of the selection.

  3. Bind the text of the Text widget to the name detail (selected person's name).


1. Observe changes in the viewer's selection


IObservableValue has a value. I know, rocket science. But what IObservableValue provides us is a reference to a transient instance and we can observe changes from one instance to the next. If the selection of the viewer is a person the value of the selection observable will be this person. If the selection is null the value of the selection observable will be null. Regardless of the value we have a reference to an object that will contain the viewer's selection. Change events will fire when a new person, or null, is set as the value of this observable. This is the master.

2. Observe the detail of the master (name of the selected person)


When we observe a detail of the master we're effectively saying...
"observe the value of a property of the value contained in an observable".


Translated into our use case we're saying...
"observe the value of the name property of the person contained in the selection"


If the selection of the the Viewer is a Person with the name "mickey mouse" the following will be true...

  • selectionObservable.getValue() == Person("mickey mouse")

  • detailObservable.getValue() == "mickey mouse"


When the selection changes to a Person with the name of "daffy duck" the following will be true...

  • selectionObservable.getValue() == Person("daffy duck")

  • detailObservable.getValue() == "daffy duck"


When the selection changes to null the following will be true...

  • selectionObservable.getValue() == null

  • detailObservable.getValue() == null


When the master value changes the detail populates itself with the value from the master. This gives us the master detail behavior. To finish it off we need to...

3. Bind the detail to the Text widget


The last step is to bind the detail observable to the Text widget in order to display the detail. Enough said.

Tomorrow night I'll describe a use case that explains how this fits into using JFace Data Binding and the presentation model design pattern.