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: 114
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
Eclipse UserFriend
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.
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: 114
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
Eclipse UserFriend
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.
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: 114
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
Eclipse UserFriend
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.
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: Thu Apr 25 21:53:39 GMT 2024

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

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

Back to the top