]> source.dussan.org Git - vaadin-framework.git/commitdiff
#8019 Fixed Sizable UNITS to use an Enum Unit instead of constants
authorJens Jansson <peppe@vaadin.com>
Wed, 21 Dec 2011 16:22:58 +0000 (18:22 +0200)
committerJens Jansson <peppe@vaadin.com>
Wed, 21 Dec 2011 16:22:58 +0000 (18:22 +0200)
15 files changed:
src/com/vaadin/terminal/Sizeable.java
src/com/vaadin/terminal/gwt/server/ComponentSizeValidator.java
src/com/vaadin/ui/AbsoluteLayout.java
src/com/vaadin/ui/AbstractComponent.java
src/com/vaadin/ui/AbstractComponentContainer.java
src/com/vaadin/ui/AbstractSplitPanel.java
src/com/vaadin/ui/CustomField.java
src/com/vaadin/ui/LoginForm.java
tests/server-side/com/vaadin/tests/server/component/absolutelayout/ComponentPosition.java
tests/testbench/com/vaadin/tests/components/splitpanel/AbstractSplitPanelTest.java
tests/testbench/com/vaadin/tests/components/table/TablePageLengthUpdate.java
tests/testbench/com/vaadin/tests/components/window/WindowResizeListener.java
tests/testbench/com/vaadin/tests/layouts/TestAbsoluteLayout.java
tests/testbench/com/vaadin/tests/tickets/Ticket1435.java
tests/testbench/com/vaadin/tests/tickets/Ticket2090.java

