Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » EMF Databinding without generated models?
EMF Databinding without generated models? [message #485033] Thu, 10 September 2009 10:29 Go to next message
Daniel Rampanelli is currently offline Daniel RampanelliFriend
Messages: 8
Registered: July 2009
Junior Member
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 #485082 is a reply to message #485033] Thu, 10 September 2009 13:43 Go to previous messageGo to next message
Hallvard Traetteberg is currently offline Hallvard TraettebergFriend
Messages: 673
Registered: July 2009
Location: Trondheim, Norway
Senior Member
Daniel Rampanelli wrote:
>
> do you think it would be possible to use the EMF databinding without
> having EMF generate the code for the models?

EMF databinding may be used with both generated code and dynamic
instances created using the reflective API. I.e. it doesn't matter how
EObject instances for you model are instantiated, once you have them you
may bind to them.

Hallvard
Re: EMF Databinding without generated models? [message #485298 is a reply to message #485082] Fri, 11 September 2009 09:10 Go to previous messageGo to next message
Daniel Rampanelli is currently offline Daniel RampanelliFriend
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 Go to previous messageGo to next message
Hallvard Traetteberg is currently offline Hallvard TraettebergFriend
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
>
Re: EMF Databinding without generated models? [message #485347 is a reply to message #485335] Fri, 11 September 2009 12:55 Go to previous messageGo to next message
Daniel Rampanelli is currently offline Daniel RampanelliFriend
Messages: 8
Registered: July 2009
Junior Member
Yes, I'm sure that would work because until now I was using this method :-)

But since the JFace databinding lack of undo/redo support (which EMF
Databinding does), I decided to use these bindings but maybe I'm
completely wrong.

What I need to accomplish is to combine the undo/redo feature on editors
that handle hand-coded classes.

I hope someone can give me a hint on how to solve my issue!

Daniel
Re: EMF Databinding without generated models? [message #485348 is a reply to message #485347] Fri, 11 September 2009 13:12 Go to previous messageGo to next message
Eike Stepper is currently offline Eike StepperFriend
Messages: 6682
Registered: July 2009
Senior Member
Hi Daniel,

Adding undo/redo support to JFace databinding sounds like a good idea ;-)

Cheers
/Eike

----
http://thegordian.blogspot.com



Daniel Rampanelli schrieb:
> Yes, I'm sure that would work because until now I was using this
> method :-)
>
> But since the JFace databinding lack of undo/redo support (which EMF
> Databinding does), I decided to use these bindings but maybe I'm
> completely wrong.
>
> What I need to accomplish is to combine the undo/redo feature on
> editors that handle hand-coded classes.
>
> I hope someone can give me a hint on how to solve my issue!
>
> Daniel
>


Re: EMF Databinding without generated models? [message #485355 is a reply to message #485347] Fri, 11 September 2009 13:39 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Daniel,

You'd need a generated model to implement existing interfaces. I.e.,
you'd need to generate an ICustomerImpl that does the right
notifications and also implements the existing ICustomer interface. EMF
certainly supports that approach...


Daniel Rampanelli wrote:
> Yes, I'm sure that would work because until now I was using this
> method :-)
>
> But since the JFace databinding lack of undo/redo support (which EMF
> Databinding does), I decided to use these bindings but maybe I'm
> completely wrong.
>
> What I need to accomplish is to combine the undo/redo feature on
> editors that handle hand-coded classes.
>
> I hope someone can give me a hint on how to solve my issue!
>
> Daniel
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: EMF Databinding without generated models? [message #485375 is a reply to message #485347] Fri, 11 September 2009 14:17 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
Daniel Rampanelli wrote:
> But since the JFace databinding lack of undo/redo support

See https://bugs.eclipse.org/bugs/show_bug.cgi?id=116465
Re: EMF Databinding without generated models? [message #485612 is a reply to message #485375] Mon, 14 September 2009 07:08 Go to previous message
Daniel Rampanelli is currently offline Daniel RampanelliFriend
Messages: 8
Registered: July 2009
Junior Member
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
Previous Topic:Two features named in the model by the same name
Next Topic:Observing EList size
Goto Forum:
  


Current Time: Thu Apr 25 09:54:12 GMT 2024

Powered by FUDForum. Page generated in 0.05623 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top