Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » [Databinding] multi-layered binding with master detail scenario
[Databinding] multi-layered binding with master detail scenario [message #1288352] Tue, 08 April 2014 13:41 Go to next message
Erdal Karaca is currently offline Erdal KaracaFriend
Messages: 845
Registered: July 2009
Senior Member
I have a scenario that involves 'multiple layers' of databinding with an additional master detail binding:

1. layer (B): Business Domain Object
2. layer (I): Intermediate Domain Object
3. layer (W): Widget Domain Object

B's property B.prop1 is bound to I's properties I.prop1, I.prop2, I.prop3 and I.prop4.
Each prop1..4 of I is bound to a W property.

The problem: when the master value is replaced, the Intermediate Object is not updated, so, the Widget Objects are not updated either.

In code, this Looks like the following:

final Button checkbox1 = ...;
final Button checkbox2 = ...;
final Button checkbox3 = ...;
final Button checkbox4 = ...;
final WritableValue master = ...;
final IntermediateObject i = ...;
final IObservableValue iProp1 = BeansObservables.observeValue(i, "prop1");
final IObservableValue iProp2 = BeansObservables.observeValue(i, "prop2");
final IObservableValue iProp3 = BeansObservables.observeValue(i, "prop3");
final IObservableValue iProp4 = BeansObservables.observeValue(i, "prop4");

// bind each of the 4 props of Intermediate Object to its own check box instance
dbc.bindValue(SWTObservables.observeSelection(checkbox1), iProp1);
dbc.bindValue(SWTObservables.observeSelection(checkbox2), iProp2);
dbc.bindValue(SWTObservables.observeSelection(checkbox3), iProp3);
dbc.bindValue(SWTObservables.observeSelection(checkbox4), iProp4);

ComputedValue computedValue = new ComputedValue(String.class) {

    @Override
    protected Object calculate() {
        // if master value == null, return
        
        String calculatedValue = iProp1.getValue() + iProp2.getValue() + iProp3.getValue() + iProp4.getValue();
        return calculatedValue;
    }

    @Override
    protected void doSetValue(Object value) {
        final String businessObjectValue = (String)value;
        iProp1.setValue(deriveProp1FromValue(businessObjectValue));
        iProp2.setValue(deriveProp2FromValue(businessObjectValue));
        iProp3.setValue(deriveProp3FromValue(businessObjectValue));
        iProp4.setValue(deriveProp4FromValue(businessObjectValue));
    }
};

// now, bind the calculated value of the 4 props of the Intermediate Object to the prop of the Business Object
dbc.bindValue(computedValue, BeansObservables.observeDetailValue(master, "prop1", String.class));


Any ideas?

Thanks!

[Updated on: Tue, 08 April 2014 13:42]

Report message to a moderator

Re: [Databinding] multi-layered binding with master detail scenario [message #1323641 is a reply to message #1288352] Wed, 30 April 2014 11:07 Go to previous message
Nigel Westbury is currently offline Nigel WestburyFriend
Messages: 18
Registered: July 2009
Junior Member
I tried this out and I see no problem. Unfortunately you had not
attached a test case or even complete code, so I had to fill in all the
missing code to create the test case (attached herewith). If you still
see a problem, it would be easiest if you could amend the test case to
show the problem.

BTW, for an alternative solution you might like to look at
DateAndTimeObservableValue. It does a similar thing by taking a single
timestamp from the model and splitting it into a date part and a time
part for binding to two controls. There is unfortunately more
boilerplate code than with your solution but it is more generic in that
the code to split the value is separate from the code that binds to
specific observables in the model and target.

Nigel Westbury

On 08/04/2014 14:41, Erdal Karaca wrote:
> I have a scenario that involves 'multiple layers' of databinding with an
> additional master detail binding:
>
> 1. layer (B): Business Domain Object
> 2. layer (I): Intermediate Domain Object
> 3. layer (W): Widget Domain Object
>
> B's property B.prop1 is bound to I's properties I.prop1, I.prop2,
> I.prop3 and I.prop4.
> Each prop1..4 of I is bound to a W property.
>
> The problem: when the master value is replaced, the Intermediate Object
> is not updated, so, the Widget Objects are not updated either.
>
> In code, this Looks like the following:
>
>
> final Button checkbox1 = ...;
> final Button checkbox2 = ...;
> final Button checkbox3 = ...;
> final Button checkbox4 = ...;
> final WritableValue master = ...;
> final IntermediateObject i = ...;
> final IObservableValue iProp1 = BeansObservables.observeValue(i, "prop1");
> final IObservableValue iProp2 = BeansObservables.observeValue(i, "prop2");
> final IObservableValue iProp3 = BeansObservables.observeValue(i, "prop3");
> final IObservableValue iProp4 = BeansObservables.observeValue(i, "prop4");
>
> // bind each of the 4 props of Intermediate Object to its own check box
> instance
> dbc.bindValue(SWTObservables.observeSelection(checkbox1), iProp1);
> dbc.bindValue(SWTObservables.observeSelection(checkbox2), iProp2);
> dbc.bindValue(SWTObservables.observeSelection(checkbox3), iProp3);
> dbc.bindValue(SWTObservables.observeSelection(checkbox4), iProp4);
>
> ComputedValue computedValue = new ComputedValue(String.class) {
>
> @Override
> protected Object calculate() {
> // if master value == null, return
> String calculatedValue = iProp1.getValue() + iProp2.getValue() +
> iProp3.getValue() + iProp4.getValue();
> return calculatedValue;
> }
>
> @Override
> protected void doSetValue(Object value) {
> final String businessObjectValue = (String)value;
> iProp1.setValue(deriveProp1FromValue(businessObjectValue));
> iProp2.setValue(deriveProp2FromValue(businessObjectValue));
> iProp3.setValue(deriveProp3FromValue(businessObjectValue));
> iProp4.setValue(deriveProp4FromValue(businessObjectValue));
> }
> };
>
> // now, bind the calculated value of the 4 props of the Intermediate
> Object to the prop of the Business Object
> dbc.bindValue(frameValue, BeansObservables.observeDetailValue(master,
> "prop1", String.class));
>
>
> Any ideas?
>
> Thanks!


package org.eclipse.core.tests.databinding;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.value.ComputedValue;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.WritableValue;
import org.eclipse.jface.tests.databinding.AbstractDefaultRealmTestCase;

public class ErdalKaracaTest extends AbstractDefaultRealmTestCase {

class MyModel {

String prop;

private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(
this);

public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}

public void addPropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(propertyName,
listener);
}

public void removePropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(listener);
}

public void removePropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(propertyName,
listener);
}

