Binding with a CustomComponent

I am creating a custom widget to implement visual controls, AControlWidget by extending CustomComponent similar to section 5.20 in the Vaadin book and I can render my layout OK.

My widget will have icons, images, textFields, Buttons. Some will be editable or have button actions requiring server actions.

For data/interaction, my Vaadin Application will access a separate REST server to get/set physical controls each of which has a set of properties according to control type.

I have a populateControlsContainer() method which pulls in data from the RESTserver and local classes I have made such as FooControl and BarControl each of which has its properties.

At the moment I create instances of FooControl & BarControl, initialise them and use addBean(.) to put them in a sub-class of BeanItemContainer.

Now I am struggling to see how to bind the Buttons (say) in my CustomComponent (AControlWidget) to a specific property of a particular control instance in the data container. Normally, I would have made a key out of Control “name” and Control “type” which I know is unique and put them in a map. I could then access a particular property.

Since the BeanItemContainer behaves more as a set or an array, I can not do that. Doing a search for each use seems inefficient.

I wondering which classes I should be using to do this so as not to duplicate functionality which is already there in Vaadin classes. I suppose I was looking for a method

addBean( item, (String id) )

, but there is not one.

Section 5.20 stops short of discussing binding (because that is dealt with later in chapter 9, which I read, but I didn’t quite see how to finish off 5.20.

Finally, my REST server data changes autonomously with time, so periodically I need to refresh my display. When binding is in place, can I just refresh the data model which in turn would refresh the display? Who has to initiate refresh?

Can anyone suggest directly, or give pointers to how to deal with this kind of thing?

I can answer the last bit. Your display (i.e. each vaadin application instance, which is in fact an http session) needs to be told to update by listening to the data model. You will need to create a Listener pattern (there are classes in Java to do this) so you can notify the relevant sessions. Upon receipt of the event, you may want to hide/show/tweak things to reflect the data model. This has to be done in a synchronized(app) block because the update is not initiated by the browser, and you may be interfering with user interaction that comes from the browser at that very moment.
Once you have updated the interface on the server side, the client (browser) needs to be nudged to take notice. The easiest way to do this is to use the ICEPush add-on.

Thanks. I didn’t get to the refresh bit yet. I am working on the initial display and MVC scheme for now. But, I must get to refresh soon.

Regarding my other question, I have blundered along trying trying different approaches.

My current thoughts are this:

In a MVC scheme, the model should be independent of the view and vice versa.

In my opinion, in using Vaadin Collections this is not really be the case.

What is in a Vaadin datasource as a Collection seems to me to be rather a prototype of a view, as the rows, columns and order of the Collection (generally) will be the order of the view. The collection is the list of items to be in the view and the order is important to the view. It is more sink than source.

What works for me is to set up the model as suits the natural structure of the model. It provides a local connection to remote data in a manner that reflects the remote datasource. The model supports several concurrent views. It has data structures that are optimised for the physical model.

I then construct Vaadin Collections which target specific views which CustomComponent will assist in rendering. To me, this is part of the View, the CustomComponent, but I have put this in the controller.

To figure out how to organise stuff, I think one has to take into account sessions. Each session will create its own CustomComponent, whereas one Collection and model can and perhaps should serve all views (sessions), with appropriate synchronisation. If I put Collection in view where I believe it belongs in true MVC, I would end up creating many copies of it. So M, V and C are a bit of a muddle, but that is not unusual?

In my project, there is just one physical model to which all access must be synchronised. Not much different from a database really.

Does that make sense?

At the moment I have just physical-source-to-view working. I don’t know if control flow in the opposite direction will lead me to change my setup.