I've tried to bind an entity to some Text widgets, but without success.
The text fields are just empty.
Below the dialog in which I'm testing this functionality ... (the class
Customer obviously inherits EObjectImpl)
<snippet>
import model.ICustomer;
import net.miginfocom.swt.MigLayout;
import ...
public class MyDialog extends Dialog {
private ICustomer entity;
private DataBindingContext context;
private EClass eClass;
public MyDialog(Shell shell, ICustomer customer) {
super(shell);
entity = customer;
context = new DataBindingContext();
eClass = EcoreFactory.eINSTANCE.createEClass();
eClass.setInstanceClass(customer.getClass());
eClass.setName("Customer");
}
@Override
protected Control createDialogArea(Composite parent) {
Composite control = new Composite(parent, SWT.NONE);
control.setLayout(new MigLayout("fill, wrap 2", "[200!]10[fill,
300!]"));
new Label(control, SWT.BORDER).setText("First Name");
Text firstName = new Text(control, SWT.BORDER);
firstName.setLayoutData("");
new Label(control, SWT.BORDER).setText("Last Name");
Text lastName = new Text(control, SWT.BORDER);
lastName.setLayoutData("");
new Label(control, SWT.BORDER).setText("Occupation");
Text occupation = new Text(control, SWT.BORDER);
occupation.setLayoutData("");
bind("firstName", firstName);
bind("lastName", lastName);
bind("occupation", occupation);
return control;
}
private void bind(String propertyName, Text text) {
EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute();
eAttribute.setName(propertyName);
eAttribute.setEType(EcorePackage.eINSTANCE.getEString());
eAttribute.setChangeable(true);
eClass.getEStructuralFeatures().add(eAttribute);
ISWTObservableValue targetObservable =
SWTObservables.observeText(text, SWT.Modify);
IObservableValue modelObservable =
EMFObservables.observeValue(entity, eAttribute);
getContext().bindValue(targetObservable, modelObservable,
new UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE),
new
UpdateValueStrategy(UpdateValueStrategy.POLICY_UPDATE));
}
public DataBindingContext getContext() {
return context;
}
}
</snippet>
Best regards,
Daniel