How can I force Vaadin v8 to update the screen? - java

I have a small Vaadin v8 application that has several input fields (comboboxes, selectgroups, etc...). The content of most of these is determined by the chosen content of the first ComboBox. However, when I select something in it, all the others stay blank until I click one, at which point they all update. This is not desired behaviour, but I assume it's being caused by the server-side being up to date but not updating the client side view. (Even when adding requestRepaint() in my first Combobox's ValueChangeListener)
There must be some method to force Vaadin to get the data I want it to display even if no other components are clicked?
EDIT
I'm not allowed to post answers to my own question so soon, so I'm putting it here temporarily:
I found that there's a javascript method that synchs client and server.
myComponent.getApplication().getMainWindow().executeJavaScript("javascript:vaadin.forceSync();");
The only problem I have now is that the ValueChangeListener on one of my comboboxes still only fires when I click another combobox (or the same one twice). It's the weirdest thing because the second combobox, when loaded, fires it's event perfectly.

Is the first ComboBox in "immediate" mode?
If not, it probably should be : component.setImmediate(true).
See https://vaadin.com/book/-/page/components.selection.html

I had the same problem, see below how it could be done in version 8.0.5 (from 2017):
#Push
public class WebUi extends UI {
public void fireComponentUpdated() {
getUI().push();
}
}

There is a hack you can use if you have set a datasource for your componets that forces vaadin to re-render them. I use this for updating tables that have dynamic data
yourcomponent.setContainerDataSource(yourcomponent.getContainerDataSource());

Did you requestRepaint on the correct components?
Keep in mind that requestRepaint marks the component as dirty but doesn't mean it will be repainted - a client can only be refreshed when it makes a request to the server.
See this thread https://vaadin.com/forum/-/message_boards/view_message/231271 for more information about your options (it deals with UI refreshes due to background thread processing).

In Vaadin 7 it is enough to put this line in main UI.init(VaadinRequest) method:
UI.getCurrent().setPollInterval( 1000 );
if you want to refresh your UI (in this case) every second. This way you instruct UI to poll server for changes in defined interval.
Beware, excessive server traffic might be a problem if you have lot of users that use your application at the same time.
In Vaadin 6 you will have to play with ProgressIndicator (which could be invisible if you want) and try to do the similar what UI.getCurrent().setPollInterval(int) in Vaadin 7 does.

Related

Vaadin with Spring and Redis loads previous UI state on slow environment

