From: Artur Date: Tue, 24 Jan 2017 13:41:15 +0000 (+0200) Subject: Support using multiple @JavaScript and @StyleSheet annotations on a class (#8293) X-Git-Tag: 8.0.0.beta2~46 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c916782da19362dc210402bbe922873d6386b05d;p=vaadin-framework.git Support using multiple @JavaScript and @StyleSheet annotations on a class (#8293) --- diff --git a/server/src/main/java/com/vaadin/annotations/InternalContainerAnnotationForJS.java b/server/src/main/java/com/vaadin/annotations/InternalContainerAnnotationForJS.java new file mode 100644 index 0000000000..6b4152dfa0 --- /dev/null +++ b/server/src/main/java/com/vaadin/annotations/InternalContainerAnnotationForJS.java @@ -0,0 +1,45 @@ +/* + * Copyright 2000-2016 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.annotations; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation enabling using multiple {@link JavaScript @JavaScript} + * annotations. + *

+ * NOT meant to be used, for multiple JavaScript dependencies, + * {@link JavaScript @JavaScript} should be used instead. + * + * @author Vaadin Ltd + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Documented +public @interface InternalContainerAnnotationForJS { + + /** + * Not to be used, instead multiple {@link JavaScript @JavaScript} + * annotations should be used. + * + * @return an array of the JavaScript annotations + */ + JavaScript[] value(); +} diff --git a/server/src/main/java/com/vaadin/annotations/InternalContainerAnnotationForSS.java b/server/src/main/java/com/vaadin/annotations/InternalContainerAnnotationForSS.java new file mode 100644 index 0000000000..2c3d8b19c0 --- /dev/null +++ b/server/src/main/java/com/vaadin/annotations/InternalContainerAnnotationForSS.java @@ -0,0 +1,45 @@ +/* + * Copyright 2000-2016 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.annotations; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Annotation enabling using multiple {@link StyleSheet @StyleSheet} + * annotations. + *

+ * NOT meant to be used, for multiple style sheet dependencies, + * {@link StyleSheet @StyleSheet} should be used instead. + * + * @author Vaadin Ltd + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Documented +public @interface InternalContainerAnnotationForSS { + + /** + * Not to be used, instead multiple {@link StyleSheet @StyleSheet} + * annotations should be used. + * + * @return an array of the style sheet annotations + */ + StyleSheet[] value(); +} diff --git a/server/src/main/java/com/vaadin/annotations/JavaScript.java b/server/src/main/java/com/vaadin/annotations/JavaScript.java index 60950603fe..6ea8904c79 100644 --- a/server/src/main/java/com/vaadin/annotations/JavaScript.java +++ b/server/src/main/java/com/vaadin/annotations/JavaScript.java @@ -16,7 +16,9 @@ package com.vaadin.annotations; +import java.lang.annotation.Documented; import java.lang.annotation.ElementType; +import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @@ -49,17 +51,22 @@ import com.vaadin.server.ClientConnector; * http://host.com/file1.js as is and file2.js from /com/example/file2.js on the * server's classpath using the ClassLoader that was used to load * com.example.MyConnector. + *

+ * For adding multiple JavaScript files for a single component, you can use this + * annotation multiple times. * * @author Vaadin Ltd * @since 7.0.0 */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) +@Documented +@Repeatable(InternalContainerAnnotationForJS.class) public @interface JavaScript { /** * JavaScript files to load before initializing the client-side connector. * - * @return an array of JavaScript file urls + * @return an array of JavaScript file URLs */ public String[] value(); } diff --git a/server/src/main/java/com/vaadin/annotations/StyleSheet.java b/server/src/main/java/com/vaadin/annotations/StyleSheet.java index 59e61dd209..85d19f70cd 100644 --- a/server/src/main/java/com/vaadin/annotations/StyleSheet.java +++ b/server/src/main/java/com/vaadin/annotations/StyleSheet.java @@ -16,7 +16,9 @@ package com.vaadin.annotations; +import java.lang.annotation.Documented; import java.lang.annotation.ElementType; +import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @@ -54,12 +56,17 @@ import com.vaadin.server.ClientConnector; * http://host.com/file1.css as is and file2.css from /com/example/file2.css on * the server's classpath using the ClassLoader that was used to load * com.example.MyConnector. + *

