From: Artur Signell Date: Fri, 9 Mar 2012 14:12:08 +0000 (+0200) Subject: Moved styles list to shared state, replacing the previous String hack. X-Git-Tag: 7.0.0.alpha2~353 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2bd148914d7c2296727dd5259f480c48c7bc43d5;p=vaadin-framework.git Moved styles list to shared state, replacing the previous String hack. --- diff --git a/src/com/vaadin/terminal/gwt/client/ComponentState.java b/src/com/vaadin/terminal/gwt/client/ComponentState.java index 9a72f7cd80..371b8dc358 100644 --- a/src/com/vaadin/terminal/gwt/client/ComponentState.java +++ b/src/com/vaadin/terminal/gwt/client/ComponentState.java @@ -4,6 +4,8 @@ package com.vaadin.terminal.gwt.client; +import java.util.List; + import com.vaadin.terminal.gwt.client.communication.SharedState; import com.vaadin.terminal.gwt.client.communication.URLReference; import com.vaadin.ui.Component; @@ -20,7 +22,6 @@ public class ComponentState extends SharedState { private String width = ""; private boolean readOnly = false; private boolean immediate = false; - private String style = ""; private boolean enabled = true; private String description = ""; // Note: for the caption, there is a difference between null and an empty @@ -28,6 +29,7 @@ public class ComponentState extends SharedState { private String caption = null; private boolean visible = true; private URLReference icon = null; + private List styles = null; /** * Returns the component height as set by the server. @@ -151,36 +153,13 @@ public class ComponentState extends SharedState { this.immediate = immediate; } - /** - * Returns the component styles as set by the server, as a space separated - * string. - * - * @return component styles as defined by the server, not null - */ - public String getStyle() { - if (style == null) { - return ""; - } - return style; - } - - /** - * Sets the component styles as a space separated string. - * - * @param style - * component styles as a space separated string, not null - */ - public void setStyle(String style) { - this.style = style; - } - /** * Returns true if the component has user-defined styles. * * @return true if the component has user-defined styles */ public boolean hasStyles() { - return !"".equals(getStyle()); + return styles != null && !styles.isEmpty(); } /** @@ -298,4 +277,23 @@ public class ComponentState extends SharedState { this.icon = icon; } + /** + * Gets the style names for the component. + * + * @return A List of style names or null if no styles have been set. + */ + public List getStyles() { + return styles; + } + + /** + * Sets the style names for the component. + * + * @param styles + * A list containing style names + */ + public void setStyles(List styles) { + this.styles = styles; + } + } diff --git a/src/com/vaadin/terminal/gwt/client/VCaption.java b/src/com/vaadin/terminal/gwt/client/VCaption.java index 086a2c7de3..09f26fe8b3 100644 --- a/src/com/vaadin/terminal/gwt/client/VCaption.java +++ b/src/com/vaadin/terminal/gwt/client/VCaption.java @@ -108,9 +108,8 @@ public class VCaption extends HTML { String style = CLASSNAME; if (owner.getState().hasStyles()) { - final String[] styles = owner.getState().getStyle().split(" "); - for (int i = 0; i < styles.length; i++) { - style += " " + CLASSNAME + "-" + styles[i]; + for (String customStyle : owner.getState().getStyles()) { + style += " " + CLASSNAME + "-" + customStyle; } } if (!owner.isEnabled()) { diff --git a/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java b/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java index d5a27d7abf..5f0b22261a 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/AbstractComponentConnector.java @@ -362,14 +362,13 @@ public abstract class AbstractComponentConnector extends AbstractConnector // add additional styles as css classes, prefixed with component default // stylename if (state.hasStyles()) { - final String[] styles = state.getStyle().split(" "); - for (int i = 0; i < styles.length; i++) { + for (String style : state.getStyles()) { styleBuf.append(" "); styleBuf.append(primaryStyleName); styleBuf.append("-"); - styleBuf.append(styles[i]); + styleBuf.append(style); styleBuf.append(" "); - styleBuf.append(styles[i]); + styleBuf.append(style); } } diff --git a/src/com/vaadin/terminal/gwt/client/ui/AbstractSplitPanelConnector.java b/src/com/vaadin/terminal/gwt/client/ui/AbstractSplitPanelConnector.java index ce2820b56d..54e9763650 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/AbstractSplitPanelConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/AbstractSplitPanelConnector.java @@ -3,6 +3,8 @@ */ package com.vaadin.terminal.gwt.client.ui; +import java.util.LinkedList; + import com.google.gwt.dom.client.NativeEvent; import com.google.gwt.event.dom.client.DomEvent.Type; import com.google.gwt.event.shared.EventHandler; @@ -79,9 +81,9 @@ public abstract class AbstractSplitPanelConnector extends clickEventHandler.handleEventHandlerRegistration(client); if (getState().hasStyles()) { - getWidget().componentStyleNames = getState().getStyle().split(" "); + getWidget().componentStyleNames = getState().getStyles(); } else { - getWidget().componentStyleNames = new String[0]; + getWidget().componentStyleNames = new LinkedList(); } getWidget().setLocked(uidl.getBooleanAttribute("locked")); diff --git a/src/com/vaadin/terminal/gwt/client/ui/MenuBarConnector.java b/src/com/vaadin/terminal/gwt/client/ui/MenuBarConnector.java index 60d25c5509..da7ad0265b 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/MenuBarConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/MenuBarConnector.java @@ -122,7 +122,7 @@ public class MenuBarConnector extends AbstractComponentConnector implements // any item specific styles are set above in // currentItem.updateFromUIDL(item, client) if (getState().hasStyles()) { - for (String style : getState().getStyle().split(" ")) { + for (String style : getState().getStyles()) { currentMenu.addStyleDependentName(style); } } diff --git a/src/com/vaadin/terminal/gwt/client/ui/PanelConnector.java b/src/com/vaadin/terminal/gwt/client/ui/PanelConnector.java index c54b9fbf60..92c504da97 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/PanelConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/PanelConnector.java @@ -82,11 +82,10 @@ public class PanelConnector extends AbstractComponentContainerConnector String contentClass = contentBaseClass; String decoClass = decoBaseClass; if (getState().hasStyles()) { - final String[] styles = getState().getStyle().split(" "); - for (int i = 0; i < styles.length; i++) { - captionClass += " " + captionBaseClass + "-" + styles[i]; - contentClass += " " + contentBaseClass + "-" + styles[i]; - decoClass += " " + decoBaseClass + "-" + styles[i]; + for (String style : getState().getStyles()) { + captionClass += " " + captionBaseClass + "-" + style; + contentClass += " " + contentBaseClass + "-" + style; + decoClass += " " + decoBaseClass + "-" + style; } } getWidget().captionNode.setClassName(captionClass); diff --git a/src/com/vaadin/terminal/gwt/client/ui/PopupViewConnector.java b/src/com/vaadin/terminal/gwt/client/ui/PopupViewConnector.java index 328aa0aa6d..f1b6c4bbfa 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/PopupViewConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/PopupViewConnector.java @@ -54,16 +54,15 @@ public class PopupViewConnector extends AbstractComponentContainerConnector { getWidget().preparePopup(getWidget().popup); getWidget().popup.updateFromUIDL(popupUIDL, client); if (getState().hasStyles()) { - final String[] styles = getState().getStyle().split(" "); final StringBuffer styleBuf = new StringBuffer(); final String primaryName = getWidget().popup .getStylePrimaryName(); styleBuf.append(primaryName); - for (int i = 0; i < styles.length; i++) { + for (String style : getState().getStyles()) { styleBuf.append(" "); styleBuf.append(primaryName); styleBuf.append("-"); - styleBuf.append(styles[i]); + styleBuf.append(style); } getWidget().popup.setStyleName(styleBuf.toString()); } else { diff --git a/src/com/vaadin/terminal/gwt/client/ui/RootConnector.java b/src/com/vaadin/terminal/gwt/client/ui/RootConnector.java index a42b797af8..37afeafce7 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/RootConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/RootConnector.java @@ -57,10 +57,14 @@ public class RootConnector extends AbstractComponentContainerConnector getWidget().theme = newTheme; } // this also implicitly removes old styles - getWidget() - .setStyleName( - getWidget().getStylePrimaryName() + " " - + getState().getStyle()); + String styles = ""; + styles += getWidget().getStylePrimaryName() + " "; + if (getState().hasStyles()) { + for (String style : getState().getStyles()) { + styles += style + " "; + } + } + getWidget().setStyleName(styles.trim()); clickEventHandler.handleEventHandlerRegistration(client); diff --git a/src/com/vaadin/terminal/gwt/client/ui/SliderConnector.java b/src/com/vaadin/terminal/gwt/client/ui/SliderConnector.java index dcea66ca94..4e299c947f 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/SliderConnector.java +++ b/src/com/vaadin/terminal/gwt/client/ui/SliderConnector.java @@ -30,8 +30,7 @@ public class SliderConnector extends AbstractFieldConnector { getWidget().vertical = uidl.hasAttribute("vertical"); - // TODO should these style names be used? - String style = getState().getStyle(); + // TODO should style names be used? if (getWidget().vertical) { getWidget().addStyleName(VSlider.CLASSNAME + "-vertical"); diff --git a/src/com/vaadin/terminal/gwt/client/ui/VAbstractSplitPanel.java b/src/com/vaadin/terminal/gwt/client/ui/VAbstractSplitPanel.java index da5cef252b..046b2fff91 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VAbstractSplitPanel.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VAbstractSplitPanel.java @@ -4,6 +4,8 @@ package com.vaadin.terminal.gwt.client.ui; +import java.util.List; + import com.google.gwt.dom.client.Node; import com.google.gwt.dom.client.Style; import com.google.gwt.event.dom.client.TouchCancelEvent; @@ -66,7 +68,7 @@ public class VAbstractSplitPanel extends ComplexPanel { private boolean positionReversed = false; - String[] componentStyleNames; + List componentStyleNames; private Element draggingCurtain; @@ -603,13 +605,12 @@ public class VAbstractSplitPanel extends ComplexPanel { splitterStyle = CLASSNAME + splitterSuffix + "-locked"; lockedSuffix = "-locked"; } - for (int i = 0; i < componentStyleNames.length; i++) { - splitterStyle += " " + CLASSNAME + splitterSuffix + "-" - + componentStyleNames[i] + lockedSuffix; - firstStyle += " " + CLASSNAME + firstContainerSuffix + "-" - + componentStyleNames[i]; + for (String style : componentStyleNames) { + splitterStyle += " " + CLASSNAME + splitterSuffix + "-" + style + + lockedSuffix; + firstStyle += " " + CLASSNAME + firstContainerSuffix + "-" + style; secondStyle += " " + CLASSNAME + secondContainerSuffix + "-" - + componentStyleNames[i]; + + style; } DOM.setElementProperty(splitter, "className", splitterStyle); DOM.setElementProperty(firstContainer, "className", firstStyle); diff --git a/src/com/vaadin/terminal/gwt/client/ui/VFilterSelect.java b/src/com/vaadin/terminal/gwt/client/ui/VFilterSelect.java index 303b29fe8f..7fdcaff47c 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VFilterSelect.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VFilterSelect.java @@ -552,10 +552,11 @@ public class VFilterSelect extends Composite implements Field, KeyDownHandler, */ public void updateStyleNames(UIDL uidl, ComponentState componentState) { setStyleName(CLASSNAME + "-suggestpopup"); - final String[] styles = componentState.getStyle().split(" "); - for (int i = 0; i < styles.length; i++) { - if (!"".equals(styles[i])) { - addStyleDependentName(styles[i]); + if (componentState.hasStyles()) { + for (String style : componentState.getStyles()) { + if (!"".equals(style)) { + addStyleDependentName(style); + } } } } diff --git a/src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java b/src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java index 9beef5d2df..10413684dd 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VFormLayout.java @@ -56,8 +56,7 @@ public class VFormLayout extends SimplePanel { private String[] getStylesFromState(ComponentState state, boolean enabled) { List styles = new ArrayList(); if (state.hasStyles()) { - String[] stylesnames = state.getStyle().split(" "); - for (String name : stylesnames) { + for (String name : state.getStyles()) { styles.add(name); } } diff --git a/src/com/vaadin/terminal/gwt/client/ui/VTabsheet.java b/src/com/vaadin/terminal/gwt/client/ui/VTabsheet.java index 7b166ee25f..b30483c2cd 100644 --- a/src/com/vaadin/terminal/gwt/client/ui/VTabsheet.java +++ b/src/com/vaadin/terminal/gwt/client/ui/VTabsheet.java @@ -5,6 +5,7 @@ package com.vaadin.terminal.gwt.client.ui; import java.util.Iterator; +import java.util.List; import com.google.gwt.core.client.Scheduler; import com.google.gwt.dom.client.DivElement; @@ -713,21 +714,20 @@ public class VTabsheet extends VTabsheetBase implements Focusable, // Add proper stylenames for all elements (easier to prevent unwanted // style inheritance) if (state.hasStyles()) { - final String style = state.getStyle(); - if (currentStyle != style) { - currentStyle = style; - final String[] styles = style.split(" "); + final List styles = state.getStyles(); + if (!currentStyle.equals(styles.toString())) { + currentStyle = styles.toString(); final String tabsBaseClass = TABS_CLASSNAME; String tabsClass = tabsBaseClass; final String contentBaseClass = CLASSNAME + "-content"; String contentClass = contentBaseClass; final String decoBaseClass = CLASSNAME + "-deco"; String decoClass = decoBaseClass; - for (int i = 0; i < styles.length; i++) { - tb.addStyleDependentName(styles[i]); - tabsClass += " " + tabsBaseClass + "-" + styles[i]; - contentClass += " " + contentBaseClass + "-" + styles[i]; - decoClass += " " + decoBaseClass + "-" + styles[i]; + for (String style : styles) { + tb.addStyleDependentName(style); + tabsClass += " " + tabsBaseClass + "-" + style; + contentClass += " " + contentBaseClass + "-" + style; + decoClass += " " + decoBaseClass + "-" + style; } DOM.setElementProperty(tabs, "className", tabsClass); DOM.setElementProperty(contentNode, "className", contentClass); diff --git a/src/com/vaadin/ui/AbstractComponent.java b/src/com/vaadin/ui/AbstractComponent.java index 06a15fabf8..7e8ee0a068 100644 --- a/src/com/vaadin/ui/AbstractComponent.java +++ b/src/com/vaadin/ui/AbstractComponent.java @@ -59,11 +59,6 @@ public abstract class AbstractComponent implements Component, MethodEventSource /* Private members */ - /** - * Style names. - */ - private ArrayList styles; - /** * Application specific data object. The component does not use or modify * this. @@ -206,8 +201,9 @@ public abstract class AbstractComponent implements Component, MethodEventSource */ public String getStyleName() { String s = ""; - if (styles != null) { - for (final Iterator it = styles.iterator(); it.hasNext();) { + if (getState().getStyles() != null) { + for (final Iterator it = getState().getStyles().iterator(); it + .hasNext();) { s += it.next(); if (it.hasNext()) { s += " "; @@ -223,13 +219,14 @@ public abstract class AbstractComponent implements Component, MethodEventSource */ public void setStyleName(String style) { if (style == null || "".equals(style)) { - styles = null; + getState().setStyles(null); requestRepaint(); return; } - if (styles == null) { - styles = new ArrayList(); + if (getState().getStyles() == null) { + getState().setStyles(new ArrayList()); } + List styles = getState().getStyles(); styles.clear(); String[] styleParts = style.split(" +"); for (String part : styleParts) { @@ -244,9 +241,10 @@ public abstract class AbstractComponent implements Component, MethodEventSource if (style == null || "".equals(style)) { return; } - if (styles == null) { - styles = new ArrayList(); + if (getState().getStyles() == null) { + getState().setStyles(new ArrayList()); } + List styles = getState().getStyles(); if (!styles.contains(style)) { styles.add(style); requestRepaint(); @@ -254,11 +252,11 @@ public abstract class AbstractComponent implements Component, MethodEventSource } public void removeStyleName(String style) { - if (styles != null) { + if (getState().getStyles() != null) { String[] styleParts = style.split(" +"); for (String part : styleParts) { if (part.length() > 0) { - styles.remove(part); + getState().getStyles().remove(part); } } requestRepaint(); @@ -813,8 +811,6 @@ public abstract class AbstractComponent implements Component, MethodEventSource if (null == sharedState) { sharedState = createState(); } - // basic state: caption, size, enabled, ... - // TODO This logic should be on the client side and the state should // simply be a data object with "width" and "height". if (getHeight() >= 0 @@ -833,12 +829,6 @@ public abstract class AbstractComponent implements Component, MethodEventSource sharedState.setWidth(""); } - // TODO This should be an array in state and the logic for converting - // array -> class name should be on the client side - sharedState.setStyle(getStyleName()); - - // TODO icon also in shared state - how to convert Resource? - return sharedState; }