Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » [DataBinding] Model to Viewer Databinding(Real-time automatically triggered viewer updates)
[DataBinding] Model to Viewer Databinding [message #1622342] Wed, 18 February 2015 12:27 Go to next message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 112
Registered: January 2015
Senior Member

Dear experts,

I am willing to bind a JFace TableViewer to a collection of objects, nothing that is not already covered by N tutorials and M snippets in Eclipse-land.

I successfully run one proposed snippet, where essentially a list of Person objects is displayed through a viewer.

The snippet creates the sample collection of objects, then creates and binds the viewer and persons are listed there: ok.

I then tried to insert people dynamically to verify whether the TableViewer was updating itself automatically -- which I thought was the essence of databinding -- but it is not. Even if I am then manually updating the writable list of model objects.

So.. what is the dev intervention to achieve real-time update of a bound viewer upon model changes (collection+cell)?

Thanks for clarifying,
-Piero

[Updated on: Wed, 18 February 2015 16:45]

Report message to a moderator

Re: [DataBinding] Model to Viewer Databinding [message #1622614 is a reply to message #1622342] Wed, 18 February 2015 16:20 Go to previous messageGo to next message
Brian de Alwis is currently offline Brian de AlwisFriend
Messages: 772
Registered: January 2012
Senior Member

Sounds like you're using one of the standard java.util, guava, or apache common collections classes: none of these classes are observable. You have to use an observable collection for two-way databinding to work.

Brian.


Eclipse Platform committer. Ask me about Eclipse support, training, and consulting.
Re: [DataBinding] Model to Viewer Databinding [message #1622648 is a reply to message #1622614] Wed, 18 February 2015 16:48 Go to previous messageGo to next message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 112
Registered: January 2015
Senior Member

Hi Brian,
thanks for your reply.

Both in the linked snippet and in my plugin I do create an org.eclipse.core.databinding.observable.list.WritableList list and use that as binding input to the ViewerSupport.

Isn't that the procedure?

[Updated on: Wed, 18 February 2015 16:49]

Report message to a moderator

Re: [DataBinding] Model to Viewer Databinding [message #1622806 is a reply to message #1622648] Wed, 18 February 2015 19:13 Go to previous messageGo to next message
Brian de Alwis is currently offline Brian de AlwisFriend
Messages: 772
Registered: January 2012
Senior Member

Hi Piero.

You haven't actually shown how you're adding these new people.

In the linked snippet, the ViewModel uses a LinkedList, and the binding part creates a WritableList around that list:
ViewerSupport.bind(peopleViewer, new WritableList(viewModel
					.getPeople(), Person.class), BeanProperties.value(
					Person.class, "name"));

Although that WritableList will be backed directly by the ViewModel's LinkedList, it isn't notified of changes to that LinkedList. So if you add elements directly to the ViewModel's list, there are no events thrown.

If you add your new Person instances to the WritableList, you should see your people show up and you should see them show up in the ViewModel too, since the WritableList is backed by the ViewModel's list.

Brian.

NB: that the WritableList is directly backed by the LinkedList is only to preserve backwords-compatibility; all other WritableList constructors make copies of the provided collection.


Eclipse Platform committer. Ask me about Eclipse support, training, and consulting.
Re: [DataBinding] Model to Viewer Databinding [message #1623661 is a reply to message #1622806] Thu, 19 February 2015 08:35 Go to previous messageGo to next message
Piero Campalani is currently offline Piero CampalaniFriend
Messages: 112
Registered: January 2015
Senior Member

Brian,
thanks for your comments.

Do you mean that any IObservableList wrapper is making a copy of the observing list?
Is there any particular reason behind this design?

In any case, I now update the observable list directly, and the viewer gets updated (but wait, read further please).
In my application, I guess I am going to change my list in the model package, then trigger an update to the observable list through an event.
Is it a conventional and civilized way to go?

Still referring to the JFace snippet example: I modified it to gradually insert Person objects in the list at 1-second steps.
The problem is the viewer shows me the updated list only at the end of all insertions, not raw-by-raw.

[...]
public void addPeople() {
        int wait = 1000; //[ms]
        addPerson("Steve Northover", wait);
        addPerson("Grant Gayed", wait);
        addPerson("Veronika Irvine", wait);
        addPerson("Mike Wilson", wait);
        addPerson("Christophe Cornu", wait);
        addPerson("Lynne Kues", wait);
        addPerson("Silenio Quarti", wait);
}

private void addPerson(String name, int wait) {
        try {
                View.writablePeopleList.add(new Person(name));
                Thread.sleep(wait);
                System.out.println(people.get(people.size() - 1));
        } catch (InterruptedException e) {
                e.printStackTrace();
        }
}
[...]


Re: [DataBinding] Model to Viewer Databinding [message #1624058 is a reply to message #1623661] Thu, 19 February 2015 14:29 Go to previous message
Brian de Alwis is currently offline Brian de AlwisFriend
Messages: 772
Registered: January 2012
Senior Member

I don't know the history but all of WritableList's constructors but one take a Collection which is cloned. The single exception is a constructor with taking a List where the provided list is used as the underlying list. My guess is that this pattern was deemed to be a bad idea since the java.util Collection classes aren't observable and would lead to confusion like what you're encountering.

Your example is not working as you planned as you're almost certainly running your Thread.sleep on the SWT thread and thus blocking the UI event loop. Use Display.timerExec() instead -- or better yet, since you're using databinding, find your Realm and use its Realm.timerExec() instead.

I wouldn't say building your own event mechanism is more civilized. Remember that WritableList implements java.util.List, and there are equivalents for Set and Map too.

Brian.


Eclipse Platform committer. Ask me about Eclipse support, training, and consulting.
Previous Topic:[JFace] Image of ControlDecoration is visible outside composite when the scrolling a composite
Next Topic:Eclipse warns about deprecated methods
Goto Forum:
  


Current Time: Sun Sep 23 04:40:16 GMT 2018

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

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

Back to the top