[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.tools.emf] Re: EMF Databinding without generated models?

Daniel,

I think I understand what you're trying to do. You have an object of an existing (hand-coded) class (ICustomer), and create an EClass corresponding to this class, to which you add EAttributes corresponding to Text fields in a form and presumably also properties/fields of the ICustomer class. This way of mixing existing classes AND dynamic EClasses will not work.

What could create an instance (EObject) of the EClass and bind EAttributes of this EObject to Text fields in the form. But this would still not bind to the existing ICustomer object.

Why don't you bind to properties of the ICustomer directly, using BeansObservables (assuming it has appropriate get/set-methods)?

Hallvard

Daniel Rampanelli wrote:
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