Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Need simple example bind EMF-Model value to simple swt widget(I need an simple example for binding a simple attribute of an emf moddeled object, to s simple swt widget)
Need simple example bind EMF-Model value to simple swt widget [message #1142881] Thu, 17 October 2013 22:47 Go to next message
Tim Hinkes is currently offline Tim HinkesFriend
Messages: 4
Registered: October 2013
Junior Member
Hello,

i'm currently working on my first real emf-based Project.
And additionally this is my first bigger GUI Project, so i have problems with GUI "best practices".

My problem with emf is, to understand how i should use the item/label/content providers

The label provider does somehow provide the actual Text to be displayed by somekind of widget (lets say a SWT Label).

The content Provider provides the children of an object (for a tree where objects do have kind of reference lists of other objects, there the content provider provides the entries of this list)

I somehow managed to understand how treeviewer or tableviewer/listviewer are working with these providers, the call their appropriate methods like getImage() on the label provider.

My question now is, how can these Label/Content/item-Providers can help me with my simple text widgets where a widget should just show(and update model->view) one single attribute? This is just for my understanding, of course i know how to get a single attribute out of an object and show it, even update it by writing my own eventlistener class which i can add to the objects listeners.

So there is my object.

Class Foo{
String name
String dailyLimit
String monthlyLimit
}

Now in the gui class

Label nameLabel = new Label();
lable.setText(fooInstance.getName());



this would give me a label with the name.
But how could i use emf-magic here to make it more simple for me to keep the text in sync?

In the long term i want to modify a lableProvider to give me something like
String dailyLimit = foo.getDailyLimit();
String monthlyLimit = foo.getMonthlyLimit();

label.setText(dailyLimit +"/"+ monthlyLimit);


so i dont have to add another Method to the model, just for needs of the view.
Becaus in the Tutrials they say something about modifying the LabelProviders to fit my needs for my "label"-texts/strings


Thank you in advance,
Timmeey
Re: Need simple example bind EMF-Model value to simple swt widget [message #1143805 is a reply to message #1142881] Fri, 18 October 2013 12:40 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

Hi, Timmeey,

Indeed, the tree editor generated by EMF itself provides a good example
of using the item-provider-based content providers. For text widgets,
such as you might present for "details" of objects in a form-like UI, I
prefer to use the EMF.Edit implementation of Eclipse Data Bindings.

Your one-stop shop for excellent EMF tutorial material is Lars Vogel's
website, and this is no exception:

http://www.vogella.com/articles/EclipseDataBindingEMF/article.html

HTH,

Christian


On 2013-10-17 22:47:28 +0000, Tim Hinkes said:

> Hello,
>
> i'm currently working on my first real emf-based Project.
> And additionally this is my first bigger GUI Project, so i have
> problems with GUI "best practices".
>
> My problem with emf is, to understand how i should use the
> item/label/content providers
>
> The label provider does somehow provide the actual Text to be displayed
> by somekind of widget (lets say a SWT Label).
>
> The content Provider provides the children of an object (for a tree
> where objects do have kind of reference lists of other objects, there
> the content provider provides the entries of this list)
>
> I somehow managed to understand how treeviewer or
> tableviewer/listviewer are working with these providers, the call their
> appropriate methods like getImage() on the label provider.
>
> My question now is, how can these Label/Content/item-Providers can help
> me with my simple text widgets where a widget should just show(and
> update model->view) one single attribute? This is just for my
> understanding, of course i know how to get a single attribute out of an
> object and show it, even update it by writing my own eventlistener
> class which i can add to the objects listeners.
>
> So there is my object.
>
>
> Class Foo{
> String name
> String dailyLimit
> String monthlyLimit
> }
>
> Now in the gui class
>
> Label nameLabel = new Label();
> lable.setText(fooInstance.getName());
>
>
>
> this would give me a label with the name.
> But how could i use emf-magic here to make it more simple for me to
> keep the text in sync?
>
> In the long term i want to modify a lableProvider to give me something like
>
> String dailyLimit = foo.getDailyLimit();
> String monthlyLimit = foo.getMonthlyLimit();
>
> label.setText(dailyLimit +"/"+ monthlyLimit);
>
>
> so i dont have to add another Method to the model, just for needs of the view.
> Becaus in the Tutrials they say something about modifying the
> LabelProviders to fit my needs for my "label"-texts/strings
>
>
> Thank you in advance,
> Timmeey
Re: Need simple example bind EMF-Model value to simple swt widget [message #1143865 is a reply to message #1142881] Fri, 18 October 2013 13:27 Go to previous messageGo to next message
Felix Dorner is currently offline Felix DornerFriend
Messages: 295
Registered: March 2012
Senior Member
With EMF Databinding, I did this to bind a form text (like a label..)

dataBindingContext = new EMFDataBindingContext();
final IObservableValue formTextHandler = new
WritableValue(version.getName(), String.class);
formTextHandler.addChangeListener(new IChangeListener(){
@Override
public void handleChange(ChangeEvent event_p) {
getManagedForm().getForm().setText(version.getName());
}
});
dataBindingContext.bindValue(formTextHandler,
EMFEditProperties.value(getEditingDomainFor(version),
VersioningPackage.Literals.VERSION__NAME).observe(version));


This doesn't use a label provider but goes right through to the model.
Maybe there's a nicer way, but I couldn't find a better way to do
"one-way" bindings to labels/form texts, so this is almost no
improvement over using raw adapters..


The one thing you should take care for is to not add more and more and
more adapters to your objects, independently of which facility you use
(roll your own, databinding, adapterfactories..)
Re: Need simple example bind EMF-Model value to simple swt widget [message #1143872 is a reply to message #1143805] Fri, 18 October 2013 13:32 Go to previous messageGo to next message
Tim Hinkes is currently offline Tim HinkesFriend
Messages: 4
Registered: October 2013
Junior Member
Thank you Christian,

in fact i did read the vogella.com EMFTutorials prior to posting.
And my simple GUI was working with the standard databinding.

Maybe my question was a little bit too unspecific.

It is about, if there is an abstraction layer between the Model and the "observeable" which i bind my gui to. Some layer which i can extend/inherit/implement to realize some kind of data aggregation for my Gui-TextFields, so that i can combine firstName and lastName (stored as 2 attributes in the model) to get the whole name in one field in the gui, without modifying the model.

But anyway thank you Smile
Re: Need simple example bind EMF-Model value to simple swt widget [message #1144215 is a reply to message #1143872] Fri, 18 October 2013 18:31 Go to previous message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

Hi, Tim,

I see. I think perhaps I didn't read far enough (or carefully enough)
your original post. Let me repeat that mistake. ;-)

The Data Bindings framework has a ComputedValue that may help: you can
create a ComputedValue that is derived from observables on the
first-name and last-name properties; it can combine the names for
model-to-view updates. I'm not sure about handling the inverse
calculation in view-to-model updates.

For bidirectional bindings, perhaps an Adapter-based approach as
alluded to by Felix would be more practical ...

HTH,

Christian


On 2013-10-18 13:32:39 +0000, Tim Hinkes said:

> Thank you Christian,
>
> in fact i did read the vogella.com EMFTutorials prior to posting.
> And my simple GUI was working with the standard databinding.
>
> Maybe my question was a little bit too unspecific.
>
> It is about, if there is an abstraction layer between the Model and the
> "observeable" which i bind my gui to. Some layer which i can
> extend/inherit/implement to realize some kind of data aggregation for
> my Gui-TextFields, so that i can combine firstName and lastName (stored
> as 2 attributes in the model) to get the whole name in one field in the
> gui, without modifying the model.
>
> But anyway thank you :)
Previous Topic:Cannot add an Inheritance from a new EClass to an EClass imported from another ecore
Next Topic:Building a Bidirectional Containment Map
Goto Forum:
  


Current Time: Thu Mar 28 09:21:21 GMT 2024

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

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

Back to the top