|
|
Re: CCombo and Databinding [message #330251 is a reply to message #330214] |
Wed, 23 July 2008 06:31 |
Matthias Schoettle Messages: 44 Registered: July 2009 |
Member |
|
|
Matthew Hall wrote:
> Matthias Schoettle wrote:
>> I have a list of objects and a CCombo which shows the name attribute
>> of these objects. I want to bind the objects that when an item is
>> added/removed from the CCombo the model gets updated (i.e. I want to
>> create/delete objects from the list). Is this possible?
>
> This is easier to do if you use a combo viewer:
>
> ComboViewer viewer = new ComboViewer(ccombo);
> ObservableListContentProvider cp = new ObservableListContentProvider();
> viewer.setContentProvider(cp);
> // (Substitute the actual class and property names in your code)
> IObservableMap itemNames = BeansObservables.observeMap(
> cp.getKnownElements(), YourModelClass.class, "name");
> viewer.setLabelProvider(new ObservableMapLabelProvider(itemNames));
>
> IObservableList list = BeansObservables.observeList(
> yourModelContainer, "modelList"); // (substitute real property name)
> viewer.setInput(list);
>
> In this setup, any additions or removals to the observable list will be
> automatically forwarded to the viewer. This is because
> ObservableListContentProvider monitors the input observable list for
> changes and updates the viewer accordingly.
>
> As for copying changes back to the model, there are a few different ways
> I've seen folks do this:
>
> 1) Add and remove elements on the input observable list. Because this
> list is backed by your model, changes to the list will be propagated
> back to the model.
>
> 2) Add and remove elements directly on the model. As long as your model
> objects fire property change events for each change, the changes will
> show up immediately in the combo.
>
> Hope this helps,
>
> Matthew
Thanks Matthew, works great. I only need to refresh the ComboViewer when
removing the current selected object as the selection doesn't change.
Matthias
|
|
|
Powered by
FUDForum. Page generated in 0.03504 seconds