index 74fb8eefbb55a72166c7435d97c1188ae0af84fe..055c74f20f239ffb14f5b8026fba6379870b13a8 100644 (file)
@@ -18,71 +18,127 @@ import java.io.Serializable;
 public interface Sizeable extends Serializable {
 
     /**
-     * Unit code representing pixels.
+     * @deprecated from 7.0, use {@link Unit#PIXELS} instead    
      */
-    public static final int UNITS_PIXELS = 0;
+    @Deprecated
+    public static final Unit UNITS_PIXELS = Unit.PIXELS;
 
     /**
-     * Unit code representing points (1/72nd of an inch).
+     * @deprecated from 7.0, use {@link Unit#POINTS} instead    
      */
-    public static final int UNITS_POINTS = 1;
+    @Deprecated
+    public static final Unit UNITS_POINTS = Unit.POINTS;
 
     /**
-     * Unit code representing picas (12 points).
+     * @deprecated from 7.0, use {@link Unit#PICAS} instead    
      */
-    public static final int UNITS_PICAS = 2;
+    @Deprecated
+    public static final Unit UNITS_PICAS = Unit.PICAS;
 
     /**
-     * Unit code representing the font-size of the relevant font.
+     * @deprecated from 7.0, use {@link Unit#EM} instead    
      */
-    public static final int UNITS_EM = 3;
+    @Deprecated
+    public static final Unit UNITS_EM = Unit.EM;
 
     /**
-     * Unit code representing the x-height of the relevant font.
+     * @deprecated from 7.0, use {@link Unit#EX} instead    
      */
-    public static final int UNITS_EX = 4;
+    @Deprecated
+    public static final Unit UNITS_EX = Unit.EX;
 
     /**
-     * Unit code representing millimeters.
+     * @deprecated from 7.0, use {@link Unit#MM} instead    
      */
-    public static final int UNITS_MM = 5;
+    @Deprecated
+    public static final Unit UNITS_MM = Unit.MM;
 
     /**
-     * Unit code representing centimeters.
+     * @deprecated from 7.0, use {@link Unit#CM} instead    
      */
-    public static final int UNITS_CM = 6;
+    @Deprecated
+    public static final Unit UNITS_CM = Unit.CM;
 
     /**
-     * Unit code representing inches.
+     * @deprecated from 7.0, use {@link Unit#INCH} instead    
      */
-    public static final int UNITS_INCH = 7;
+    @Deprecated
+    public static final Unit UNITS_INCH = Unit.INCH;
 
     /**
-     * Unit code representing in percentage of the containing element defined by
-     * terminal.
+     * @deprecated from 7.0, use {@link Unit#PERCENTAGE} instead    
      */
-    public static final int UNITS_PERCENTAGE = 8;
+    @Deprecated
+    public static final Unit UNITS_PERCENTAGE = Unit.PERCENTAGE;
 
     public static final float SIZE_UNDEFINED = -1;
 
-    /**
-     * Textual representations of units symbols. Supported units and their
-     * symbols are:
-     * <ul>
-     * <li>{@link #UNITS_PIXELS}: "px"</li>
-     * <li>{@link #UNITS_POINTS}: "pt"</li>
-     * <li>{@link #UNITS_PICAS}: "pc"</li>
-     * <li>{@link #UNITS_EM}: "em"</li>
-     * <li>{@link #UNITS_EX}: "ex"</li>
-     * <li>{@link #UNITS_MM}: "mm"</li>
-     * <li>{@link #UNITS_CM}. "cm"</li>
-     * <li>{@link #UNITS_INCH}: "in"</li>
-     * <li>{@link #UNITS_PERCENTAGE}: "%"</li>
-     * </ul>
-     * These can be used like <code>Sizeable.UNIT_SYMBOLS[UNITS_PIXELS]</code>.
-     */
-    public static final String[] UNIT_SYMBOLS = { "px", "pt", "pc", "em", "ex",
-            "mm", "cm", "in", "%" };
+    public enum Unit {
+        /**
+         * Unit code representing pixels.
+         */
+        PIXELS("px"),
+        /**
+         * Unit code representing points (1/72nd of an inch).
+         */
+        POINTS("pt"),
+        /**
+         * Unit code representing picas (12 points).
+         */
+        PICAS("pc"),
+        /**
+         * Unit code representing the font-size of the relevant font.
+         */
+        EM("em"),
+        /**
+         * Unit code representing the x-height of the relevant font.
+         */
+        EX("ex"),
+        /**
+         * Unit code representing millimeters.
+         */
+        MM("mm"),
+        /**
+         * Unit code representing centimeters.
+         */
+        CM("cm"),
+        /**
+         * Unit code representing inches.
+         */
+        INCH("in"),
+        /**
+         * Unit code representing in percentage of the containing element
+         * defined by terminal.
+         */
+        PERCENTAGE("%");
+
+        private String symbol;
+
+        private Unit(String symbol) {
+            this.symbol = symbol;
+        }
+
+        public String getSymbol() {
+            return symbol;
+        }
+
+        @Override
+        public String toString() {
+            return symbol;
+        }
+
+        public static Unit getUnitFromSymbol(String symbol) {
+            if (symbol == null) {
+                return null;
+            }
+            for (Unit unit : Unit.values()) {
+                if (symbol.equals(unit.getSymbol())) {
+                    return unit;
+                }
+            }
+            return null;
+        }
+    }
 
     /**
      * Gets the width of the object. Negative number implies unspecified size
@@ -105,14 +161,14 @@ public interface Sizeable extends Serializable {
      * 
      * @return units used in width property.
      */
-    public int getWidthUnits();
+    public Unit getWidthUnits();
 
     /**
      * Gets the height property units.
      * 
      * @return units used in height property.
      */
-    public int getHeightUnits();
+    public Unit getHeightUnits();
 
     /**
      * Sets the height of the component using String presentation.
@@ -139,13 +195,9 @@ public interface Sizeable extends Serializable {
      * @param width
      *            the width of the object.
      * @param unit
-     *            the unit used for the width. Possible values include
-     *            {@link #UNITS_PIXELS}, {@link #UNITS_POINTS},
-     *            {@link #UNITS_PICAS}, {@link #UNITS_EM}, {@link #UNITS_EX},
-     *            {@link #UNITS_MM}, {@link #UNITS_CM}, {@link #UNITS_INCH},
-     *            {@link #UNITS_PERCENTAGE}.
+     *            the unit used for the width.
      */
-    public void setWidth(float width, int unit);
+    public void setWidth(float width, Unit unit);
 
     /**
      * Sets the height of the object. Negative number implies unspecified size
@@ -154,13 +206,9 @@ public interface Sizeable extends Serializable {
      * @param height
      *            the height of the object.
      * @param unit
-     *            the unit used for the width. Possible values include
-     *            {@link #UNITS_PIXELS}, {@link #UNITS_POINTS},
-     *            {@link #UNITS_PICAS}, {@link #UNITS_EM}, {@link #UNITS_EX},
-     *            {@link #UNITS_MM}, {@link #UNITS_CM}, {@link #UNITS_INCH},
-     *            {@link #UNITS_PERCENTAGE}.
+     *            the unit used for the width.
      */
-    public void setHeight(float height, int unit);
+    public void setHeight(float height, Unit unit);
 
     /**
      * Sets the width of the component using String presentation.
index 97ae19a8dd96da3914e97e218feae572b9cba90c..fad5220eb7f04d5e554bdd296d370745f301bbb9 100644 (file)
@@ -16,7 +16,7 @@ import java.util.Vector;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import com.vaadin.terminal.Sizeable;
+import com.vaadin.terminal.Sizeable.Unit;
 import com.vaadin.ui.AbstractOrderedLayout;
 import com.vaadin.ui.Component;
 import com.vaadin.ui.ComponentContainer;
@@ -314,7 +314,7 @@ public class ComponentSizeValidator implements Serializable {
             width += "MAIN WINDOW";
         } else if (component.getWidth() >= 0) {
             width += "ABSOLUTE, " + component.getWidth() + " "
-                    + Sizeable.UNIT_SYMBOLS[component.getWidthUnits()];
+                    + component.getWidthUnits().getSymbol();
         } else {
             width += "UNDEFINED";
         }
@@ -330,7 +330,7 @@ public class ComponentSizeValidator implements Serializable {
             height += "MAIN WINDOW";
         } else if (component.getHeight() > 0) {
             height += "ABSOLUTE, " + component.getHeight() + " "
-                    + Sizeable.UNIT_SYMBOLS[component.getHeightUnits()];
+                    + component.getHeightUnits().getSymbol();
         } else {
             height += "UNDEFINED";
         }
@@ -477,7 +477,7 @@ public class ComponentSizeValidator implements Serializable {
     }
 
     private static boolean hasRelativeHeight(Component component) {
-        return (component.getHeightUnits() == Sizeable.UNITS_PERCENTAGE && component
+        return (component.getHeightUnits() == Unit.PERCENTAGE && component
                 .getHeight() > 0);
     }
 
@@ -493,7 +493,7 @@ public class ComponentSizeValidator implements Serializable {
 
     private static boolean hasRelativeWidth(Component paintable) {
         return paintable.getWidth() > 0
-                && paintable.getWidthUnits() == Sizeable.UNITS_PERCENTAGE;
+                && paintable.getWidthUnits() == Unit.PERCENTAGE;
     }
 
     public static boolean parentCanDefineWidth(Component component) {
index 7872e0577411e87d7ce658e0d2bc6451f3cc5edd..3bea3b0847a4d03e9b28b4aa5503acd4ff11bd5c 100644 (file)
@@ -176,10 +176,10 @@ public class AbsoluteLayout extends AbstractLayout implements
         private Float bottomValue = null;
         private Float leftValue = null;
 
-        private int topUnits;
-        private int rightUnits;
-        private int bottomUnits;
-        private int leftUnits;
+        private Unit topUnits;
+        private Unit rightUnits;
+        private Unit bottomUnits;
+        private Unit leftUnits;
 
         /**
          * Sets the position attributes using CSS syntax. Attributes not
@@ -193,7 +193,7 @@ public class AbsoluteLayout extends AbstractLayout implements
          */
         public void setCSSString(String css) {
             topValue = rightValue = bottomValue = leftValue = null;
-            topUnits = rightUnits = bottomUnits = leftUnits = 0;
+            topUnits = rightUnits = bottomUnits = leftUnits = null;
             zIndex = -1;
             if (css == null) {
                 return;
@@ -215,47 +215,31 @@ public class AbsoluteLayout extends AbstractLayout implements
                     } else {
                         value = "";
                     }
-                    String unit = value.replaceAll("[0-9\\.\\-]+", "");
-                    if (!unit.equals("")) {
-                        value = value.substring(0, value.indexOf(unit)).trim();
+                    String symbol = value.replaceAll("[0-9\\.\\-]+", "");
+                    if (!symbol.equals("")) {
+                        value = value.substring(0, value.indexOf(symbol))
+                                .trim();
                     }
                     float v = Float.parseFloat(value);
-                    int unitInt = parseCssUnit(unit);
+                    Unit unit = Unit.getUnitFromSymbol(symbol);
                     if (key.equals("top")) {
                         topValue = v;
-                        topUnits = unitInt;
+                        topUnits = unit;
                     } else if (key.equals("right")) {
                         rightValue = v;
-                        rightUnits = unitInt;
+                        rightUnits = unit;
                     } else if (key.equals("bottom")) {
                         bottomValue = v;
-                        bottomUnits = unitInt;
+                        bottomUnits = unit;
                     } else if (key.equals("left")) {
                         leftValue = v;
-                        leftUnits = unitInt;
+                        leftUnits = unit;
                     }
                 }
             }
             requestRepaint();
         }
 
