Home » Modeling » EMF » EMF Databinding without generated models?
EMF Databinding without generated models? [message #485033] |
Thu, 10 September 2009 06:29  |
Eclipse User |
|
|
|
Hi,
do you think it would be possible to use the EMF databinding without
having EMF generate the code for the models?
I wanted to do this, because until now I was using JFace databindings, but
unfortunately, they lack support for undo/redo. So I thought that maybe I
could take advantage of the features integrated in EMF.
Thanks in advance,
Daniel
|
|
| |
Re: EMF Databinding without generated models? [message #485298 is a reply to message #485082] |
Fri, 11 September 2009 05:10   |
Eclipse User |
|
|
|
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 08:19   |
Eclipse User |
|
|
|
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
>
|
|
| | | | |
Re: EMF Databinding without generated models? [message #485612 is a reply to message #485375] |
Mon, 14 September 2009 03:08  |
Eclipse User |
|
|
|
I see, now I've understood a little better the situation. I've decided to
create a solution by myself using custom observables and the undo/redo
framework present in Eclipse (IUndoableOperation & Co.).
Thanks to all for the help/contribution.
Daniel
|
|
|
Goto Forum:
Current Time: Sun Jul 20 03:04:03 EDT 2025
Powered by FUDForum. Page generated in 0.05392 seconds
|