+ * For adding multiple style sheets for a single component, you can use this + * annotation multiple times. * * @author Vaadin Ltd * @since 7.0.0 */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) +@Documented +@Repeatable(InternalContainerAnnotationForSS.class) public @interface StyleSheet { /** * Style sheets to load before initializing the client-side connector. diff --git a/server/src/main/java/com/vaadin/server/BootstrapHandler.java b/server/src/main/java/com/vaadin/server/BootstrapHandler.java index 895598ae6f..be0e9e4593 100644 --- a/server/src/main/java/com/vaadin/server/BootstrapHandler.java +++ b/server/src/main/java/com/vaadin/server/BootstrapHandler.java @@ -437,23 +437,29 @@ public abstract class BootstrapHandler extends SynchronizedRequestHandler { .attr("href", themeUri + "/favicon.ico"); } - JavaScript javaScript = uiClass.getAnnotation(JavaScript.class); - if (javaScript != null) { - String[] resources = javaScript.value(); - for (String resource : resources) { - String url = registerDependency(context, uiClass, resource); - head.appendElement("script").attr("type", "text/javascript") - .attr("src", url); + JavaScript[] javaScripts = uiClass + .getAnnotationsByType(JavaScript.class); + if (javaScripts != null) { + for (JavaScript javaScript : javaScripts) { + String[] resources = javaScript.value(); + for (String resource : resources) { + String url = registerDependency(context, uiClass, resource); + head.appendElement("script").attr("type", "text/javascript") + .attr("src", url); + } } } - StyleSheet styleSheet = uiClass.getAnnotation(StyleSheet.class); - if (styleSheet != null) { - String[] resources = styleSheet.value(); - for (String resource : resources) { - String url = registerDependency(context, uiClass, resource); - head.appendElement("link").attr("rel", "stylesheet") - .attr("type", "text/css").attr("href", url); + StyleSheet[] styleSheets = uiClass + .getAnnotationsByType(StyleSheet.class); + if (styleSheets != null) { + for (StyleSheet styleSheet : styleSheets) { + String[] resources = styleSheet.value(); + for (String resource : resources) { + String url = registerDependency(context, uiClass, resource); + head.appendElement("link").attr("rel", "stylesheet") + .attr("type", "text/css").attr("href", url); + } } } diff --git a/server/src/main/java/com/vaadin/server/communication/UidlWriter.java b/server/src/main/java/com/vaadin/server/communication/UidlWriter.java index 7132433963..f8f6c2fc5b 100644 --- a/server/src/main/java/com/vaadin/server/communication/UidlWriter.java +++ b/server/src/main/java/com/vaadin/server/communication/UidlWriter.java @@ -288,21 +288,25 @@ public class UidlWriter implements Serializable { List styleDependencies = new ArrayList<>(); for (Class class1 : newConnectorTypes) { - JavaScript jsAnnotation = class1 - .getAnnotation(JavaScript.class); - if (jsAnnotation != null) { - for (String uri : jsAnnotation.value()) { - scriptDependencies - .add(manager.registerDependency(uri, class1)); + JavaScript[] jsAnnotations = class1 + .getAnnotationsByType(JavaScript.class); + if (jsAnnotations != null) { + for (JavaScript jsAnnotation : jsAnnotations) { + for (String uri : jsAnnotation.value()) { + scriptDependencies.add( + manager.registerDependency(uri, class1)); + } } } - StyleSheet styleAnnotation = class1 - .getAnnotation(StyleSheet.class); - if (styleAnnotation != null) { - for (String uri : styleAnnotation.value()) { - styleDependencies - .add(manager.registerDependency(uri, class1)); + StyleSheet[] styleAnnotations = class1 + .getAnnotationsByType(StyleSheet.class); + if (styleAnnotations != null) { + for (StyleSheet styleAnnotation : styleAnnotations) { + for (String uri : styleAnnotation.value()) { + styleDependencies.add( + manager.registerDependency(uri, class1)); + } } } } diff --git a/uitest/src/main/java/com/vaadin/tests/components/javascriptcomponent/JavaScriptPreloading.java b/uitest/src/main/java/com/vaadin/tests/components/javascriptcomponent/JavaScriptPreloading.java index 1e69c410e3..3377fc4bfa 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/javascriptcomponent/JavaScriptPreloading.java +++ b/uitest/src/main/java/com/vaadin/tests/components/javascriptcomponent/JavaScriptPreloading.java @@ -28,7 +28,8 @@ public class JavaScriptPreloading extends AbstractReindeerTestUI { public String xhtml; } - @JavaScript({ "js_label.js", "wholly_different.js" }) + @JavaScript("js_label.js") + @JavaScript("wholly_different.js") public class JsLabel extends AbstractJavaScriptComponent { public JsLabel(final String xhtml) { diff --git a/uitest/src/main/java/com/vaadin/tests/components/ui/UiDependenciesInHtml.java b/uitest/src/main/java/com/vaadin/tests/components/ui/UiDependenciesInHtml.java index 53eff1b733..fd76f2f957 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/ui/UiDependenciesInHtml.java +++ b/uitest/src/main/java/com/vaadin/tests/components/ui/UiDependenciesInHtml.java @@ -23,7 +23,8 @@ import com.vaadin.tests.components.AbstractReindeerTestUI; import com.vaadin.ui.Label; @JavaScript("uiDependency.js") -@StyleSheet("theme://uiDependency.css") +@StyleSheet({ "theme://uiDependency1.css", "theme://uiDependency2.css" }) +@StyleSheet("theme://uiDependency3.css") @Theme("tests-valo") public class UiDependenciesInHtml extends AbstractReindeerTestUI { diff --git a/uitest/src/main/resources/com/vaadin/tests/components/ui/uiDependency.js b/uitest/src/main/resources/com/vaadin/tests/components/ui/uiDependency.js index 4a5775c57f..053a03f41d 100644 --- a/uitest/src/main/resources/com/vaadin/tests/components/ui/uiDependency.js +++ b/uitest/src/main/resources/com/vaadin/tests/components/ui/uiDependency.js @@ -1,24 +1,34 @@ (function() { var loadedBeforeVaadin = (window.vaadin === undefined); - + window.reportUiDependencyStatus = function() { - var styleIndex = 1000; + var style1Index = 1000; + var style2Index = 1000; + var style3Index = 1000; var themeIndex = -1; - + var stylesheets = document.querySelectorAll("link[rel=stylesheet]"); - for(var i = 0; i < stylesheets.length; i++) { + for (var i = 0; i < stylesheets.length; i++) { var stylesheet = stylesheets[i]; - var href = stylesheet.getAttribute("href"); - if (href.indexOf("uiDependency.css") > -1) { - styleIndex = i; + var href = stylesheet.getAttribute("href"); + if (href.indexOf("uiDependency1.css") > -1) { + style1Index = i; + } else if (href.indexOf("uiDependency2.css") > -1) { + style2Index = i; + } else if (href.indexOf("uiDependency3.css") > -1) { + style3Index = i; } else if (href.indexOf("styles.css" > -1)) { themeIndex = i; } } - - var status = "Script loaded before vaadinBootstrap.js: " + loadedBeforeVaadin; - status += "
Style tag before vaadin theme: " + (styleIndex < themeIndex); - + + var status = "Script loaded before vaadinBootstrap.js: " + + loadedBeforeVaadin; + status += "
Style tag before vaadin theme: " + + (style1Index < themeIndex); + status += "
Style tags in correct order: " + + (style1Index < style2Index && style2Index < style3Index); + document.getElementById("statusBox").innerHTML = status; } })(); \ No newline at end of file diff --git a/uitest/src/main/themes/VAADIN/themes/tests-valo/uiDependency.css b/uitest/src/main/themes/VAADIN/themes/tests-valo/uiDependency.css deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/uitest/src/main/themes/VAADIN/themes/tests-valo/uiDependency1.css b/uitest/src/main/themes/VAADIN/themes/tests-valo/uiDependency1.css new file mode 100644 index 0000000000..e69de29bb2 diff --git a/uitest/src/main/themes/VAADIN/themes/tests-valo/uiDependency2.css b/uitest/src/main/themes/VAADIN/themes/tests-valo/uiDependency2.css new file mode 100644 index 0000000000..e69de29bb2 diff --git a/uitest/src/main/themes/VAADIN/themes/tests-valo/uiDependency3.css b/uitest/src/main/themes/VAADIN/themes/tests-valo/uiDependency3.css new file mode 100644 index 0000000000..e69de29bb2 diff --git a/uitest/src/test/java/com/vaadin/tests/components/ui/UiDependenciesInHtmlTest.java b/uitest/src/test/java/com/vaadin/tests/components/ui/UiDependenciesInHtmlTest.java index 445d0ffe06..f74c5fa396 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/ui/UiDependenciesInHtmlTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/ui/UiDependenciesInHtmlTest.java @@ -30,7 +30,7 @@ public class UiDependenciesInHtmlTest extends SingleBrowserTest { String statusText = findElement(By.id("statusBox")).getText(); Assert.assertEquals( - "Script loaded before vaadinBootstrap.js: true\nStyle tag before vaadin theme: true", + "Script loaded before vaadinBootstrap.js: true\nStyle tag before vaadin theme: true\nStyle tags in correct order: true", statusText); }