I successfully enabled redis in a spring boot + vaadin application and it runs fine on my computer. The application is on a test run in a slower environment and an error occours multiple times.
WARN c.v.s.communication.ServerRpcHandler [ServerRpcHandler.java : 266] - Unexpected message id from the client. Expected: 248, got: 249
It seems like it happens when the serialization/deserialization of the VaadinSession takes too long. For example I have a page that has multiple checkboxes. I click on the first, then the second and third. After this the upper warn is thrown and a previous state of the page appears. In this case it might be without any cheched checkboxes or with one or two checked checkboxes. In rare cases it works properly.
I can't think of a solution for the problem. One thing I tried is showing a loading indicator immediately (100ms) (the default is after 300ms of loading) but it doesn't solve the problem.
Can I somehow configure when the serialization/deserialization occurs instead of every UI change or make it faster by leaving parts of the VaadinSession out of it? (I need the data on the current page so I can't make the ui components transient.)
We had a discussion about the problem in my workplace and we think that the components are working properly. The problem occurs when a serialization is slower then the next request's deserialization. (Every UI change begins with a deserialization to get the latest state then serializes the modified state.) My solution was creating an aspect that stores the latest VaadinSession that was sent for serialization and compares every deserialized VaadinSession to the stored one. I keep the one with the higher lastProcessedClientToServerId. This solves the issue almost every time.

How to dectect modification on page? Which Listener in java?

Is there any Listener in Java, which can detect, if the content of the page has been changed? "changed" means text in the page has been added/removed...
Process: Author modifys the page and activate it. In publish Instance it must be checked if the page content has been modified/changed
I don't think there is such listener. You're gonna have to reload/access the page or you can hook it up so when the author submits his changes you insert a value to the database that this specific page has been modified. After that you just read the data from the DB using a timer that triggers every now and then and if new line appears you do your action.
This is more of a design question and you should think about what project you're working on and what's the best approach to implement this feature.
Apache sling can handle events. There is nice tutorial here http://sling.apache.org/documentation/tutorials-how-tos/how-to-manage-events-in-sling.html .
Basically create a listener ad check if the event relates to a page node (or its subnode). Then apply whatever logic you want.
Be careful to check whether you are in an author or publish instance ( or turn off the service in author)

window.onunload doesn't complete when closing IE

I have a web app running in ie, and when i close the application with click "X" right corner on IE, it will call(by javascript window.onunload) a struts action and delete a record in a table in database.
But now I found it was incorrect sometimes. the record was not deleted sometimes when i close ie. It should be the case that struts action was not be called when record was not deleted.
More interesting is that when I open Fiddler, record was deleted correctly when close ie everytime, no fail case.
Thanks a lot.
You cannot rely on "onunload" event.
Think about it from the perspective of a web-browser: Should it wait till all open pages process "onunload" event, even if it takes 30 seconds? Probably browser reliably fires the "onunload" event to all open tabs, however it waits just very short time to complete them. Thats why you see inconsistent behavior.
If you really have to do this, take a look at onbeforeunload, but its implementation is inconsistent across browsers: IIRC, opera doesn't implement it.

Can the headers of an AjaxFallbackDefaultDataTable be updated via AJAX?

I'm currently writing a web application. I am using an AjaxFallbackDefaultDataTable to display domain objects. The tables include pagination and so far everything works just fine.
What I want to be able to do:
In my implementation of IColumn<T> I am returning my own header component via Component getHeader(String componentId). This component depends on the page and page size which is currently used by the table (it shows a link to another page which should only care about the current slice of data). Now, I have implemented the void onPageChanged() method of the table so that it updates the columns accordingly.
Problem:
I am using the Wicket AJAX debug window and it shows me that the whole table is rerendered on the server and sent to the client. However, the headers doesn't seem to update correctly so that I'm forever stuck with page 0. Using a debugger I can clearly see that Component getHeader(String componentId) is only called once, when the table is created initially.
Question:
Is there any way I can solve this issue without writing my own implementation of an AJAXified data table? If not, can somebody please point me in the right direction?
HeadersToolbar creates the headers once only.
Either you implement your own toolbar recreating the headers before each render, or rewrite your header component to always render an up-to-date link.

How to avoid editing same value in a form in a Multi-user environment?

My Environment: Multi-user application with server and client. The server knows which field is currently in use and can’t edit from another user.
I have a form with several JComponents like JCheckBox, JTextField, JTextArea and JComboBox. The problem is that I want to control whether the user is allowed to edit the value of the field or not. When the user is not allowed to edit the field, the component is not allowed to go to Edit Mode.
For Example:
User A is editing a value of textfield.
Now User B wants to edit the same value. He clicks on the same textfield and at this point I want to make sure that the textfield ain’t switching in the Edit Mode.
Has someone a good idea where the right place is for this check? Do I have to implement a check in every single component or is there maybe a solution that fit to all JComponents in my form?
Thanks
I believe that you can provide generic solution. Create listener EditabilityListener. When user changes value if component it should send signal to server and all other users should get it, so their instances of JComponent become disabled.
The problem is in concurrent access. For example what would you like to do if user A started typing in text field a second after user B but before the signal arrived to his application?
The server can support a request for component ownership, which returns a boolean used by the client. The server side implementation should maintain a synchronized data structure aware of component ownerships. You can use a ConcurrentHashMap for example (it supports putIfAbsent which performs an atomic operation that can help).
Note that you'll also need clients to report when they release the component (possibly also adding ownership timeout in server).

Categories

Resources