From 45c7d559f791060be0642b9b7c447107c07f2232 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Thu, 1 Nov 2012 16:55:28 +0200 Subject: [PATCH] Update location when initialized UI is opened again (#10044) * Also fix Page.updateLocation to avoid NPE if fragment changes from null to null Change-Id: I03f3883c2653eaf438ccdfc313078fbe8fe898bc --- .../server/AbstractCommunicationManager.java | 21 ++++++++++- server/src/com/vaadin/server/Page.java | 4 +- .../application/RefreshFragmentChange.html | 37 +++++++++++++++++++ .../application/RefreshStatePreserve.java | 16 +++++++- 4 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 uitest/src/com/vaadin/tests/application/RefreshFragmentChange.html diff --git a/server/src/com/vaadin/server/AbstractCommunicationManager.java b/server/src/com/vaadin/server/AbstractCommunicationManager.java index 43a9ced608..c3a6213265 100644 --- a/server/src/com/vaadin/server/AbstractCommunicationManager.java +++ b/server/src/com/vaadin/server/AbstractCommunicationManager.java @@ -61,6 +61,7 @@ import org.json.JSONException; import org.json.JSONObject; import com.vaadin.annotations.JavaScript; +import com.vaadin.annotations.PreserveOnRefresh; import com.vaadin.annotations.StyleSheet; import com.vaadin.server.ComponentSizeValidator.InvalidLayout; import com.vaadin.server.RpcManager.RpcInvocationException; @@ -2467,7 +2468,7 @@ public abstract class AbstractCommunicationManager implements Serializable { UI existingUi = legacyProvider .getExistingUI(classSelectionEvent); if (existingUi != null) { - UI.setCurrent(existingUi); + reinitUI(existingUi, request); return existingUi; } } @@ -2498,6 +2499,7 @@ public abstract class AbstractCommunicationManager implements Serializable { if (retainedUIId != null) { UI retainedUI = session.getUIById(retainedUIId.intValue()); if (uiClass.isInstance(retainedUI)) { + reinitUI(retainedUI, request); return retainedUI; } else { getLogger() @@ -2546,6 +2548,23 @@ public abstract class AbstractCommunicationManager implements Serializable { return ui; } + /** + * Updates a UI that has already been initialized but is now loaded again, + * e.g. because of {@link PreserveOnRefresh}. + * + * @param ui + * @param request + */ + private void reinitUI(UI ui, VaadinRequest request) { + UI.setCurrent(ui); + + // Fire fragment change if the fragment has changed + String location = request.getParameter("loc"); + if (location != null) { + ui.getPage().updateLocation(location); + } + } + /** * Generates the initial UIDL message that can e.g. be included in a html * page to avoid a separate round trip just for getting the UIDL. diff --git a/server/src/com/vaadin/server/Page.java b/server/src/com/vaadin/server/Page.java index 1f4ff040ac..ab8c591b13 100644 --- a/server/src/com/vaadin/server/Page.java +++ b/server/src/com/vaadin/server/Page.java @@ -657,8 +657,8 @@ public class Page implements Serializable { String oldFragment = this.location.getFragment(); this.location = new URI(location); String newFragment = this.location.getFragment(); - if (newFragment == null && oldFragment != null - || !newFragment.equals(oldFragment)) { + if (newFragment == null ? oldFragment != null : !newFragment + .equals(oldFragment)) { fireEvent(new FragmentChangedEvent(this, newFragment)); } } catch (URISyntaxException e) { diff --git a/uitest/src/com/vaadin/tests/application/RefreshFragmentChange.html b/uitest/src/com/vaadin/tests/application/RefreshFragmentChange.html new file mode 100644 index 0000000000..147e67f19d --- /dev/null +++ b/uitest/src/com/vaadin/tests/application/RefreshFragmentChange.html @@ -0,0 +1,37 @@ + + + + + + +New Test + + + + + + + + + + + + + + + + + + + + + + + + + + + +
New Test
open/run/com.vaadin.tests.application.RefreshStatePreserve?restartApplication
assertTextvaadin=runcomvaadintestsapplicationRefreshStatePreserve::PID_SLog_row_01. Initial fragment: null
open/run/com.vaadin.tests.application.RefreshStatePreserve#asdf
assertTextvaadin=runcomvaadintestsapplicationRefreshStatePreserve::PID_SLog_row_02. Fragment changed to asdf
+ + diff --git a/uitest/src/com/vaadin/tests/application/RefreshStatePreserve.java b/uitest/src/com/vaadin/tests/application/RefreshStatePreserve.java index c99e384f81..f4d7f2a4e4 100644 --- a/uitest/src/com/vaadin/tests/application/RefreshStatePreserve.java +++ b/uitest/src/com/vaadin/tests/application/RefreshStatePreserve.java @@ -1,18 +1,32 @@ package com.vaadin.tests.application; import com.vaadin.annotations.PreserveOnRefresh; +import com.vaadin.server.Page.FragmentChangedEvent; +import com.vaadin.server.Page.FragmentChangedListener; import com.vaadin.server.VaadinRequest; import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.tests.util.Log; import com.vaadin.ui.Label; @PreserveOnRefresh public class RefreshStatePreserve extends AbstractTestUI { + private Log log = new Log(5); + @Override protected void setup(VaadinRequest request) { // Internal parameter sent by vaadinBootstrap.js, addComponent(new Label("window.name: " + request.getParameter("wn"))); addComponent(new Label("UI id: " + getUIId())); + addComponent(log); + + log.log("Initial fragment: " + getPage().getFragment()); + getPage().addFragmentChangedListener(new FragmentChangedListener() { + @Override + public void fragmentChanged(FragmentChangedEvent event) { + log.log("Fragment changed to " + event.getFragment()); + } + }); } @Override @@ -24,4 +38,4 @@ public class RefreshStatePreserve extends AbstractTestUI { protected Integer getTicketNumber() { return Integer.valueOf(8068); } -} +} \ No newline at end of file -- 2.39.5