-        /**
-         * Parses a string and checks if a unit is found. If a unit is not found
-         * from the string the unit pixels is used.
-         * 
-         * @param string
-         *            The string to parse the unit from
-         * @return The found unit
-         */
-        private int parseCssUnit(String string) {
-            for (int i = 0; i < UNIT_SYMBOLS.length; i++) {
-                if (UNIT_SYMBOLS[i].equals(string)) {
-                    return i;
-                }
-            }
-            return 0; // defaults to px (eg. top:0;)
-        }
-
         /**
          * Converts the internal values into a valid CSS string.
          * 
@@ -264,16 +248,16 @@ public class AbsoluteLayout extends AbstractLayout implements
         public String getCSSString() {
             String s = "";
             if (topValue != null) {
-                s += "top:" + topValue + UNIT_SYMBOLS[topUnits] + ";";
+                s += "top:" + topValue + topUnits.getSymbol() + ";";
             }
             if (rightValue != null) {
-                s += "right:" + rightValue + UNIT_SYMBOLS[rightUnits] + ";";
+                s += "right:" + rightValue + rightUnits.getSymbol() + ";";
             }
             if (bottomValue != null) {
-                s += "bottom:" + bottomValue + UNIT_SYMBOLS[bottomUnits] + ";";
+                s += "bottom:" + bottomValue + bottomUnits.getSymbol() + ";";
             }
             if (leftValue != null) {
-                s += "left:" + leftValue + UNIT_SYMBOLS[leftUnits] + ";";
+                s += "left:" + leftValue + leftUnits.getSymbol() + ";";
             }
             if (zIndex >= 0) {
                 s += "z-index:" + zIndex + ";";
@@ -291,7 +275,7 @@ public class AbsoluteLayout extends AbstractLayout implements
          *            The unit of the 'top' attribute. See UNIT_SYMBOLS for a
          *            description of the available units.
          */
