From e4a50934a2cfb0e652b872376decf581bc0ab057 Mon Sep 17 00:00:00 2001 From: Johannes Dahlström Date: Wed, 19 Mar 2014 17:29:45 +0200 Subject: Add reinit method for preserve-on-refresh UIs (#12191) UI.reinit() is now called when an existing, preserved UI is shown after a browser reload of the current page. The default implementation is empty. The browser window size and location are up to date in UI.reinit(); window resize and URI fragment listeners, if any, will be called after returning from UI.reinit(). Change-Id: Ie7aa670aaecf8e0e1510c91325b2a137b41263af --- server/src/com/vaadin/ui/UI.java | 57 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) (limited to 'server/src/com/vaadin/ui/UI.java') diff --git a/server/src/com/vaadin/ui/UI.java b/server/src/com/vaadin/ui/UI.java index b3004e9ad2..2b2e773601 100644 --- a/server/src/com/vaadin/ui/UI.java +++ b/server/src/com/vaadin/ui/UI.java @@ -16,6 +16,7 @@ package com.vaadin.ui; +import java.net.URI; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -27,6 +28,7 @@ import java.util.concurrent.Future; import java.util.logging.Level; import java.util.logging.Logger; +import com.vaadin.annotations.PreserveOnRefresh; import com.vaadin.event.Action; import com.vaadin.event.Action.Handler; import com.vaadin.event.ActionManager; @@ -161,7 +163,7 @@ public abstract class UI extends AbstractSingleComponentContainer implements public void resize(int viewWidth, int viewHeight, int windowWidth, int windowHeight) { // TODO We're not doing anything with the view dimensions - getPage().updateBrowserWindowSize(windowWidth, windowHeight); + getPage().updateBrowserWindowSize(windowWidth, windowHeight, true); } @Override @@ -361,7 +363,7 @@ public abstract class UI extends AbstractSingleComponentContainer implements if (variables.containsKey(UIConstants.LOCATION_VARIABLE)) { String location = (String) variables .get(UIConstants.LOCATION_VARIABLE); - getPage().updateLocation(location); + getPage().updateLocation(location, true); } } @@ -659,6 +661,57 @@ public abstract class UI extends AbstractSingleComponentContainer implements */ protected abstract void init(VaadinRequest request); + /** + * Internal reinitialization method, should not be overridden. + * + * @since 7.2 + * @param request + * the request that caused this UI to be reloaded + */ + public void doReinit(VaadinRequest request) { + // This is a horrible hack. We want to have the most recent location and + // browser window size available in reinit(), but we want to call + // listeners, if any, only after reinit(). So we momentarily assign the + // old values back before setting the new values again to ensure the + // events are properly fired. + + Page page = getPage(); + + URI oldLocation = page.getLocation(); + int oldWidth = page.getBrowserWindowWidth(); + int oldHeight = page.getBrowserWindowHeight(); + + page.init(request); + + reinit(request); + + URI newLocation = page.getLocation(); + int newWidth = page.getBrowserWindowWidth(); + int newHeight = page.getBrowserWindowHeight(); + + page.updateLocation(oldLocation.toString(), false); + page.updateBrowserWindowSize(oldWidth, oldHeight, false); + + page.updateLocation(newLocation.toString(), true); + page.updateBrowserWindowSize(newWidth, newHeight, true); + } + + /** + * Reinitializes this UI after a browser refresh if the UI is set to be + * preserved on refresh, typically using the {@link PreserveOnRefresh} + * annotation. This method is intended to be overridden by subclasses if + * needed; the default implementation is empty. + *

+ * The {@link VaadinRequest} can be used to get information about the + * request that caused this UI to be reloaded. + * + * @since 7.2 + * @param request + * the request that caused this UI to be reloaded + */ + protected void reinit(VaadinRequest request) { + } + /** * Sets the thread local for the current UI. This method is used by the * framework to set the current application whenever a new request is -- cgit v1.2.3