/**
* @param string
*/
public MyModel(String prop) {
this.prop = prop;
}

public String getProp() {
return prop;
}

public void setProp(String value) {
propertyChangeSupport
.firePropertyChange("prop", prop, prop = value);
}

}

private DataBindingContext dbc;

class IntermediateObject {
String prop1, prop2, prop3, prop4;

public String getProp1() {
return prop1;
}

public String getProp2() {
return prop2;
}

public String getProp3() {
return prop3;
}

public String getProp4() {
return prop4;
}

public void setProp1(String value) {
prop1 = value;
}

public void setProp2(String value) {
prop2 = value;
}

public void setProp3(String value) {
prop3 = value;
}

public void setProp4(String value) {
prop4 = value;
}

}

final WritableValue<MyModel> master = new WritableValue<MyModel>();
final IntermediateObject i = new IntermediateObject();
final IObservableValue<String> iProp1 = BeanProperties.value(
IntermediateObject.class, "prop1", String.class).observe(i);
final IObservableValue<String> iProp2 = BeanProperties.value(
IntermediateObject.class, "prop2", String.class).observe(i);
final IObservableValue<String> iProp3 = BeanProperties.value(
IntermediateObject.class, "prop3", String.class).observe(i);
final IObservableValue<String> iProp4 = BeanProperties.value(
IntermediateObject.class, "prop4", String.class).observe(i);

protected void setUp() throws Exception {
super.setUp();

dbc = new DataBindingContext();
}

public void testBinding() {
ComputedValue<String> computedValue = new ComputedValue<String>() {

@Override
protected String calculate() {
// if master value == null, return
String calculatedValue = iProp1.getValue() + ":"
+ iProp2.getValue() + ":" + iProp3.getValue() + ":"
+ iProp4.getValue();
return calculatedValue;
}

@Override
protected void doSetValue(String value) {
String[] parts = value.split(":");
iProp1.setValue(parts[0]);
iProp2.setValue(parts[1]);
iProp3.setValue(parts[2]);
iProp4.setValue(parts[3]);
}
};

final IObservableValue<String> iProp = BeanProperties.value(
MyModel.class, "prop", String.class).observeDetail(master);

dbc.bindValue(computedValue, iProp);

MyModel model1 = new MyModel("a:b:c:d");
MyModel model2 = new MyModel("a:b:x:d");
master.setValue(model1);
assertEquals("a", iProp1.getValue());
assertEquals("b", iProp2.getValue());
assertEquals("c", iProp3.getValue());
assertEquals("d", iProp4.getValue());
master.setValue(model2);
assertEquals("a", iProp1.getValue());
assertEquals("b", iProp2.getValue());
assertEquals("x", iProp3.getValue());
assertEquals("d", iProp4.getValue());
}

}
Previous Topic:Boolean in ComboBoxCellEditor
Next Topic:What are the latest databinding versions?
Goto Forum:
  


Current Time: Tue Sep 18 13:55:13 GMT 2018

Powered by FUDForum. Page generated in 0.02040 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top