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

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 org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.emf.databinding.EMFDataBindingContext;
import org.eclipse.emf.databinding.EMFObservables;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EcoreFactory;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.jface.databinding.swt.ISWTObservableValue;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

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