[DataBinding] Databinding a ListViewer to a model list [message #335874] |
Thu, 07 May 2009 16:00 ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: markus.wolf.nmmn.com
Hi there,
I try to incorporate Databinding into our application and struggle with
a usecase we require. I cannot find any documentation on this so I ask here.
We have a model with a list property and we have a ListViewer displaying
this items. This does work fine. Also adding and removing works as
expected.
But we want to validate on changes that the list is not empty and show a
warning to the user. So far no problem if we could create a binding of
the two lists and add a IValidator. And this is the problem.
We created the code with a WritableList and the
ObservableListContentProvider but this does not give us a binding.
So how do we create a binding between the WritableList and our model or
the WritableList and the viewer? Which type of IOberservable is useful
for this?
Thanks for any help
Markus Wolf
--
NMMN - New Media Markets & Networks GmbH
Langbehnstrasse 6, 22761 Hamburg
Geschäftsführung: Kfm. Michael Schütt
Finanzamt HH-Altona - UStID DE 812 699 852 - HRB 71102 Hamburg
HypoVereinsbank - BLZ 200 300 00 - Konto-Nr. 156 29 82
http://www.nmmn.com
Tel.: +49 40 284 118-0 - Fax: +49 40 284118-999
Softwareentwicklung LLynch: -720
|
|
|
|
|
Re: [DataBinding] Databinding a ListViewer to a model list [message #335882 is a reply to message #335879] |
Thu, 07 May 2009 17:42 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: markus.wolf.nmmn.com
Hi Matthew,
> Revised:
>
> final WritableList items = ... // your WritableList
> MultiValidator validator = new MultiValidator() {
> protected IStatus validate() {
> if (items.isEmpty()) {
> return ValidationStatus.error("Missing required item(s)");
> }
> return ValidationStatus.ok();
> }
> };
> bindingContext.addValidationStatusProvider(validator); // important!
but how does this validator recognizes the list changes? When does it
evaluate?
After adding your code snippet I think that the validator is not
triggered. I could request the validation status on my own, but this
will not need the entire databinding since then i could check manually
on each UI update.
Regards
Markus Wolf
--
NMMN - New Media Markets & Networks GmbH
Langbehnstrasse 6, 22761 Hamburg
Geschäftsführung: Kfm. Michael Schütt
Finanzamt HH-Altona - UStID DE 812 699 852 - HRB 71102 Hamburg
HypoVereinsbank - BLZ 200 300 00 - Konto-Nr. 156 29 82
http://www.nmmn.com
Tel.: +49 40 284 118-0 - Fax: +49 40 284118-999
Softwareentwicklung LLynch: -720
|
|
|
|
Re: [DataBinding] Databinding a ListViewer to a model list [message #335899 is a reply to message #335893] |
Fri, 08 May 2009 08:39 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: markus.wolf.nmmn.com
Hi Matthew,
Thanks for your help so far. :)
>> I could request the validation status on my own, but this
>> will not need the entire databinding since then i could check manually
>> on each UI update.
>
> If you could provide a small example of the problematic code I think
> I'll be able to help a little more.
>
Here is some example code. Its not my complete code, because it would be
too much but I hope it is enough:
### snip ###
// These attributes I declare in my class:
private ListViewer viewer;
private WritableList nameservers;
private MultiValidator nameserversValidator;
// ---------------------------
// To create the GUI I use this:
this.viewer = new ListWithButtons().create(listParent, GridDataFactory
.fillDefaults().grab(true, false).hint(SWT.DEFAULT, 100)
.create(), getView().getToolkit());
this.viewer.setContentProvider(new ObservableListContentProvider());
// ---------------------------
// When I load my model I execute this code:
if (this.nameserversValidator != null) {
getView().getDataBindingContext()
.removeValidationStatusProvider(this.nameserversValidator);
}
this.nameservers = new WritableList(getBusinessObject()
.getNameservers(), Nameserver.class);
this.nameserversValidator = new MultiValidator() {
@Override
protected IStatus validate() {
if (NameserverPart.this.nameservers.isEmpty()) {
ValidationStatus.error("Select at least two nameservers");
}
return ValidationStatus.ok();
}
};
getView().getDataBindingContext().addValidationStatusProvide r(
this.nameserversValidator);
this.viewer.setInput(this.nameservers);
// ---------------------------
### snip ###
And I have to buttons in my GUI to add and remove its from the list.
There just call nameservers.add(object) and nameservers.remove(object).
But when adding or removing something no validation is triggered.
If you need some more code then please tell me.
Thanks
Markus Wolf
--
NMMN - New Media Markets & Networks GmbH
Langbehnstrasse 6, 22761 Hamburg
Geschäftsführung: Kfm. Michael Schütt
Finanzamt HH-Altona - UStID DE 812 699 852 - HRB 71102 Hamburg
HypoVereinsbank - BLZ 200 300 00 - Konto-Nr. 156 29 82
http://www.nmmn.com
Tel.: +49 40 284 118-0 - Fax: +49 40 284118-999
Softwareentwicklung LLynch: -720
|
|
|
|
Re: [DataBinding] Databinding a ListViewer to a model list [message #335914 is a reply to message #335912] |
Fri, 08 May 2009 16:25 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: markus.wolf.nmmn.com
Hi Matthew,
>> Here is some example code. Its not my complete code, because it would be
>> too much but I hope it is enough:
>
> I think I see what's happening. You are wrapping your getNameservers()
> list in a WritableList to get an observable:
>
>> this.nameservers = new WritableList(getBusinessObject()
>> .getNameservers(), Nameserver.class);
>
> Then you use that observable list in your MultiValidator.
>
>> this.nameserversValidator = new MultiValidator() {
>> @Override
>> protected IStatus validate() {
>> if (NameserverPart.this.nameservers.isEmpty()) {
>> ValidationStatus.error("Select at least two nameservers");
>> }
>> return ValidationStatus.ok();
>> }
>> };
>> getView().getDataBindingContext().addValidationStatusProvide r(
>> this.nameserversValidator);
>
> This part looks correct.
>
> I think your problem is here:
>
>> And I have to buttons in my GUI to add and remove its from the list.
>> There just call nameservers.add(object) and nameservers.remove(object).
>> But when adding or removing something no validation is triggered.
>
> A regular java.util.List does not have listener API, so it's impossible
> for WritableList to know that the list changed if you make changes
> directly to the wrapped list. So you need to add and remove elements on
> the WritableList.
>
But we use the WritableList.add() and .remove() methods. That should be
notable by the databinding I think.
> Does your BusinessObject class have event notification for the
> getNameservers() list? If so you could use a property observable to
> trap those changes and this would be another possible approach.
>
No, therefore we use the WritableList as a wrapper around it.
Regards
Markus Wolf
--
NMMN - New Media Markets & Networks GmbH
Langbehnstrasse 6, 22761 Hamburg
Geschäftsführung: Kfm. Michael Schütt
Finanzamt HH-Altona - UStID DE 812 699 852 - HRB 71102 Hamburg
HypoVereinsbank - BLZ 200 300 00 - Konto-Nr. 156 29 82
http://www.nmmn.com
Tel.: +49 40 284 118-0 - Fax: +49 40 284118-999
Softwareentwicklung LLynch: -720
|
|
|
|
Re: [DataBinding] Databinding a ListViewer to a model list [message #335946 is a reply to message #335921] |
Mon, 11 May 2009 07:14 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: markus.wolf.nmmn.com
Hi Matthew,
>>>> And I have to buttons in my GUI to add and remove its from the list.
>>>> There just call nameservers.add(object) and nameservers.remove(object).
>>>> But when adding or removing something no validation is triggered.
>>> A regular java.util.List does not have listener API, so it's impossible
>>> for WritableList to know that the list changed if you make changes
>>> directly to the wrapped list. So you need to add and remove elements on
>>> the WritableList.
>>>
>> But we use the WritableList.add() and .remove() methods. That should be
>> notable by the databinding I think.
>
> And all your adds and removes and done on the same WritableList
> instance, right? You're not creating several WritableLists on the same
> java.util.List instance?
>
No, as you could see in the code I posted in a former message we are
removing the binding from the DatabindingContext before creating a new
WritableList. This is required when we reload our business model from
database to synchronize the database state to the application model.
But that it.
Regards
Markus Wolf
--
NMMN - New Media Markets & Networks GmbH
Langbehnstrasse 6, 22761 Hamburg
Geschäftsführung: Kfm. Michael Schütt
Finanzamt HH-Altona - UStID DE 812 699 852 - HRB 71102 Hamburg
HypoVereinsbank - BLZ 200 300 00 - Konto-Nr. 156 29 82
http://www.nmmn.com
Tel.: +49 40 284 118-0 - Fax: +49 40 284118-999
Softwareentwicklung LLynch: -720
|
|
|
Re: [DataBinding] Databinding a ListViewer to a model list [message #335954 is a reply to message #335899] |
Mon, 11 May 2009 14:45 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: markus.wolf.nmmn.com
Hi Matthew,
I found the problem. Its my fault. In the posted code snippet the
ValidationStatus.error(...) was missing the return statement. Therefore
the Validator always returns ValiationStatus.ok(...).
Thanks for all your help :)
Markus Wolf
--
NMMN - New Media Markets & Networks GmbH
Langbehnstrasse 6, 22761 Hamburg
Geschäftsführung: Kfm. Michael Schütt
Finanzamt HH-Altona - UStID DE 812 699 852 - HRB 71102 Hamburg
HypoVereinsbank - BLZ 200 300 00 - Konto-Nr. 156 29 82
http://www.nmmn.com
Tel.: +49 40 284 118-0 - Fax: +49 40 284118-999
Softwareentwicklung LLynch: -720
|
|
|
Powered by
FUDForum. Page generated in 0.04312 seconds