Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » [DataBinding] Databinding a ListViewer to a model list
[DataBinding] Databinding a ListViewer to a model list [message #335874] Thu, 07 May 2009 16:00 Go to next message
Eclipse UserFriend
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 #335878 is a reply to message #335874] Thu, 07 May 2009 17:29 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
Markus,

You can validate the WritableList directly without creating a binding.
Use a ValidationStatusProvider such as MultiValidator.

final WritableList items = ... // your WritableList
MultiValidator validator = new MultiValidator() {
protected IStatus validate() {
if (items.isEmpty()) {
return ValidationStatus.error("Missing required item(s)");
}
}
};
bindingContext.addValidationStatusProvider(validator); // important!

Hope this helps,

Matthew

Markus Wolf wrote:
> 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
Re: [DataBinding] Databinding a ListViewer to a model list [message #335879 is a reply to message #335878] Thu, 07 May 2009 17:30 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
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!
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 messageGo to next message
Eclipse UserFriend
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 #335893 is a reply to message #335882] Thu, 07 May 2009 22:19 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
Markus Wolf wrote:
> but how does this validator recognizes the list changes? When does it
> evaluate?

MultiValidator, like ComputedValue, ComputedList, etc uses
ObservableTracker.runAndMonitor to determine which observables you
access inside the validate method. The validator then tracks these
observables for changes and revalidates whenever one or more of these
dependencies change.

> After adding your code snippet I think that the validator is not
> triggered.

If you are using WizardPageSupport or TitleAreaDialogSupport you won't
see the validation status until after you make a change to a data-bound
field on your form. This is to comply with the Eclipse UI guidelines
that require that forms not show an error message when they first pop up.

> 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.

Matthew
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 messageGo to next message
Eclipse UserFriend
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 #335912 is a reply to message #335899] Fri, 08 May 2009 16:10 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
Markus Wolf wrote:
> 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.

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.

Matthew
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 messageGo to next message
Eclipse UserFriend
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 #335921 is a reply to message #335914] Fri, 08 May 2009 19:11 Go to previous messageGo to next message
Matthew Hall is currently offline Matthew HallFriend
Messages: 368
Registered: July 2009
Senior Member
Markus Wolf wrote:
>>> 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?

Matthew
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 messageGo to next message
Eclipse UserFriend
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
Eclipse UserFriend
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
Previous Topic:workspace settings
Next Topic:Can builder change delta?
Goto Forum:
  


Current Time: Fri Mar 29 15:19:53 GMT 2024

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

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

Back to the top