1

just a question about Vaadin, I searched a lot, without finding a useful information.

I need to know whether a UI has been refreshed when it has NOT @PreserveOnRefresh annotation set (meaning that overriding UI.refresh() will not work for me).

Basically I have a Map which maps a user session id to the opened UIs in the browser, when a new UI is detected I need to add it to the Collection of UIs for the current session.

If the user requests the same UI (same UI implementation mapped by the Servlet @WebServlet configuration) in the browser I need to either add this UI if and only if it is a UI opened in another window of the browser or in another tab, and do nothing or better remove the old UI and put the new UI if the browser window is refreshed.

I know that if I add a @PreservOnRefresh annotation to the UI this will work (but again I need the UI to be not preservable).

So how can I actually do that? I found this implementation (seems a little bit ugly for me) -> http://dev.vaadin.com/ticket/12191#trac-change-5-1374239207261117

Is there another possible way to do that?

Thanks for the attention!

Jan Galinski
  • 11,768
  • 8
  • 54
  • 77
tonix
  • 6,671
  • 13
  • 75
  • 136

2 Answers2

4

A new UI is created on refresh so could you just add the UI instance to the map in the init method of the UI?

To remove UIs from the map, you can add a DetachListener by calling UI.addDetachListener method. This method will be called when Vaadin detaches the UI from the application.

Henri Kerola
  • 4,947
  • 1
  • 17
  • 19
  • Thank you for you answering! Yes, I can do that, but when the same UI is created how do I know if it was created via a browser refresh or just because the user has opened the same UI in another tab of the browser? I need to differentiate this because, because if I add the UI instance which was refreshed I need to remove the old reference to the same UI which I had before, conversely if the same UI is created but in another tab of the browser, I need to keeep the old reference and add the new created. – tonix Dec 11 '14 at 07:20
  • I can't work with the detach listener because I use async push to the UIs and I have a logout button which when clicked redirects all the UIs currently opened by the user and closes the session, then I have noticed that inside detach() method the WrappedSession session id (which I use in the map) is no longer available (I get a NullPointerException when I try to do this call `String sessId = VaadinSession.getCurrent().getSession().getId();` inside `public void detach()` override) – tonix Dec 11 '14 at 07:28
  • The detach thing is easy to fix so that you cleanup the map on the session expiration and in the detach listener you do removal only if the session still exists. – Henri Kerola Dec 11 '14 at 22:49
  • So I check if WrappedSession is != null and if so I need to remove this UI from the map right? – tonix Dec 12 '14 at 08:30
  • @user3019105 Haven't tested by myself but I think so. – Henri Kerola Dec 12 '14 at 17:13
  • All give it a try, anyway I still need a way to identify the UI when it hasn't @PreserveOnRefresh annotation set and it is refreshed, how can I do it? Do I have to implement some client functionality? – tonix Dec 12 '14 at 19:35
2

Just override refresh() method in your UI class. It works with @PreserveOnRefresh so you don't have to reinitialize your UI instance.

@Override
protected void refresh(VaadinRequest request) {
    super.refresh(request);
    // TODO do your thing here
}
bekce
  • 3,782
  • 29
  • 30