Thursday, February 12, 2009

Master-detail editing with multiple selection

A few weeks ago we released DuplexingObservableValue. This class, among other things, helps make possible master-detail editing on a multiple selection:


Because the selected movies have the same director and same writer, those values are displayed in the master-detail fields at the bottom. However, since they have different titles and release dates, those fields use a stand-in "multi value" instead.

Editing the release date field simultaneously changes the release date of all movies in the selection.


Here's the code that was used to hook up the detail fields to the multi-selection of the viewer:

IObservableList selections = ViewerProperties.multipleSelection()
.observe(viewer);

dbc.bindValue(
WidgetProperties.text(SWT.Modify).observe(title),
DuplexingObservableValue.withDefaults(
BeanProperties.value("title").observeDetail(selections),
"", "<Multiple titles>"));

dbc.bindValue(
WidgetProperties.text(SWT.Modify).observe(releaseDate),
DuplexingObservableValue.withDefaults(
BeanProperties.value("releaseDate").observeDetail(selections),
"", "<Multiple dates>"));

dbc.bindValue(
WidgetProperties.text(SWT.Modify).observe(director),
DuplexingObservableValue.withDefaults(
BeanProperties.value("director").observeDetail(selections),
"", "<Multiple directors>"));

dbc.bindValue(
WidgetProperties.text(SWT.Modify).observe(writer),
DuplexingObservableValue.withDefaults(
BeanProperties.value("writer").observeDetail(selections),
"", "<Multiple writers>"));
See the full code listing for this snippet here.

Disclaimer: The method DuplexingObservableValue.withDefaults() was added after the 3.5M5 milestone, so in order to use it you need to check out the databinding projects from CVS HEAD, or be using a more recent integration build.

8 comments:

Benjamin Cabé said...

Wow, this is quite impressive!

Ed Merks said...

One problem I've had with EMF support for editing merged properties is that it results in multiple commands on the command stack and hence a single edit requires multiple undos. Even EMF's data binding integration for what you show here will have this same type of problem (I think). Should I care or is that just a fundamental limitation?

Matthew Hall said...

I'm not too familiar with EMF, but you could extend DuplexingObservableValue and wrap the set operation in a transaction:

class EMFDuplexingObservableValue extends DuplexingObservableValue {
  protected void doSetValue(Object value) {
    startTransaction();
    super.doSetValue(value);
    commitTransaction();
  }
}

Where startTransaction and commitTransaction are replaced with whatever the appropriate code is for EMF.

vogella said...

This is very helpful. Will this be available in Eclipse 3.5M6?

Matthew Hall said...

vogella: yes this will be in M6

Website design nyc said...

Great post

Anonymous said...

Great, just what I was looking for.
Unfortunately I'm using more specialized widgets than Text for input, i.e. Spinner, DateTime, (C)Combo. Combos work fine but AFAIK Spinner & DateTime don't allow texts like "<multiple Values>" (Spinner) or "dd.mm.yyyy" (DateTime) to represent the situation of multiple selected datasets. All workarounds I can think of are rather clumsy IMO.
I'm not yet a very experienced user in the RCP-SWT-JFace... multiverse, do you have any suggestions how to overcome this problem ?

Boris Bokowski said...

Hi Anonymous,

I have no good idea either. If your specialized widgets are custom widgets (e.g. from the Nebula project), you could try to extend the custom widget itself.