From 9f97638b0f75b882b76eeb5d2b95f87bb5263a73 Mon Sep 17 00:00:00 2001 From: Leif Åstrand Date: Fri, 23 Nov 2012 11:46:14 +0200 Subject: Refactor BrowserPopupOpener API (#10241) * Rename BrowserPopupOpener to BrowserWindowOpener * Rename setPopupName -> setWindowName * Rename BrowserPopupUIProvider -> BrowserWindowUIProvider * Make BrowserWindowUIProvider a private static inner class Change-Id: Id839b274762424bb50fe3590d7cd1187e92baa6c --- .../src/com/vaadin/server/BrowserPopupOpener.java | 168 ----------------- .../com/vaadin/server/BrowserPopupUIProvider.java | 50 ------ .../src/com/vaadin/server/BrowserWindowOpener.java | 199 +++++++++++++++++++++ 3 files changed, 199 insertions(+), 218 deletions(-) delete mode 100644 server/src/com/vaadin/server/BrowserPopupOpener.java delete mode 100644 server/src/com/vaadin/server/BrowserPopupUIProvider.java create mode 100644 server/src/com/vaadin/server/BrowserWindowOpener.java (limited to 'server') diff --git a/server/src/com/vaadin/server/BrowserPopupOpener.java b/server/src/com/vaadin/server/BrowserPopupOpener.java deleted file mode 100644 index c55fa65931..0000000000 --- a/server/src/com/vaadin/server/BrowserPopupOpener.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright 2012 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.server; - -import com.vaadin.shared.ApplicationConstants; -import com.vaadin.shared.ui.BrowserPopupExtensionState; -import com.vaadin.ui.AbstractComponent; -import com.vaadin.ui.UI; - -/** - * Component extension that opens a browser popup window when the extended - * component is clicked. - * - * @author Vaadin Ltd - * @since 7.0.0 - */ -public class BrowserPopupOpener extends AbstractExtension { - - private final BrowserPopupUIProvider uiProvider; - - /** - * Creates a popup opener that will open popups containing the provided UI - * class - * - * @param uiClass - * the UI class that should be opened when the extended component - * is clicked - */ - public BrowserPopupOpener(Class uiClass) { - this(uiClass, generateUIClassUrl(uiClass)); - } - - /** - * Creates a popup opener that will open popups containing the provided UI - * using the provided path - * - * @param uiClass - * the UI class that should be opened when the extended component - * is clicked - * @param path - * the path that the UI should be bound to - */ - public BrowserPopupOpener(Class uiClass, String path) { - // Create a Resource with a translated URL going to the VaadinService - this(new ExternalResource(ApplicationConstants.APP_PROTOCOL_PREFIX - + path), new BrowserPopupUIProvider(uiClass, path)); - } - - /** - * Creates a popup opener that will open popups to the provided URL - * - * @param url - * the URL to open in the popup - */ - public BrowserPopupOpener(String url) { - this(new ExternalResource(url)); - } - - /** - * Creates a popup opener that will open popups to the provided resource - * - * @param resource - * the resource to open in the popup - */ - public BrowserPopupOpener(Resource resource) { - this(resource, null); - } - - private BrowserPopupOpener(Resource resource, - BrowserPopupUIProvider uiProvider) { - this.uiProvider = uiProvider; - setResource("popup", resource); - } - - public void extend(AbstractComponent target) { - super.extend(target); - } - - /** - * Sets the target window name that will be used when opening the popup. If - * a popup has already been opened with the same name, the contents of that - * window will be replaced instead of opening a new window. If the name is - * null or "blank", the popup will always be - * opened in a new window. - * - * @param popupName - * the target name for the popups - */ - public void setPopupName(String popupName) { - getState().target = popupName; - } - - /** - * Gets the popup target name. - * - * @see #setPopupName(String) - * - * @return the popup target string - */ - public String getPopupName() { - return getState().target; - } - - // Avoid breaking url to multiple lines - // @formatter:off - /** - * Sets the features for opening the popup. See e.g. - * {@link https://developer.mozilla.org/en-US/docs/DOM/window.open#Position_and_size_features} - * for a description of the commonly supported features. - * - * @param features a string with popup features, or null to use the default features. - */ - // @formatter:on - public void setFeatures(String features) { - getState().features = features; - } - - /** - * Gets the popup features. - * - * @see #setFeatures(String) - * @return - */ - public String getFeatures() { - return getState().features; - } - - @Override - protected BrowserPopupExtensionState getState() { - return (BrowserPopupExtensionState) super.getState(); - } - - @Override - public void attach() { - super.attach(); - if (uiProvider != null - && !getSession().getUIProviders().contains(uiProvider)) { - getSession().addUIProvider(uiProvider); - } - } - - @Override - public void detach() { - if (uiProvider != null) { - getSession().removeUIProvider(uiProvider); - } - super.detach(); - } - - private static String generateUIClassUrl(Class uiClass) { - return "popup/" + uiClass.getSimpleName(); - } - -} diff --git a/server/src/com/vaadin/server/BrowserPopupUIProvider.java b/server/src/com/vaadin/server/BrowserPopupUIProvider.java deleted file mode 100644 index 6259fb605d..0000000000 --- a/server/src/com/vaadin/server/BrowserPopupUIProvider.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2012 Vaadin Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy of - * the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations under - * the License. - */ - -package com.vaadin.server; - -import com.vaadin.ui.UI; - -public class BrowserPopupUIProvider extends UIProvider { - - private final String path; - private final Class uiClass; - - public BrowserPopupUIProvider(Class uiClass, String path) { - this.path = ensureInitialSlash(path); - this.uiClass = uiClass; - } - - private static String ensureInitialSlash(String path) { - if (path == null) { - return null; - } else if (!path.startsWith("/")) { - return '/' + path; - } else { - return path; - } - } - - @Override - public Class getUIClass(UIClassSelectionEvent event) { - String requestPathInfo = event.getRequest().getPathInfo(); - if (path.equals(requestPathInfo)) { - return uiClass; - } else { - return null; - } - } -} diff --git a/server/src/com/vaadin/server/BrowserWindowOpener.java b/server/src/com/vaadin/server/BrowserWindowOpener.java new file mode 100644 index 0000000000..3ed039887c --- /dev/null +++ b/server/src/com/vaadin/server/BrowserWindowOpener.java @@ -0,0 +1,199 @@ +/* + * Copyright 2012 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.vaadin.server; + +import com.vaadin.shared.ApplicationConstants; +import com.vaadin.shared.ui.BrowserWindowOpenerState; +import com.vaadin.ui.AbstractComponent; +import com.vaadin.ui.UI; + +/** + * Component extension that opens a browser popup window when the extended + * component is clicked. + * + * @author Vaadin Ltd + * @since 7.0.0 + */ +public class BrowserWindowOpener extends AbstractExtension { + + private static class BrowserWindowOpenerUIProvider extends UIProvider { + + private final String path; + private final Class uiClass; + + public BrowserWindowOpenerUIProvider(Class uiClass, String path) { + this.path = ensureInitialSlash(path); + this.uiClass = uiClass; + } + + private static String ensureInitialSlash(String path) { + if (path == null) { + return null; + } else if (!path.startsWith("/")) { + return '/' + path; + } else { + return path; + } + } + + @Override + public Class getUIClass(UIClassSelectionEvent event) { + String requestPathInfo = event.getRequest().getPathInfo(); + if (path.equals(requestPathInfo)) { + return uiClass; + } else { + return null; + } + } + } + + private final BrowserWindowOpenerUIProvider uiProvider; + + /** + * Creates a window opener that will open windows containing the provided UI + * class + * + * @param uiClass + * the UI class that should be opened when the extended component + * is clicked + */ + public BrowserWindowOpener(Class uiClass) { + this(uiClass, generateUIClassUrl(uiClass)); + } + + /** + * Creates a window opener that will open windows containing the provided UI + * using the provided path + * + * @param uiClass + * the UI class that should be opened when the extended component + * is clicked + * @param path + * the path that the UI should be bound to + */ + public BrowserWindowOpener(Class uiClass, String path) { + // Create a Resource with a translated URL going to the VaadinService + this(new ExternalResource(ApplicationConstants.APP_PROTOCOL_PREFIX + + path), new BrowserWindowOpenerUIProvider(uiClass, path)); + } + + /** + * Creates a window opener that will open windows to the provided URL + * + * @param url + * the URL to open in the window + */ + public BrowserWindowOpener(String url) { + this(new ExternalResource(url)); + } + + /** + * Creates a window opener that will open window to the provided resource + * + * @param resource + * the resource to open in the window + */ + public BrowserWindowOpener(Resource resource) { + this(resource, null); + } + + private BrowserWindowOpener(Resource resource, + BrowserWindowOpenerUIProvider uiProvider) { + this.uiProvider = uiProvider; + setResource(BrowserWindowOpenerState.locationResource, resource); + } + + public void extend(AbstractComponent target) { + super.extend(target); + } + + /** + * Sets the target window name that will be used. If a window has already + * been opened with the same name, the contents of that window will be + * replaced instead of opening a new window. If the name is + * null or "_blank", a new window will always be + * opened. + * + * @param windowName + * the target name for the window + */ + public void setWindowName(String windowName) { + getState().target = windowName; + } + + /** + * Gets the target window name. + * + * @see #setWindowName(String) + * + * @return the window target string + */ + public String getWindowName() { + return getState().target; + } + + // Avoid breaking url to multiple lines + // @formatter:off + /** + * Sets the features for opening the window. See e.g. + * {@link https://developer.mozilla.org/en-US/docs/DOM/window.open#Position_and_size_features} + * for a description of the commonly supported features. + * + * @param features a string with window features, or null to use the default features. + */ + // @formatter:on + public void setFeatures(String features) { + getState().features = features; + } + + /** + * Gets the window features. + * + * @see #setFeatures(String) + * @return + */ + public String getFeatures() { + return getState().features; + } + + @Override + protected BrowserWindowOpenerState getState() { + return (BrowserWindowOpenerState) super.getState(); + } + + @Override + public void attach() { + super.attach(); + if (uiProvider != null + && !getSession().getUIProviders().contains(uiProvider)) { + getSession().addUIProvider(uiProvider); + } + } + + @Override + public void detach() { + if (uiProvider != null) { + getSession().removeUIProvider(uiProvider); + } + super.detach(); + } + + private static String generateUIClassUrl(Class uiClass) { + return "popup/" + uiClass.getSimpleName(); + } + +} -- cgit v1.2.3