Optionally copy values between EMF attributes

46 Views Asked by At

Question

Imagine following EMF-model based JFace-form

+-------------------------------------------------+
| My text field1           :  __________________  |
+-------------------------------------------------+
| Inherit value from field1:  [x]                 |
| My text field2           :  __________________  |
+-------------------------------------------------+

Corresponding EMF-EClass

class Model {
    String field1;
    boolean inherit;
    String field2;
}

Here the user should enter the value of the field1. Then he can

  • check the checkbox to copy the value from field1 to field2
  • uncheck the checkbox and enter a different value for the field2

My question:

  • How this kind of pattern should be properly implemented using JFace data-binding?

(The text field my be all kind of widgets including tables)

(I would like to leave the enabling/disabling field2 text box out of the scope of this question)

Dirty solution

IObservableValue value1Obs = EMFProperties.value(field1).observe(model);
IObservableValue value2Obs = EMFProperties.value(field2).observe(model);
IObservableValue inheritObs = EMFProperties.value(inherit).observe(model);

IObservableValue copyObs = new ComputedValue() {
    @Override
    protected Object calculate() {
        if ((Boolean)inheritObs.getValue()) {
            return value1Obs.getValue();
        }
        return value2Obs.getValue();
    }
}
getBindingContext().bindValue(value2Obs, copyObs);
  • Don't use this
  • This works for simple attributes but don't work for lists/tables.
  • Also in case inherit=false I bind field2 to itself. This looks weird and may cause problems in the future.
0

There are 0 best solutions below