Home » Modeling » EMF » EMF Databinding without generated models?
| |
Re: EMF Databinding without generated models? [message #485298 is a reply to message #485082] |
Fri, 11 September 2009 09:10 |
Daniel Rampanelli Messages: 8 Registered: July 2009 |
Junior Member |
|
|
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.IObservableVal ue;
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
|
|
|
Re: EMF Databinding without generated models? [message #485335 is a reply to message #485298] |
Fri, 11 September 2009 12:19 |
Hallvard Traetteberg Messages: 673 Registered: July 2009 Location: Trondheim, Norway |
Senior Member |
|
|
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
>
|
|
| | | | | |
Goto Forum:
Current Time: Fri Apr 26 05:24:42 GMT 2024
Powered by FUDForum. Page generated in 0.04730 seconds
|