From: Anna Koskinen
* The supplied {@code windowName} is used as the target name in a
* window.open call in the client. This means that special values such as
@@ -717,7 +729,7 @@ public class Page implements Serializable {
* browser's popup-blocker because the new browser window is opened when
* processing a response from the server. To avoid this, you should instead
* use {@link Link} for opening the window because browsers are more
- * forgiving then the window is opened directly from a client-side click
+ * forgiving when the window is opened directly from a client-side click
* event.
*
+ * The supplied {@code windowName} is used as the target name in a
+ * window.open call in the client. This means that special values such as
+ * "_blank", "_self", "_top", "_parent" have special meaning. An empty or
+ *
+ * "", null and "_self" as {@code windowName} all causes the URL to be
+ * opened in the current window, replacing any old contents. For
+ * downloadable content you should avoid "_self" as "_self" causes the
+ * client to skip rendering of any other changes as it considers them
+ * irrelevant (the page will be replaced by the response from the URL). This
+ * can speed up the opening of a URL, but it might also put the client side
+ * into an inconsistent state if the window content is not completely
+ * replaced e.g., if the URL is downloaded instead of displayed in the
+ * browser.
+ *
+ * "_blank" as {@code windowName} causes the URL to always be opened in a
+ * new window or tab (depends on the browser and browser settings).
+ *
+ * "_top" and "_parent" as {@code windowName} works as specified by the HTML
+ * standard.
+ *
+ * Any other {@code windowName} will open the URL in a window with that
+ * name, either by opening a new window/tab in the browser or by replacing
+ * the contents of an existing window with that name.
+ *
+ * Please note that opening a popup window in this way may be blocked by the
+ * browser's popup-blocker because the new browser window is opened when
+ * processing a response from the server. To avoid this, you should instead
+ * use {@link Link} for opening the window because browsers are more
+ * forgiving when the window is opened directly from a client-side click
+ * event.
+ * null
window name is also a special case.
+ *
+ * The supplied {@code windowName} is used as the target name in a
+ * window.open call in the client. This means that special values such as
+ * "_blank", "_self", "_top", "_parent" have special meaning. An empty or
+ * null
window name is also a special case.
+ *
+ * "", null and "_self" as {@code windowName} all causes the resource to be + * opened in the current window, replacing any old contents. For + * downloadable content you should avoid "_self" as "_self" causes the + * client to skip rendering of any other changes as it considers them + * irrelevant (the page will be replaced by the resource). This can speed up + * the opening of a resource, but it might also put the client side into an + * inconsistent state if the window content is not completely replaced e.g., + * if the resource is downloaded instead of displayed in the browser. + *
+ *+ * "_blank" as {@code windowName} causes the resource to always be opened in + * a new window or tab (depends on the browser and browser settings). + *
+ *+ * "_top" and "_parent" as {@code windowName} works as specified by the HTML + * standard. + *
+ *+ * Any other {@code windowName} will open the resource in a window with that + * name, either by opening a new window/tab in the browser or by replacing + * the contents of an existing window with that name. + *
+ *+ * If {@code windowName} is set to open the resource in a new window or tab + * and {@code tryToOpenAsPopup} is true, this method attempts to force the + * browser to open a new window instead of a tab. NOTE: This is a + * best-effort attempt and may not work reliably with all browsers and + * different pop-up preferences. With most browsers using default settings, + * {@code tryToOpenAsPopup} works properly. + *
+ *+ * As of Vaadin 7.0.0, the functionality for opening a Resource in a Page + * has been replaced with similar methods based on a String URL. This is + * because the usage of Resource is problematic with memory management and + * with security features in some browsers. Is is recommended to instead use + * {@link Link} for starting downloads. + *
+ * + * @param resource + * the resource. + * @param windowName + * the name of the window. + * @param tryToOpenAsPopup + * Whether to try to force the resource to be opened in a new + * window + * */ + public void open(Resource resource, String windowName, + boolean tryToOpenAsPopup) { + getPage().open(resource, windowName, tryToOpenAsPopup); } /** diff --git a/uitest/src/com/vaadin/tests/components/window/LegacyWindowOpenTest.java b/uitest/src/com/vaadin/tests/components/window/LegacyWindowOpenTest.java new file mode 100644 index 0000000000..175c3f6d8a --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/window/LegacyWindowOpenTest.java @@ -0,0 +1,74 @@ +package com.vaadin.tests.components.window; + +import com.vaadin.server.ExternalResource; +import com.vaadin.tests.TestForWindowOpen; +import com.vaadin.tests.components.TestBase; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.LegacyWindow; + +public class LegacyWindowOpenTest extends TestBase { + + final ExternalResource r = new ExternalResource("http://www.google.com"); + + @Override + protected void setup() { + final LegacyWindow win = getMainWindow(); + + addComponent(new TestForWindowOpen()); + + addComponent(new Button("Window.open _blank always as popup", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + win.open(r, "_blank", true); + } + })); + + addComponent(new Button("Window.open _blank NOT always as popup", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + win.open(r, "_blank", false); + } + })); + + addComponent(new Button("Window.open _new always as popup", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + win.open(r, "_new", true); + } + })); + + addComponent(new Button("Window.open _new NOT always as popup", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + win.open(r, "_new", false); + } + })); + addComponent(new Button( + "Window execute Javascript window.open(www.google.com, _blank)", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + win.executeJavaScript("window.open(\"http://www.google.com\", \"_blank\");"); + } + })); + addComponent(new Button( + "Window execute Javascript window.open(www.google.com, _blank, resizable=yes,menubar=yes,toolbar=yes,directories=yes,location=yes,scrollbars=yes,status=yes)", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + win.executeJavaScript("window.open(\"http://www.google.com\", \"_blank\", \"resizable=yes,menubar=yes,toolbar=yes,directories=yes,location=yes,scrollbars=yes,status=yes\");"); + } + })); + + } + + @Override + protected String getDescription() { + return "Windows never opened to a new tab"; + } + + @Override + protected Integer getTicketNumber() { + return 7842; + } + +} diff --git a/uitest/src/com/vaadin/tests/components/window/PageOpenTest.java b/uitest/src/com/vaadin/tests/components/window/PageOpenTest.java new file mode 100644 index 0000000000..2dbc24cb66 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/window/PageOpenTest.java @@ -0,0 +1,81 @@ +package com.vaadin.tests.components.window; + +import com.vaadin.server.Page; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.TestForWindowOpen; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.JavaScript; + +public class PageOpenTest extends AbstractTestUI { + + final String url = "http://www.google.com"; + + @Override + protected void setup(VaadinRequest request) { + final Page page = getPage(); + + addComponent(new TestForWindowOpen()); + + addComponent(new Button("Page.open _blank always as popup", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + page.open(url, "_blank", true); + } + })); + + addComponent(new Button("Page.open _blank NOT always as popup", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + page.open(url, "_blank", false); + } + })); + + addComponent(new Button("Page.open _new always as popup", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + page.open(url, "_new", true); + } + })); + + addComponent(new Button("Page.open _new NOT always as popup", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + page.open(url, "_new", false); + } + })); + addComponent(new Button( + "Execute Javascript window.open(www.google.com, _blank)", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + JavaScript + .getCurrent() + .execute( + "window.open(\"http://www.google.com\", \"_blank\");"); + } + })); + addComponent(new Button( + "Execute Javascript window.open(www.google.com, _blank, resizable=yes,menubar=yes,toolbar=yes,directories=yes,location=yes,scrollbars=yes,status=yes)", + new Button.ClickListener() { + public void buttonClick(ClickEvent event) { + JavaScript + .getCurrent() + .execute( + "window.open(\"http://www.google.com\", \"_blank\", \"resizable=yes,menubar=yes,toolbar=yes,directories=yes,location=yes,scrollbars=yes,status=yes\");"); + } + })); + + } + + @Override + protected String getTestDescription() { + return "Windows never opened to a new tab"; + } + + @Override + protected Integer getTicketNumber() { + return 7842; + } + +}