Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Databinding To Combo?  () 1 Vote
Databinding To Combo? [message #498658] Tue, 17 November 2009 22:20 Go to next message
Ryan is currently offline RyanFriend
Messages: 74
Registered: July 2009
Location: Indiana
Member
I'm looking for information on EMF databinding to a combo box. There are at least 4 scenarios that I'd like to see demonstrated:
Filling the combo with values from an enum and setting a variable in the model to the selected enum literal, fill the combo with objects from the model and set a variable to the selected object, fill the combo in the gui and based on the selected index, set a variable in the model, and fill the combo in the gui and set a string based on the text of the selection.

I've looked around for this, but so far have come up empty handed. I'm not sure if this is more of a databinding issue, or a combination of both. I figured I'd ask here, as the first two scenarios seem like the best solution, but I have a few very simple combos that putting the values in the model either as enums or as an instance of some object really doesn't make sense.

Thanks,
Ryan
Re: Databinding To Combo? [message #498672 is a reply to message #498658] Wed, 18 November 2009 00:08 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Hi,

This is OTOH so there might be class spelling errors

enum E { A, B, C }


class M {
E myEnum;
O obj;
int index;
String s;
}

class N {
List<O> o;
}

class O {
String s;
}


UC1:
====
M myObj = ....

ComboViewer v = new ComboViewer(p);
v.setContentProvider(new ArrayContentProvider());
v.setLabelProvider(new LabelProvider());
v.setInput(E.values());

EMFDatabindingContext dbc = new EMFDatabindingContext();
IEMFProperty m = EMFProperties.value(MyPackage.Literals.M__MY_ENUM);
IValueProperty t = ViewerProperties.singleSelection();
dbc.bindValue(t.observe(v),m.oberve(myObj));


UC2:
====
M myObj = ...
N listObj = ...

ComboViewer v = new ComboViewer(p);
ObservableListContentProvider pv = new ObservableListContentProvider();
v.setContentProvider(pv);

IObservableMap m =
EMFProperties.value(MyPackage.Literals.O__S).observeDetail(p v.getKnowElements());
v.setLabelProvider(new ObservableMapLabelProvider(m))

IEMFListProperty p = EMFProperties.list(MyPackage.Literals.N__O);
v.setInput(p.observe(listObj));

IEMFProperty m = EMFProperties.value(MyPackage.Literals.M__OBJ);
IValueProperty t = ViewerProperties.singleSelection();
dbc.bindValue(t.observe(v),m.oberve(myObj));

I'm not sure I understand UC3 + 4. Do you want an editable combo or
simply do some value conversion (In UC 3 you might simply want to
observe the selection index using the WidgetProperties-factory).

Please also take a look at my Databinding Tutorial I published as part
of Eclipse Galileo [1].

Tom

[1] http://tomsondev.bestsolution.at/2009/06/06/galileo-improved -emf-databinding-support/

Ryan schrieb:
> I'm looking for information on EMF databinding to a combo box. There
> are at least 4 scenarios that I'd like to see demonstrated:
> Filling the combo with values from an enum and setting a variable in the
> model to the selected enum literal, fill the combo with objects from the
> model and set a variable to the selected object, fill the combo in the
> gui and based on the selected index, set a variable in the model, and
> fill the combo in the gui and set a string based on the text of the
> selection.
>
> I've looked around for this, but so far have come up empty handed. I'm
> not sure if this is more of a databinding issue, or a combination of
> both. I figured I'd ask here, as the first two scenarios seem like the
> best solution, but I have a few very simple combos that putting the
> values in the model either as enums or as an instance of some object
> really doesn't make sense.
>
> Thanks,
> Ryan
Re: Databinding To Combo? [message #498780 is a reply to message #498672] Wed, 18 November 2009 15:41 Go to previous messageGo to next message
Ryan is currently offline RyanFriend
Messages: 74
Registered: July 2009
Location: Indiana
Member
Thanks Tom for the prompt reply.

Tom Schindl wrote on Tue, 17 November 2009 19:08

...

I'm not sure I understand UC3 + 4. Do you want an editable combo or
simply do some value conversion (In UC 3 you might simply want to
observe the selection index using the WidgetProperties-factory).

Please also take a look at my Databinding Tutorial I published as part
of Eclipse Galileo [1].

Tom

[1] http://tomsondev.bestsolution.at/2009/06/06/galileo-improved -emf-databinding-support/



I've read through the tutorial and have used the properties api to link to nested attributes. I'm still a little new to all of this and trying to wrap my head around it.

For UC3, I was thinking as an example, something like the months. So the combo would be filled by the gui class with values Jan, Feb, Mar, etc., but the field in my model would be an int, so the selected index(+1) would be the month of the year.

For UC4, I was thinking as above, the gui would fill in the combo with the months, but my field in the model would be a string to hold the month name.

In all of these instances, the combo would be a read-only combo.

I'd like to perhaps also do something fancy, where I have an editable combo, that validates the user input against the data in the list, such that when they type in a valid input, and leave the field, the proper field is selected. The value that can be assigned is still limited to what is in the combo, just the user can type what they want. So, ie. I would have an editable combo box, that the user could type "1", or "Jan" or "January" and when they leave the field or hit enter, etc., then the combo would select the first option, and if they typed "Aug", it would select the August option. Would this be done with update strategies?

Again, I appreciate your help and your tutorials, as I wouldn't be as far as I am without them.

Thanks,
Ryan
Re: Databinding To Combo? [message #498795 is a reply to message #498780] Wed, 18 November 2009 16:24 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Ryan schrieb:
> Thanks Tom for the prompt reply.
>
> Tom Schindl wrote on Tue, 17 November 2009 19:08
>> ...
>>
>> I'm not sure I understand UC3 + 4. Do you want an editable combo or
>> simply do some value conversion (In UC 3 you might simply want to
>> observe the selection index using the WidgetProperties-factory).
>>
>> Please also take a look at my Databinding Tutorial I published as part
>> of Eclipse Galileo [1].
>>
>> Tom
>>
>> [1] http://tomsondev.bestsolution.at/2009/06/06/galileo-improved
>> -emf-databinding-support/
>
>
> I've read through the tutorial and have used the properties api to link
> to nested attributes. I'm still a little new to all of this and trying
> to wrap my head around it.
>
> For UC3, I was thinking as an example, something like the months. So
> the combo would be filled by the gui class with values Jan, Feb, Mar,
> etc., but the field in my model would be an int, so the selected
> index(+1) would be the month of the year.

Use WidgetProperties and observe the selection index.

>
> For UC4, I was thinking as above, the gui would fill in the combo with
> the months, but my field in the model would be a string to hold the
> month name.
>

That's very similar to UC1:

M myObj = ....

ComboViewer v = new ComboViewer(p);
v.setContentProvider(new ArrayContentProvider());
v.setLabelProvider(new LabelProvider());
v.setInput(new String[] { "Jan", "Feb", ... });

EMFDatabindingContext dbc = new EMFDatabindingContext();
IEMFProperty m = EMFProperties.value(MyPackage.Literals.M__S);
IValueProperty t = ViewerProperties.singleSelection();
dbc.bindValue(t.observe(v),m.oberve(myObj));

> In all of these instances, the combo would be a read-only combo.
>
> I'd like to perhaps also do something fancy, where I have an editable
> combo, that validates the user input against the data in the list, such
> that when they type in a valid input, and leave the field, the proper
> field is selected. The value that can be assigned is still limited to
> what is in the combo, just the user can type what they want. So, ie. I
> would have an editable combo box, that the user could type "1", or "Jan"
> or "January" and when they leave the field or hit enter, etc., then the
> combo would select the first option, and if they typed "Aug", it would
> select the August option. Would this be done with update strategies?

I'm not sure at which point an editable combo fires a selection event
but you might have to use UpdateValueStrategy.POLICY_NEVER and do the
sync your own but I could be wrong.

>
> Again, I appreciate your help and your tutorials, as I wouldn't be as
> far as I am without them.
>
> Thanks,
> Ryan

Another combo like control you might be interested in is a
ProposalViewer I wrote for my UFaceKit-Project.

[1] http://tomsondev.bestsolution.at/2009/07/15/ufacekit-and-jav a5-viewers/
Re: Databinding To Combo? [message #499245 is a reply to message #498795] Fri, 20 November 2009 14:46 Go to previous messageGo to next message
Ryan is currently offline RyanFriend
Messages: 74
Registered: July 2009
Location: Indiana
Member
Tom,
That control may work for me, as I was basically trying to find a way to have a keyboard selectable combo box, which I think is what your control tries to solve?

I understand much better how to do the databinding to the combo, however, I'm still a little bit confused on another issue. I can pull in the data to fill the combo from the model, but now I want to take it a step further, and provide i18n support and have the combo fill with proper translated strings. Does that relegate me to pulling the strings out of the model, filling the combo elsewhere, then bind the model attribute to the selection? Or does EMF provide a way to have i18n literals?

Thanks,
Ryan

[Updated on: Fri, 20 November 2009 14:47]

Report message to a moderator

Re: Databinding To Combo? [message #499252 is a reply to message #499245] Fri, 20 November 2009 14:56 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Ryan,

The .edit plugin provides a *.properties file with translated versions
of all the names in your models. They uses stylized names that allow
you to easily compute the key from the model information...


Ryan wrote:
> Tom,
> That control may work for me, as I was basically trying to find a way
> to have a keyboard selectable combo box, which I think is what your
> control tries to solve?
>
> I understand much better how to do the databinding to the combo,
> however, I'm still a little bit confused on another issue. I can pull
> in the data to fill the combo from the model, but now I want to take
> it a step further, and provide i18n support and have the combo fill
> with proper translated strings. Does that relegate me to pulling the
> strings out of the model and binding to the selection? Or does EMF
> provide a way to have i18n literals?
>
> Thanks,
> Ryan


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Databinding To Combo? [message #507587 is a reply to message #499252] Wed, 13 January 2010 23:15 Go to previous messageGo to next message
Ryan is currently offline RyanFriend
Messages: 74
Registered: July 2009
Location: Indiana
Member
Just got back around to the i18n issue. I was using Tom's suggestion to fill the combo box:

v.setInput(E.values());

the values() method, does that pull the values from the .ecore file or from the .edit plugin's .properties file?
Re: Databinding To Combo? [message #507712 is a reply to message #507587] Thu, 14 January 2010 12:45 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Ryan,

No, I don't think so. I'm not sure if a label provider kicks in later
to do that in this scenario.


Ryan wrote:
> Just got back around to the i18n issue. I was using Tom's suggestion
> to fill the combo box:
>
> v.setInput(E.values());
>
> the values() method, does that pull the values from the .ecore file or
> from the .edit plugin's .properties file?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Databinding To Combo? [message #521865 is a reply to message #507712] Fri, 19 March 2010 00:27 Go to previous messageGo to next message
Ryan is currently offline RyanFriend
Messages: 74
Registered: July 2009
Location: Indiana
Member
I just got back around to this issue. The .values() method pulls the enum values from the model, and the generic LabelProvider() pulls the name or literal from the model itself; basically, no knowledge of the .edit plugin's .properties file. The .edit plugin's AdapterFactoryLabelProvider doesn't have a way to get at the enum literals in the .properties file. What is the recommended way to create a label provider for the combo box that pulls the literals from the .properties file?
Re: Databinding To Combo? [message #521930 is a reply to message #521865] Fri, 19 March 2010 06:10 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Ryan,

Using the name of the enumerator's data type and the name of the
enumerator you can compose the key used in the plugin.properties file:

XyzEditPlugin.INSTANCE.getString
("_UI_" + eDataType.getName() + "_" +
((Enumerator)value).getName() + "_literal");

Ryan wrote:
> I just got back around to this issue. The .values() method pulls the
> enum values from the model, and the generic LabelProvider() pulls the
> name or literal from the model itself; basically, no knowledge of the
> .edit plugin's .properties file. The .edit plugin's
> AdapterFactoryLabelProvider doesn't have a way to get at the enum
> literals in the .properties file. What is the recommended way to
> create a label provider for the combo box that pulls the literals from
> the .properties file?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Databinding To Combo? [message #521964 is a reply to message #521930] Fri, 19 March 2010 13:31 Go to previous messageGo to next message
Ryan is currently offline RyanFriend
Messages: 74
Registered: July 2009
Location: Indiana
Member
Thanks Ed for the quick reply.
I started to look through the code that populates the combo boxes in the properties view, because I knew it was getting the labels from the .properties file, and saw that the code is basically what you replied with. There is a generic implementation in EMF and nothing special in the generated .edit plugin for enums.

Where should this code be added in the .edit plugin? Should I make a new EnumItemProvider for the adapterfactory to use? If I did that, how would the adapterfactory get that provider based on the java enum it would be selecting on? Or, do I need a different item provider for each enum in my model?

Is there a reason that a generated label provider isn't generated for the enums and booleans?

Ryan
Re: Databinding To Combo? [message #521966 is a reply to message #521964] Fri, 19 March 2010 08:52 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
Ryan,

Enumerator values aren't EObjects and hence aren't Notifiers and don't
support Adapters and as such, item providers don't apply. Of course
such values can be converted just to their string value, which is
typically fine. But sometimes folks like to translate them, so we
enable that too in the way you see.


Ryan wrote:
> Thanks Ed for the quick reply.
> I started to look through the code that populates the combo boxes in
> the properties view, because I knew it was getting the labels from the
> .properties file, and saw that the code is basically what you replied
> with. There is a generic implementation in EMF and nothing special in
> the generated .edit plugin for enums.
>
> Where should this code be added in the .edit plugin? Should I make a
> new EnumItemProvider for the adapterfactory to use? If I did that,
> how would the adapterfactory get that provider based on the java enum
> it would be selecting on? Or, do I need a different item provider for
> each enum in my model?
>
> Is there a reason that a generated label provider isn't generated for
> the enums and booleans?
>
> Ryan


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Databinding To Combo? [message #522030 is a reply to message #521966] Fri, 19 March 2010 17:24 Go to previous messageGo to next message
Ryan is currently offline RyanFriend
Messages: 74
Registered: July 2009
Location: Indiana
Member
Ed,
I have run into this issue because I need to have translated strings. Is there any way to reuse the org.eclipse.emf.edit.provider.ItemPropertyDescriptor that has this code for enums in a non-properties view? The combo boxes that show up in the generated properties view behave exactly like I'd like the ones in my custom gui to behave. I understand why the adapters can't be used on it since it ends up being a standard java enum, but the itempropertydescriptor shows that there is a generic way to access these string values. I should be able to tap into that generic code, but I haven't been clever enough to do so.

[Updated on: Fri, 19 March 2010 17:26]

Report message to a moderator

Re: Databinding To Combo? [message #522033 is a reply to message #522030] Fri, 19 March 2010 17:48 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------010702080409020203010108
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Ryan,

If you know the EObject has a property descriptor for the feature that
you're binding and you have an adapter factory for that EObject's item
providers, you could do this:

LibraryItemProviderAdapterFactory libraryItemProviderAdapterFactory
= new LibraryItemProviderAdapterFactory();
AdapterFactoryItemDelegator adapterFactoryItemDelegator = new
AdapterFactoryItemDelegator(libraryItemProviderAdapterFactor y);
Book book = LibraryFactory.eINSTANCE.createBook();
IItemPropertyDescriptor categoryPropertyDescriptor =
adapterFactoryItemDelegator.getPropertyDescriptor(book,
LibraryPackage.Literals.BOOK__CATEGORY);
IItemLabelProvider labelProvider =
categoryPropertyDescriptor.getLabelProvider(book);
System.out.println(labelProvider.getText(book.getCategory()) );


Ryan wrote:
> Ed,
> I have run into this issue because I need to have translated strings.
> Is there any way to reuse the itempropertydescriptor that has this
> code for enums in a non-properties view? The combo boxes that show up
> in the generated properties view behave exactly like I'd like the ones
> in my custom gui to behave. I understand why the adapters can't be
> used on it since it ends up being a standard java enum, but the
> itempropertydescriptor shows that there is a generic way to access
> these string values. I should be able to tap into that generic code,
> but I haven't been clever enough to do so.

--------------010702080409020203010108
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 8bit

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Ryan,<br>
<br>
If you know the EObject has a property descriptor for the feature that
you're binding and you have an adapter factory for that EObject's item
providers, you could do this:<br>
<blockquote>LibraryItemProviderAdapterFactory
libraryItemProviderAdapterFactory = new
LibraryItemProviderAdapterFactory();<br>
AdapterFactoryItemDelegator adapterFactoryItemDelegator = new
AdapterFactoryItemDelegator(libraryItemProviderAdapterFactor y); <br>
Book book = LibraryFactory.eINSTANCE.createBook();<br>
IItemPropertyDescriptor categoryPropertyDescriptor =
adapterFactoryItemDelegator.getPropertyDescriptor(book,
LibraryPackage.Literals.BOOK__CATEGORY);<br>
IItemLabelProvider labelProvider =
categoryPropertyDescriptor.getLabelProvider(book);<br>
System.out.println(labelProvider.getText(book.getCategory()) ); <br>
</blockquote>
<br>
Ryan wrote:
<blockquote cite="mid:ho0c10$enn$1@build.eclipse.org" type="cite">Ed,
<br>
I have run into this issue because I need to have translated strings. 
Is there any way to reuse the itempropertydescriptor that has this code
for enums in a non-properties view?  The combo boxes that show up in
the generated properties view behave exactly like I'd like the ones in
my custom gui to behave.  I understand why the adapters can't be used
on it since it ends up being a standard java enum, but the
itempropertydescriptor shows that there is a generic way to access
these string values.  I should be able to tap into that generic code,
but I haven't been clever enough to do so.  </blockquote>
</body>
</html>

--------------010702080409020203010108--


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Databinding To Combo? [message #522042 is a reply to message #522033] Fri, 19 March 2010 18:30 Go to previous messageGo to next message
Ryan is currently offline RyanFriend
Messages: 74
Registered: July 2009
Location: Indiana
Member
Thanks Ed,
I see now how to get at some of that stuff. I tweaked it a slight bit
LibraryItemProviderAdapterFactory libraryItemProviderAdapterFactory = new LibraryItemProviderAdapterFactory();
AdapterFactoryItemDelegator adapterFactoryItemDelegator = new AdapterFactoryItemDelegator(libraryItemProviderAdapterFactory);
Book book = LibraryFactory.eINSTANCE.createBook();
IItemPropertyDescriptor categoryItemPropertyDescriptor = adapterFactoryItemDelegator.getPropertyDescriptor(book, LibraryPackage.Literals.BOOK__CATEGORY);
PropertyDescriptor categoryPropertyDescriptor = new PropertyDescriptor(book, categoryItemPropertyDescriptor);

comboViewer.setContentProvider(new ArrayContentProvider());
comboViewer.setLableProvider(categoryPropertyDescriptor.getLabelProvider());
comboViewer.setInput(Category.values());



This seemed to work well to get the strings from the .properties file into the combo box. It would be nice if it were possible to do something similar but without the need for the reference to the book object.
Re: Databinding To Combo? [message #833345 is a reply to message #498672] Sat, 31 March 2012 10:20 Go to previous messageGo to next message
Bernard Sarter is currently offline Bernard SarterFriend
Messages: 88
Registered: August 2011
Location: Paris, France
Member

Hello,

Sorry to re-open this old post, but I'm exactly in the situation described by "UC2".

I'm using Eclipse 3.7.2, and I achieved to get the combo initialized with the content of a EList (so the first part is Ok), but I failed to do the databinding between the selection of the combo and an object of my EMF model.

In short, the three lines:

IEMFProperty m = EMFProperties.value(MyPackage.Literals.M__OBJ);
IValueProperty t = ViewerProperties.singleSelection();
dbc.bindValue(t.observe(v),m.oberve(myObj));


seem to be no longer valid with Eclipse 3.7.2 (especially the m.observe(myObj), and don't know by what to replace them (I made of course various assumptions and tests, but without success).

Any help welcome,

Thanks and best regards,
Bernard.
Re: Databinding To Combo? [message #833367 is a reply to message #833345] Sat, 31 March 2012 11:06 Go to previous message
Bernard Sarter is currently offline Bernard SarterFriend
Messages: 88
Registered: August 2011
Location: Paris, France
Member
Ok, it works fine with 3.7.2 in fact (stupid me) ...

IEMFValueProperty m = EMFProperties.value(MyPackage.Literals.M__OBJ);

Previous Topic:[CDO][Hibernate] ClassCastException org.eclipse.emf.cdo.server.internal.hibernate.tuplizer.Hibernate
Next Topic:Refactoring the generated editor
Goto Forum:
  


Current Time: Fri Apr 26 15:38:05 GMT 2024

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

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

Back to the top