-        public void setTop(Float topValue, int topUnits) {
+        public void setTop(Float topValue, Unit topUnits) {
             this.topValue = topValue;
             this.topUnits = topUnits;
             requestRepaint();
@@ -307,7 +291,7 @@ public class AbsoluteLayout extends AbstractLayout implements
          *            The unit of the 'right' attribute. See UNIT_SYMBOLS for a
          *            description of the available units.
          */
-        public void setRight(Float rightValue, int rightUnits) {
+        public void setRight(Float rightValue, Unit rightUnits) {
             this.rightValue = rightValue;
             this.rightUnits = rightUnits;
             requestRepaint();
@@ -323,7 +307,7 @@ public class AbsoluteLayout extends AbstractLayout implements
          *            The unit of the 'bottom' attribute. See UNIT_SYMBOLS for a
          *            description of the available units.
          */
-        public void setBottom(Float bottomValue, int bottomUnits) {
+        public void setBottom(Float bottomValue, Unit bottomUnits) {
             this.bottomValue = bottomValue;
             this.bottomUnits = bottomUnits;
             requestRepaint();
@@ -339,7 +323,7 @@ public class AbsoluteLayout extends AbstractLayout implements
          *            The unit of the 'left' attribute. See UNIT_SYMBOLS for a
          *            description of the available units.
          */
-        public void setLeft(Float leftValue, int leftUnits) {
+        public void setLeft(Float leftValue, Unit leftUnits) {
             this.leftValue = leftValue;
             this.leftUnits = leftUnits;
             requestRepaint();
@@ -456,7 +440,7 @@ public class AbsoluteLayout extends AbstractLayout implements
          * @return See {@link Sizeable} UNIT_SYMBOLS for a description of the
          *         available units.
          */
-        public int getTopUnits() {
+        public Unit getTopUnits() {
             return topUnits;
         }
 
@@ -467,7 +451,7 @@ public class AbsoluteLayout extends AbstractLayout implements
          *            See {@link Sizeable} UNIT_SYMBOLS for a description of the
          *            available units.
          */
-        public void setTopUnits(int topUnits) {
+        public void setTopUnits(Unit topUnits) {
             this.topUnits = topUnits;
             requestRepaint();
         }
@@ -478,7 +462,7 @@ public class AbsoluteLayout extends AbstractLayout implements
          * @return See {@link Sizeable} UNIT_SYMBOLS for a description of the
          *         available units.
          */
-        public int getRightUnits() {
+        public Unit getRightUnits() {
             return rightUnits;
         }
 
@@ -489,7 +473,7 @@ public class AbsoluteLayout extends AbstractLayout implements
          *            See {@link Sizeable} UNIT_SYMBOLS for a description of the
          *            available units.
          */
-        public void setRightUnits(int rightUnits) {
+        public void setRightUnits(Unit rightUnits) {
             this.rightUnits = rightUnits;
             requestRepaint();
         }
@@ -500,7 +484,7 @@ public class AbsoluteLayout extends AbstractLayout implements
          * @return See {@link Sizeable} UNIT_SYMBOLS for a description of the
          *         available units.
          */
-        public int getBottomUnits() {
+        public Unit getBottomUnits() {
             return bottomUnits;
         }
 
@@ -511,7 +495,7 @@ public class AbsoluteLayout extends AbstractLayout implements
          *            See {@link Sizeable} UNIT_SYMBOLS for a description of the
          *            available units.
          */
-        public void setBottomUnits(int bottomUnits) {
+        public void setBottomUnits(Unit bottomUnits) {
             this.bottomUnits = bottomUnits;
             requestRepaint();
         }
@@ -522,7 +506,7 @@ public class AbsoluteLayout extends AbstractLayout implements
          * @return See {@link Sizeable} UNIT_SYMBOLS for a description of the
          *         available units.
          */
-        public int getLeftUnits() {
+        public Unit getLeftUnits() {
             return leftUnits;
         }
 
@@ -533,7 +517,7 @@ public class AbsoluteLayout extends AbstractLayout implements
          *            See {@link Sizeable} UNIT_SYMBOLS for a description of the
          *            available units.
          */
-        public void setLeftUnits(int leftUnits) {
+        public void setLeftUnits(Unit leftUnits) {
             this.leftUnits = leftUnits;
             requestRepaint();
         }
index 70d7fe32a9ede8f70a93d7a610f2ebd92b0881d1..7462aab3a58abe40fcfdca33e85394aedc075efa 100644 (file)
@@ -140,8 +140,8 @@ public abstract class AbstractComponent implements Component, MethodEventSource
 
     private float width = SIZE_UNDEFINED;
     private float height = SIZE_UNDEFINED;
-    private int widthUnit = UNITS_PIXELS;
-    private int heightUnit = UNITS_PIXELS;
+    private Unit widthUnit = Unit.PIXELS;
+    private Unit heightUnit = Unit.PIXELS;
     private static final Pattern sizePattern = Pattern
             .compile("^(-?\\d+(\\.\\d+)?)(%|px|em|ex|in|cm|mm|pt|pc)?$");
 
@@ -743,13 +743,13 @@ public abstract class AbstractComponent implements Component, MethodEventSource
             // Only paint content of visible components.
             if (isVisible()) {
                 if (getHeight() >= 0
-                        && (getHeightUnits() != UNITS_PERCENTAGE || ComponentSizeValidator
+                        && (getHeightUnits() != Unit.PERCENTAGE || ComponentSizeValidator
                                 .parentCanDefineHeight(this))) {
                     target.addAttribute("height", "" + getCSSHeight());
                 }
 
                 if (getWidth() >= 0
-                        && (getWidthUnits() != UNITS_PERCENTAGE || ComponentSizeValidator
+                        && (getWidthUnits() != Unit.PERCENTAGE || ComponentSizeValidator
                                 .parentCanDefineWidth(this))) {
                     target.addAttribute("width", "" + getCSSWidth());
                 }
@@ -807,10 +807,10 @@ public abstract class AbstractComponent implements Component, MethodEventSource
      * @return CSS height
      */
     private String getCSSHeight() {
-        if (getHeightUnits() == UNITS_PIXELS) {
-            return ((int) getHeight()) + UNIT_SYMBOLS[getHeightUnits()];
+        if (getHeightUnits() == Unit.PIXELS) {
+            return ((int) getHeight()) + getHeightUnits().getSymbol();
         } else {
-            return getHeight() + UNIT_SYMBOLS[getHeightUnits()];
+            return getHeight() + getHeightUnits().getSymbol();
         }
     }
 
@@ -820,10 +820,10 @@ public abstract class AbstractComponent implements Component, MethodEventSource
      * @return CSS width
      */
     private String getCSSWidth() {
-        if (getWidthUnits() == UNITS_PIXELS) {
-            return ((int) getWidth()) + UNIT_SYMBOLS[getWidthUnits()];
+        if (getWidthUnits() == Unit.PIXELS) {
+            return ((int) getWidth()) + getWidthUnits().getSymbol();
         } else {
-            return getWidth() + UNIT_SYMBOLS[getWidthUnits()];
+            return getWidth() + getWidthUnits().getSymbol();
         }
     }
 
@@ -1299,7 +1299,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource
      * 
      * @see com.vaadin.terminal.Sizeable#getHeightUnits()
      */
-    public int getHeightUnits() {
+    public Unit getHeightUnits() {
         return heightUnit;
     }
 
@@ -1317,7 +1317,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource
      * 
      * @see com.vaadin.terminal.Sizeable#getWidthUnits()
      */
-    public int getWidthUnits() {
+    public Unit getWidthUnits() {
         return widthUnit;
     }
 
@@ -1326,7 +1326,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource
      * 
      * @see com.vaadin.terminal.Sizeable#setHeight(float, int)
      */
-    public void setHeight(float height, int unit) {
+    public void setHeight(float height, Unit unit) {
         this.height = height;
         heightUnit = unit;
         requestRepaint();
@@ -1339,8 +1339,8 @@ public abstract class AbstractComponent implements Component, MethodEventSource
      * @see com.vaadin.terminal.Sizeable#setSizeFull()
      */
     public void setSizeFull() {
-        setWidth(100, UNITS_PERCENTAGE);
-        setHeight(100, UNITS_PERCENTAGE);
+        setWidth(100, Unit.PERCENTAGE);
+        setHeight(100, Unit.PERCENTAGE);
     }
 
     /*
@@ -1349,8 +1349,8 @@ public abstract class AbstractComponent implements Component, MethodEventSource
      * @see com.vaadin.terminal.Sizeable#setSizeUndefined()
      */
     public void setSizeUndefined() {
-        setWidth(-1, UNITS_PIXELS);
-        setHeight(-1, UNITS_PIXELS);
+        setWidth(-1, Unit.PIXELS);
+        setHeight(-1, Unit.PIXELS);
     }
 
     /*
@@ -1358,7 +1358,7 @@ public abstract class AbstractComponent implements Component, MethodEventSource
      * 
      * @see com.vaadin.terminal.Sizeable#setWidth(float, int)
      */
-    public void setWidth(float width, int unit) {
+    public void setWidth(float width, Unit unit) {
         this.width = width;
         widthUnit = unit;
         requestRepaint();
@@ -1371,8 +1371,8 @@ public abstract class AbstractComponent implements Component, MethodEventSource
      * @see com.vaadin.terminal.Sizeable#setWidth(java.lang.String)
      */
     public void setWidth(String width) {
-        float[] p = parseStringSize(width);
-        setWidth(p[0], (int) p[1]);
+        Size size = parseStringSize(width);
+        setWidth(size.getSize(), size.getUnit());
     }
 
     /*
@@ -1381,58 +1381,56 @@ public abstract class AbstractComponent implements Component, MethodEventSource
      * @see com.vaadin.terminal.Sizeable#setHeight(java.lang.String)
      */
     public void setHeight(String height) {
-        float[] p = parseStringSize(height);
-        setHeight(p[0], (int) p[1]);
+        Size size = parseStringSize(height);
+        setHeight(size.getSize(), size.getUnit());
     }
 
     /*
      * Returns array with size in index 0 unit in index 1. Null or empty string
-     * will produce {-1,UNITS_PIXELS}
+     * will produce {-1,Unit#PIXELS}
      */
-    private static float[] parseStringSize(String s) {
-        float[] values = { -1, UNITS_PIXELS };
+    private static Size parseStringSize(String s) {
         if (s == null) {
-            return values;
+            return null;
         }
         s = s.trim();
         if ("".equals(s)) {
-            return values;
+            return null;
         }
-
+        float size = 0;
+        Unit unit = null;
         Matcher matcher = sizePattern.matcher(s);
         if (matcher.find()) {
-            values[0] = Float.parseFloat(matcher.group(1));
-            if (values[0] < 0) {
-                values[0] = -1;
+            size = Float.parseFloat(matcher.group(1));
+            if (size < 0) {
+                size = -1;
             } else {
-                String unit = matcher.group(3);
-                if (unit == null) {
-                    values[1] = UNITS_PIXELS;
-                } else if (unit.equals("px")) {
-                    values[1] = UNITS_PIXELS;
-                } else if (unit.equals("%")) {
-                    values[1] = UNITS_PERCENTAGE;
-                } else if (unit.equals("em")) {
-                    values[1] = UNITS_EM;
-                } else if (unit.equals("ex")) {
-                    values[1] = UNITS_EX;
-                } else if (unit.equals("in")) {
-                    values[1] = UNITS_INCH;
-                } else if (unit.equals("cm")) {
-                    values[1] = UNITS_CM;
-                } else if (unit.equals("mm")) {
-                    values[1] = UNITS_MM;
-                } else if (unit.equals("pt")) {
-                    values[1] = UNITS_POINTS;
-                } else if (unit.equals("pc")) {
-                    values[1] = UNITS_PICAS;
-                }
+                String symbol = matcher.group(3);
+                unit = Unit.getUnitFromSymbol(symbol);
             }
         } else {
             throw new IllegalArgumentException("Invalid size argument: \"" + s
                     + "\" (should match " + sizePattern.pattern() + ")");
         }
-        return values;
+        return new Size(size, unit);
+    }
+
+    private static class Size {
+        float size;
+        Unit unit;
+
+        public Size(float size, Unit unit) {
+            this.size = size;
+            this.unit = unit;
+        }
+
+        public float getSize() {
+            return size;
+        }
+
+        public Unit getUnit() {
+            return unit;
+        }
     }
 
     public interface ComponentErrorEvent extends Terminal.ErrorEvent {
index 5d5218307a4d19de0c0265f01c7c6263afbc41e0..bbe2c20ea5282ef0908ae0a7fe41c726dfdd5308 100644 (file)
@@ -226,7 +226,7 @@ public abstract class AbstractComponentContainer extends AbstractComponent
     }
 
     @Override
-    public void setWidth(float width, int unit) {
+    public void setWidth(float width, Unit unit) {
         /*
          * child tree repaints may be needed, due to our fall back support for
          * invalid relative sizes
@@ -237,9 +237,9 @@ public abstract class AbstractComponentContainer extends AbstractComponent
             // children currently in invalid state may need repaint
             dirtyChildren = getInvalidSizedChildren(false);
         } else if ((width == SIZE_UNDEFINED && getWidth() != SIZE_UNDEFINED)
-                || (unit == UNITS_PERCENTAGE
-                        && getWidthUnits() != UNITS_PERCENTAGE && !ComponentSizeValidator
-                        .parentCanDefineWidth(this))) {
+                || (unit == Unit.PERCENTAGE
+                        && getWidthUnits() != Unit.PERCENTAGE && !ComponentSizeValidator
+                            .parentCanDefineWidth(this))) {
             /*
              * relative width children may get to invalid state if width becomes
              * invalid. Width may also become invalid if units become percentage
@@ -326,7 +326,7 @@ public abstract class AbstractComponentContainer extends AbstractComponent
     }
 
     @Override
-    public void setHeight(float height, int unit) {
+    public void setHeight(float height, Unit unit) {
         /*
          * child tree repaints may be needed, due to our fall back support for
          * invalid relative sizes
@@ -337,9 +337,9 @@ public abstract class AbstractComponentContainer extends AbstractComponent
             // children currently in invalid state may need repaint
             dirtyChildren = getInvalidSizedChildren(true);
         } else if ((height == SIZE_UNDEFINED && getHeight() != SIZE_UNDEFINED)
-                || (unit == UNITS_PERCENTAGE
-                        && getHeightUnits() != UNITS_PERCENTAGE && !ComponentSizeValidator
-                        .parentCanDefineHeight(this))) {
+                || (unit == Unit.PERCENTAGE
+                        && getHeightUnits() != Unit.PERCENTAGE && !ComponentSizeValidator
+                            .parentCanDefineHeight(this))) {
             /*
              * relative height children may get to invalid state if height
              * becomes invalid. Height may also become invalid if units become
index adb84f9d9d7a11df12d58950b6eb55bb4dfc80d6..e0a2d24aaa81beae190fc88d9b8001671fda3fda 100644 (file)
@@ -37,7 +37,7 @@ public abstract class AbstractSplitPanel extends AbstractLayout {
 
     private int pos = 50;
 
-    private int posUnit = UNITS_PERCENTAGE;
+    private Unit posUnit = Unit.PERCENTAGE;
 
     private boolean posReversed = false;
 
@@ -209,7 +209,7 @@ public abstract class AbstractSplitPanel extends AbstractLayout {
     public void paintContent(PaintTarget target) throws PaintException {
         super.paintContent(target);
 
-        final String position = pos + UNIT_SYMBOLS[posUnit];
+        final String position = pos + posUnit.getSymbol();
 
         target.addAttribute("position", position);
 
@@ -278,7 +278,7 @@ public abstract class AbstractSplitPanel extends AbstractLayout {
      * @param unit
      *            the unit (from {@link Sizeable}) in which the size is given.
      */
-    public void setSplitPosition(int pos, int unit) {
+    public void setSplitPosition(int pos, Unit unit) {
         setSplitPosition(pos, unit, true, false);
     }
 
@@ -294,7 +294,7 @@ public abstract class AbstractSplitPanel extends AbstractLayout {
      *            second region else it is measured by the first region
      * 
      */
-    public void setSplitPosition(int pos, int unit, boolean reverse) {
+    public void setSplitPosition(int pos, Unit unit, boolean reverse) {
         setSplitPosition(pos, unit, true, reverse);
     }
 
@@ -313,7 +313,7 @@ public abstract class AbstractSplitPanel extends AbstractLayout {
      * 
      * @return unit of position of the splitter
      */
-    public int getSplitPositionUnit() {
+    public Unit getSplitPositionUnit() {
         return posUnit;
     }
 
@@ -329,9 +329,9 @@ public abstract class AbstractSplitPanel extends AbstractLayout {
      *            position info has come from the client side, thus it already
      *            knows the position.
      */
-    private void setSplitPosition(int pos, int unit, boolean repaintNeeded,
+    private void setSplitPosition(int pos, Unit unit, boolean repaintNeeded,
             boolean reverse) {
-        if (unit != UNITS_PERCENTAGE && unit != UNITS_PIXELS) {
+        if (unit != Unit.PERCENTAGE && unit != Unit.PIXELS) {
             throw new IllegalArgumentException(
                     "Only percentage and pixel units are allowed");
         }
index ca075a6a6af29270a200fc3d1c6d0564e5bb80fa..62e26d0017bea22bd24890f1fc443806cb1ce007 100644 (file)
@@ -52,7 +52,7 @@ public abstract class CustomField<T> extends AbstractField<T> implements
      */
     public CustomField() {
         // expand horizontally by default
-        setWidth(100, UNITS_PERCENTAGE);
+        setWidth(100, Unit.PERCENTAGE);
     }
 
     /**
@@ -135,13 +135,13 @@ public abstract class CustomField<T> extends AbstractField<T> implements
     // AbstractComponentContainer
 
     @Override
-    public void setHeight(float height, int unit) {
+    public void setHeight(float height, Unit unit) {
         super.setHeight(height, unit);
         requestContentRepaint();
     }
 
     @Override
-    public void setWidth(float height, int unit) {
+    public void setWidth(float height, Unit unit) {
         super.setWidth(height, unit);
         requestContentRepaint();
     }
index 369bc45fe4149a37e00492d381cb5af1ff67d1e3..7eaeb824c96587ffa4e87e7bc209ef53d35c29ed 100644 (file)
@@ -263,7 +263,7 @@ public class LoginForm extends CustomComponent {
     }
 
     @Override
-    public void setWidth(float width, int unit) {
+    public void setWidth(float width, Unit unit) {
         super.setWidth(width, unit);
         if (iframe != null) {
             if (width < 0) {
@@ -275,7 +275,7 @@ public class LoginForm extends CustomComponent {
     }
 
     @Override
-    public void setHeight(float height, int unit) {
+    public void setHeight(float height, Unit unit) {
         super.setHeight(height, unit);
         if (iframe != null) {
             if (height < 0) {
index 6fc7fbba0d12bb322df7dc9437a46964e3986418..ba4544821315e4f0a68688317516d4ffbf824d41 100644 (file)
@@ -3,6 +3,7 @@ package com.vaadin.tests.server.component.absolutelayout;
 import junit.framework.TestCase;\r
 \r
 import com.vaadin.terminal.Sizeable;\r
+import com.vaadin.terminal.Sizeable.Unit;\r
 import com.vaadin.ui.AbsoluteLayout;\r
 import com.vaadin.ui.Button;\r
 \r
@@ -12,7 +13,7 @@ public class ComponentPosition extends TestCase {
     private static final String PARTIAL_CSS = "top:7.0px;left:7.0em;";\r
     private static final Float CSS_VALUE = Float.valueOf(7);\r
 \r
-    private static final int UNIT_UNSET = Sizeable.UNITS_PIXELS;\r
+    private static final Unit UNIT_UNSET = Sizeable.Unit.PIXELS;\r
 \r
     /**\r
      * Add component w/o giving positions, assert that everything is unset\r
@@ -51,11 +52,11 @@ public class ComponentPosition extends TestCase {
         assertEquals(CSS_VALUE, layout.getPosition(b).getLeftValue());\r
         assertEquals(CSS_VALUE, layout.getPosition(b).getRightValue());\r
 \r
-        assertEquals(Sizeable.UNITS_PIXELS, layout.getPosition(b).getTopUnits());\r
-        assertEquals(Sizeable.UNITS_PICAS, layout.getPosition(b)\r
+        assertEquals(Sizeable.Unit.PIXELS, layout.getPosition(b).getTopUnits());\r
+        assertEquals(Sizeable.Unit.PICAS, layout.getPosition(b)\r
                 .getBottomUnits());\r
-        assertEquals(Sizeable.UNITS_EM, layout.getPosition(b).getLeftUnits());\r
-        assertEquals(Sizeable.UNITS_PERCENTAGE, layout.getPosition(b)\r
+        assertEquals(Sizeable.Unit.EM, layout.getPosition(b).getLeftUnits());\r
+        assertEquals(Sizeable.Unit.PERCENTAGE, layout.getPosition(b)\r
                 .getRightUnits());\r
 \r
         assertEquals(7, layout.getPosition(b).getZIndex());\r
@@ -77,9 +78,9 @@ public class ComponentPosition extends TestCase {
         assertEquals(CSS_VALUE, layout.getPosition(b).getLeftValue());\r
         assertNull(layout.getPosition(b).getRightValue());\r
 \r
-        assertEquals(Sizeable.UNITS_PIXELS, layout.getPosition(b).getTopUnits());\r
+        assertEquals(Sizeable.Unit.PIXELS, layout.getPosition(b).getTopUnits());\r
         assertEquals(UNIT_UNSET, layout.getPosition(b).getBottomUnits());\r
-        assertEquals(Sizeable.UNITS_EM, layout.getPosition(b).getLeftUnits());\r
+        assertEquals(Sizeable.Unit.EM, layout.getPosition(b).getLeftUnits());\r
         assertEquals(UNIT_UNSET, layout.getPosition(b).getRightUnits());\r
 \r
         assertEquals(-1, layout.getPosition(b).getZIndex());\r
@@ -104,9 +105,9 @@ public class ComponentPosition extends TestCase {
         assertEquals(CSS_VALUE, layout.getPosition(b).getLeftValue());\r
         assertNull(layout.getPosition(b).getRightValue());\r
 \r
-        assertEquals(Sizeable.UNITS_PIXELS, layout.getPosition(b).getTopUnits());\r
+        assertEquals(Sizeable.Unit.PIXELS, layout.getPosition(b).getTopUnits());\r
         assertEquals(UNIT_UNSET, layout.getPosition(b).getBottomUnits());\r
-        assertEquals(Sizeable.UNITS_EM, layout.getPosition(b).getLeftUnits());\r
+        assertEquals(Sizeable.Unit.EM, layout.getPosition(b).getLeftUnits());\r
         assertEquals(UNIT_UNSET, layout.getPosition(b).getRightUnits());\r
 \r
         assertEquals(-1, layout.getPosition(b).getZIndex());\r
@@ -131,21 +132,20 @@ public class ComponentPosition extends TestCase {
         layout.getPosition(b).setBottomValue(SIZE);\r
         layout.getPosition(b).setLeftValue(SIZE);\r
 \r
-        layout.getPosition(b).setTopUnits(Sizeable.UNITS_CM);\r
-        layout.getPosition(b).setRightUnits(Sizeable.UNITS_EX);\r
-        layout.getPosition(b).setBottomUnits(Sizeable.UNITS_INCH);\r
-        layout.getPosition(b).setLeftUnits(Sizeable.UNITS_MM);\r
+        layout.getPosition(b).setTopUnits(Sizeable.Unit.CM);\r
+        layout.getPosition(b).setRightUnits(Sizeable.Unit.EX);\r
+        layout.getPosition(b).setBottomUnits(Sizeable.Unit.INCH);\r
+        layout.getPosition(b).setLeftUnits(Sizeable.Unit.MM);\r
 \r
         assertEquals(SIZE, layout.getPosition(b).getTopValue());\r
         assertEquals(SIZE, layout.getPosition(b).getRightValue());\r
         assertEquals(SIZE, layout.getPosition(b).getBottomValue());\r
         assertEquals(SIZE, layout.getPosition(b).getLeftValue());\r
 \r
-        assertEquals(Sizeable.UNITS_CM, layout.getPosition(b).getTopUnits());\r
-        assertEquals(Sizeable.UNITS_EX, layout.getPosition(b).getRightUnits());\r
-        assertEquals(Sizeable.UNITS_INCH, layout.getPosition(b)\r
-                .getBottomUnits());\r
-        assertEquals(Sizeable.UNITS_MM, layout.getPosition(b).getLeftUnits());\r
+        assertEquals(Sizeable.Unit.CM, layout.getPosition(b).getTopUnits());\r
+        assertEquals(Sizeable.Unit.EX, layout.getPosition(b).getRightUnits());\r
+        assertEquals(Sizeable.Unit.INCH, layout.getPosition(b).getBottomUnits());\r
+        assertEquals(Sizeable.Unit.MM, layout.getPosition(b).getLeftUnits());\r
 \r
     }\r
 \r
@@ -159,21 +159,20 @@ public class ComponentPosition extends TestCase {
         Button b = new Button();\r
         layout.addComponent(b);\r
 \r
-        layout.getPosition(b).setTop(SIZE, Sizeable.UNITS_CM);\r
-        layout.getPosition(b).setRight(SIZE, Sizeable.UNITS_EX);\r
-        layout.getPosition(b).setBottom(SIZE, Sizeable.UNITS_INCH);\r
-        layout.getPosition(b).setLeft(SIZE, Sizeable.UNITS_MM);\r
+        layout.getPosition(b).setTop(SIZE, Sizeable.Unit.CM);\r
+        layout.getPosition(b).setRight(SIZE, Sizeable.Unit.EX);\r
+        layout.getPosition(b).setBottom(SIZE, Sizeable.Unit.INCH);\r
+        layout.getPosition(b).setLeft(SIZE, Sizeable.Unit.MM);\r
 \r
         assertEquals(SIZE, layout.getPosition(b).getTopValue());\r
         assertEquals(SIZE, layout.getPosition(b).getRightValue());\r
         assertEquals(SIZE, layout.getPosition(b).getBottomValue());\r
         assertEquals(SIZE, layout.getPosition(b).getLeftValue());\r
 \r
-        assertEquals(Sizeable.UNITS_CM, layout.getPosition(b).getTopUnits());\r
-        assertEquals(Sizeable.UNITS_EX, layout.getPosition(b).getRightUnits());\r
-        assertEquals(Sizeable.UNITS_INCH, layout.getPosition(b)\r
-                .getBottomUnits());\r
-        assertEquals(Sizeable.UNITS_MM, layout.getPosition(b).getLeftUnits());\r
+        assertEquals(Sizeable.Unit.CM, layout.getPosition(b).getTopUnits());\r
+        assertEquals(Sizeable.Unit.EX, layout.getPosition(b).getRightUnits());\r
+        assertEquals(Sizeable.Unit.INCH, layout.getPosition(b).getBottomUnits());\r
+        assertEquals(Sizeable.Unit.MM, layout.getPosition(b).getLeftUnits());\r
 \r
     }\r
 \r
index 0b702db33ab7bbf74c7b5ca231a952a9e8099faa..ba3c210006b122cbe27572154d6654b6a6ef4950 100644 (file)
@@ -1,6 +1,7 @@
 package com.vaadin.tests.components.splitpanel;\r
 \r
 import com.vaadin.terminal.Sizeable;\r
+import com.vaadin.terminal.Sizeable.Unit;\r
 import com.vaadin.tests.components.AbstractLayoutTest;\r
 import com.vaadin.ui.AbstractSplitPanel;\r
 import com.vaadin.ui.AbstractSplitPanel.SplitterClickEvent;\r
@@ -55,7 +56,7 @@ public abstract class AbstractSplitPanelTest<T extends AbstractSplitPanel>
 \r
         private boolean reverse = false;\r
         private int position;\r
-        private int unit;\r
+        private Unit unit;\r
         private String posString;\r
 \r
         public SplitPosition(String pos) {\r
@@ -67,10 +68,10 @@ public abstract class AbstractSplitPanelTest<T extends AbstractSplitPanel>
 \r
             if (pos.endsWith("px")) {\r
                 position = Integer.parseInt(pos.substring(0, pos.length() - 2));\r
-                unit = Sizeable.UNITS_PIXELS;\r
+                unit = Sizeable.Unit.PIXELS;\r
             } else if (pos.endsWith("%")) {\r
                 position = Integer.parseInt(pos.substring(0, pos.length() - 1));\r
-                unit = Sizeable.UNITS_PERCENTAGE;\r
+                unit = Sizeable.Unit.PERCENTAGE;\r
             } else {\r
                 throw new RuntimeException("Could not parse " + pos);\r
             }\r
index 71dc987e5603e49554eb091b581765b603e7a0bb..2ba1475cc8835410c7a7be4a29354767e84b693c 100644 (file)
@@ -1,7 +1,6 @@
 package com.vaadin.tests.components.table;
 
 import com.vaadin.data.util.MethodProperty;
-import com.vaadin.terminal.Sizeable;
 import com.vaadin.tests.components.TestBase;
 import com.vaadin.ui.Button;
 import com.vaadin.ui.Button.ClickEvent;
@@ -61,8 +60,7 @@ public class TablePageLengthUpdate extends TestBase {
     }
 
     public String getTableHeight() {
-        return "" + (int) table.getHeight()
-                + Sizeable.UNIT_SYMBOLS[table.getHeightUnits()];
+        return "" + (int) table.getHeight() + table.getHeightUnits().getSymbol();
     }
 
     public void setTableHeight(String height) {
index 9526fd7aec27d1a9db5521983a431081eb4e632a..c6563f200fc768fd987aef2f7f96c6992c7d55a9 100644 (file)
@@ -2,7 +2,6 @@ package com.vaadin.tests.components.window;
 
 import com.vaadin.data.Property;
 import com.vaadin.data.Property.ValueChangeEvent;
-import com.vaadin.terminal.Sizeable;
 import com.vaadin.tests.components.TestBase;
 import com.vaadin.ui.Button;
 import com.vaadin.ui.CheckBox;
@@ -97,9 +96,7 @@ class ResizeListenerWindow extends Window {
     }
 
     public void updateLabel() {
-        sizeLabel
-                .setValue(getWidth() + Sizeable.UNIT_SYMBOLS[getWidthUnits()]
-                        + " x " + getHeight()
-                        + Sizeable.UNIT_SYMBOLS[getHeightUnits()]);
+        sizeLabel.setValue(getWidth() + getWidthUnits().getSymbol() + " x "
+                + getHeight() + getHeightUnits().getSymbol());
     }
 }
index 4f7269284d7ead2f9581ae12f48f755b3aeb7fbe..8bdbc2b02e7adc3f4e67736a63139496a6311cb1 100644 (file)
@@ -47,10 +47,10 @@ public class TestAbsoluteLayout extends TestBase {
                 s.addContainerProperty("caption", String.class, "");\r
                 s.setItemCaptionPropertyId("caption");\r
                 s.setNullSelectionAllowed(false);\r
-                for (int i = 0; i < Layout.UNIT_SYMBOLS.length; i++) {\r
+                for (int i = 0; i < Layout.Unit.values().length; i++) {\r
                     Item unitItem = s.addItem(i);\r
                     unitItem.getItemProperty("caption").setValue(\r
-                            Layout.UNIT_SYMBOLS[i]);\r
+                            Layout.Unit.values()[i]);\r
                 }\r
                 return s;\r
             }\r
index 7c0b336a437b25d6527dc19d9f3f938eca675ce7..c280be4e4a1da3928449bca53278837141a11f71 100644 (file)
@@ -55,7 +55,7 @@ public class Ticket1435 extends Application.LegacyApplication {
 
         // Last known height before the panel was collapsed
         private float lastHeight = -1;
-        private int lastHeightUnit = -1;
+        private Unit lastHeightUnit = null;
 
         public ButtonPanel(String labelString) {
             setCompositionRoot(root);
@@ -163,7 +163,7 @@ public class Ticket1435 extends Application.LegacyApplication {
             root.setExpandRatio(container, 1);
         }
 
-        public void setHeight(int height, int unit) {
+        public void setHeight(int height, Unit unit) {
             root.setHeight(height, unit);
             lastHeight = height;
             lastHeightUnit = unit;
@@ -183,7 +183,7 @@ public class Ticket1435 extends Application.LegacyApplication {
             root.setWidth(width);
         }
 
-        public void setWidth(int width, int unit) {
+        public void setWidth(int width, Unit unit) {
             root.setWidth(width, unit);
         }
 
index de89f1baecf160c021e4eb84e47c806159b82f09..ea94635931b06187dfd6f0e5e9585c27b7a004f4 100644 (file)
@@ -3,7 +3,6 @@ package com.vaadin.tests.tickets;
 import com.vaadin.Application;
 import com.vaadin.data.Property;
 import com.vaadin.data.Property.ValueChangeEvent;
-import com.vaadin.terminal.Sizeable;
 import com.vaadin.terminal.UserError;
 import com.vaadin.ui.Button;
 import com.vaadin.ui.Label;
@@ -54,9 +53,8 @@ public class Ticket2090 extends Application.LegacyApplication {
 
     private void updateLabel() {
         label.setValue("width: " + target.getWidth()
-                + Sizeable.UNIT_SYMBOLS[target.getWidthUnits()] + ", height: "
-                + target.getHeight()
-                + Sizeable.UNIT_SYMBOLS[target.getHeightUnits()]);
+                + target.getWidthUnits().getSymbol() + ", height: "
+                + target.getHeight() + target.getHeightUnits().getSymbol());
     }
 
 }