Home » Modeling » EMF » Databinding To Combo?
() 1 Vote
Databinding To Combo? [message #498658] |
Tue, 17 November 2009 22:20 |
Ryan 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 |
Thomas Schindl 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 |
Ryan 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 |
Thomas Schindl 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 |
Ryan 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 |
Ed Merks 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 #522030 is a reply to message #521966] |
Fri, 19 March 2010 17:24 |
Ryan 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 |
Ed Merks 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 #833345 is a reply to message #498672] |
Sat, 31 March 2012 10:20 |
Bernard Sarter 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.
|
|
| |
Goto Forum:
Current Time: Fri Apr 26 15:38:05 GMT 2024
Powered by FUDForum. Page generated in 0.04818 seconds
|