diff options
author | John Ahlroos <john@vaadin.com> | 2013-03-27 09:57:40 +0200 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2013-04-03 07:55:39 +0000 |
commit | bde44c29bdec4647dbec94b1ed2482baaf00cddf (patch) | |
tree | a8c2e855bbda0ad1180bf2ba72fe2cae28b7d83b /server/src | |
parent | d7bfb3a99cf2dee5a7622645357bfd606585b283 (diff) | |
download | vaadin-framework-bde44c29bdec4647dbec94b1ed2482baaf00cddf.tar.gz vaadin-framework-bde44c29bdec4647dbec94b1ed2482baaf00cddf.zip |
Implemented changes to CSS injection based on API review #5500
Change-Id: I2bed5f5a5c3cfc6b97e94cbd218bb06f446c7325
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/com/vaadin/server/Page.java | 104 |
1 files changed, 47 insertions, 57 deletions
diff --git a/server/src/com/vaadin/server/Page.java b/server/src/com/vaadin/server/Page.java index e84271e883..a7e0f7dcb3 100644 --- a/server/src/com/vaadin/server/Page.java +++ b/server/src/com/vaadin/server/Page.java @@ -21,9 +21,11 @@ import java.lang.reflect.Method; import java.net.URI; import java.net.URISyntaxException; import java.util.EventObject; +import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.Map; import com.vaadin.event.EventRouter; import com.vaadin.shared.ui.BorderStyle; @@ -309,25 +311,22 @@ public class Page implements Serializable { * * @since 7.1 */ - public static class StyleSheet implements Serializable { + public static class Styles implements Serializable { - /* - * Points to last injected string injection - */ - private int injectedStringPointer; + private final Map<Integer, String> stringInjections = new HashMap<Integer, String>(); - private final List<String> stringInjections = new LinkedList<String>(); + private final Map<Integer, Resource> resourceInjections = new HashMap<Integer, Resource>(); - /* - * Points to last injected resource injection - */ - private int injectedResourcesPointer; + // The combined injection counter between both string and resource + // injections. Used as the key for the injection maps + private int injectionCounter = 0; - private final List<Resource> resourceInjections = new LinkedList<Resource>(); + // Points to the next injection that has not yet been made into the Page + private int nextInjectionPosition = 0; private final UI ui; - private StyleSheet(UI ui) { + private Styles(UI ui) { this.ui = ui; } @@ -337,24 +336,13 @@ public class Page implements Serializable { * @param css * The CSS to inject */ - public void inject(String css) { + public void add(String css) { if (css == null) { throw new IllegalArgumentException( "Cannot inject null CSS string"); } - /* - * Check if last injection was the same, in that case ignore it. - */ - if (!stringInjections.isEmpty() && injectedStringPointer > 0) { - String lastInjection = stringInjections - .get(injectedStringPointer - 1); - if (lastInjection.equals(css.trim())) { - return; - } - } - - stringInjections.add(css.trim()); + stringInjections.put(injectionCounter++, css); ui.markAsDirty(); } @@ -364,13 +352,13 @@ public class Page implements Serializable { * @param resource * The resource to inject. */ - public void inject(Resource resource) { + public void add(Resource resource) { if (resource == null) { throw new IllegalArgumentException( "Cannot inject null resource"); } - resourceInjections.add(resource); + resourceInjections.put(injectionCounter++, resource); ui.markAsDirty(); } @@ -378,37 +366,38 @@ public class Page implements Serializable { // If full repaint repaint all injections if (target.isFullRepaint()) { - injectedStringPointer = 0; - injectedResourcesPointer = 0; + nextInjectionPosition = 0; } - target.startTag("css-injections"); + if (injectionCounter > nextInjectionPosition) { - // Paint pending string injections - List<String> injections = stringInjections.subList( - injectedStringPointer, stringInjections.size()); + target.startTag("css-injections"); - for (String css : injections) { - target.startTag("css-string"); - target.addText(css); - target.endTag("css-string"); - } + while (injectionCounter > nextInjectionPosition) { - injectedStringPointer = stringInjections.size(); + String stringInjection = stringInjections + .get(nextInjectionPosition); + if (stringInjection != null) { + target.startTag("css-string"); + target.addAttribute("id", nextInjectionPosition); + target.addText(stringInjection); + target.endTag("css-string"); + } - // Paint pending resource injections - List<Resource> resInjections = resourceInjections.subList( - injectedResourcesPointer, resourceInjections.size()); + Resource resourceInjection = resourceInjections + .get(nextInjectionPosition); + if (resourceInjection != null) { + target.startTag("css-resource"); + target.addAttribute("id", nextInjectionPosition); + target.addAttribute("url", resourceInjection); + target.endTag("css-resource"); + } - for (Resource res : resInjections) { - target.startTag("css-resource"); - target.addAttribute("url", res); - target.endTag("css-resource"); - } - - target.endTag("css-injections"); + nextInjectionPosition++; + } - injectedResourcesPointer = resourceInjections.size(); + target.endTag("css-injections"); + } } } @@ -421,7 +410,7 @@ public class Page implements Serializable { private JavaScript javaScript; - private StyleSheet styleSheet; + private Styles styles; /** * The current browser location. @@ -696,11 +685,12 @@ public class Page implements Serializable { * * @since 7.1 */ - public StyleSheet getStyleSheet() { - if (styleSheet == null) { - styleSheet = new StyleSheet(uI); + public Styles getStyles() { + + if (styles == null) { + styles = new Styles(uI); } - return styleSheet; + return styles; } public void paintContent(PaintTarget target) throws PaintException { @@ -760,8 +750,8 @@ public class Page implements Serializable { location.toString()); } - if (styleSheet != null) { - styleSheet.paint(target); + if (styles != null) { + styles.paint(target); } } |