From a327be687e901dec4108da4d3e747ebf8cf833c7 Mon Sep 17 00:00:00 2001 From: Jens Jansson Date: Wed, 21 Dec 2011 18:22:58 +0200 Subject: [PATCH] #8019 Fixed Sizable UNITS to use an Enum Unit instead of constants --- src/com/vaadin/terminal/Sizeable.java | 150 ++++++++++++------ .../gwt/server/ComponentSizeValidator.java | 10 +- src/com/vaadin/ui/AbsoluteLayout.java | 76 ++++----- src/com/vaadin/ui/AbstractComponent.java | 106 ++++++------- .../vaadin/ui/AbstractComponentContainer.java | 16 +- src/com/vaadin/ui/AbstractSplitPanel.java | 14 +- src/com/vaadin/ui/CustomField.java | 6 +- src/com/vaadin/ui/LoginForm.java | 4 +- .../absolutelayout/ComponentPosition.java | 53 +++---- .../splitpanel/AbstractSplitPanelTest.java | 7 +- .../table/TablePageLengthUpdate.java | 4 +- .../window/WindowResizeListener.java | 7 +- .../tests/layouts/TestAbsoluteLayout.java | 4 +- .../com/vaadin/tests/tickets/Ticket1435.java | 6 +- .../com/vaadin/tests/tickets/Ticket2090.java | 6 +- 15 files changed, 246 insertions(+), 223 deletions(-) diff --git a/src/com/vaadin/terminal/Sizeable.java b/src/com/vaadin/terminal/Sizeable.java index 74fb8eefbb..055c74f20f 100644 --- a/src/com/vaadin/terminal/Sizeable.java +++ b/src/com/vaadin/terminal/Sizeable.java @@ -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: - * - * These can be used like Sizeable.UNIT_SYMBOLS[UNITS_PIXELS]. - */ - 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. diff --git a/src/com/vaadin/terminal/gwt/server/ComponentSizeValidator.java b/src/com/vaadin/terminal/gwt/server/ComponentSizeValidator.java index 97ae19a8dd..fad5220eb7 100644 --- a/src/com/vaadin/terminal/gwt/server/ComponentSizeValidator.java +++ b/src/com/vaadin/terminal/gwt/server/ComponentSizeValidator.java @@ -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) { diff --git a/src/com/vaadin/ui/AbsoluteLayout.java b/src/com/vaadin/ui/AbsoluteLayout.java index 7872e05774..3bea3b0847 100644 --- a/src/com/vaadin/ui/AbsoluteLayout.java +++ b/src/com/vaadin/ui/AbsoluteLayout.java @@ -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(); } diff --git a/src/com/vaadin/ui/AbstractComponent.java b/src/com/vaadin/ui/AbstractComponent.java index 70d7fe32a9..7462aab3a5 100644 --- a/src/com/vaadin/ui/AbstractComponent.java +++ b/src/com/vaadin/ui/AbstractComponent.java @@ -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 { diff --git a/src/com/vaadin/ui/AbstractComponentContainer.java b/src/com/vaadin/ui/AbstractComponentContainer.java index 5d5218307a..bbe2c20ea5 100644 --- a/src/com/vaadin/ui/AbstractComponentContainer.java +++ b/src/com/vaadin/ui/AbstractComponentContainer.java @@ -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 diff --git a/src/com/vaadin/ui/AbstractSplitPanel.java b/src/com/vaadin/ui/AbstractSplitPanel.java index adb84f9d9d..e0a2d24aaa 100644 --- a/src/com/vaadin/ui/AbstractSplitPanel.java +++ b/src/com/vaadin/ui/AbstractSplitPanel.java @@ -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"); } diff --git a/src/com/vaadin/ui/CustomField.java b/src/com/vaadin/ui/CustomField.java index bb5f154f22..72f863ca8b 100644 --- a/src/com/vaadin/ui/CustomField.java +++ b/src/com/vaadin/ui/CustomField.java @@ -55,7 +55,7 @@ public abstract class CustomField extends AbstractField implements */ public CustomField() { // expand horizontally by default - setWidth(100, UNITS_PERCENTAGE); + setWidth(100, Unit.PERCENTAGE); } /** @@ -138,13 +138,13 @@ public abstract class CustomField extends AbstractField 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(); } diff --git a/src/com/vaadin/ui/LoginForm.java b/src/com/vaadin/ui/LoginForm.java index 369bc45fe4..7eaeb824c9 100644 --- a/src/com/vaadin/ui/LoginForm.java +++ b/src/com/vaadin/ui/LoginForm.java @@ -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) { diff --git a/tests/server-side/com/vaadin/tests/server/component/absolutelayout/ComponentPosition.java b/tests/server-side/com/vaadin/tests/server/component/absolutelayout/ComponentPosition.java index 6fc7fbba0d..ba45448213 100644 --- a/tests/server-side/com/vaadin/tests/server/component/absolutelayout/ComponentPosition.java +++ b/tests/server-side/com/vaadin/tests/server/component/absolutelayout/ComponentPosition.java @@ -3,6 +3,7 @@ package com.vaadin.tests.server.component.absolutelayout; import junit.framework.TestCase; import com.vaadin.terminal.Sizeable; +import com.vaadin.terminal.Sizeable.Unit; import com.vaadin.ui.AbsoluteLayout; import com.vaadin.ui.Button; @@ -12,7 +13,7 @@ public class ComponentPosition extends TestCase { private static final String PARTIAL_CSS = "top:7.0px;left:7.0em;"; private static final Float CSS_VALUE = Float.valueOf(7); - private static final int UNIT_UNSET = Sizeable.UNITS_PIXELS; + private static final Unit UNIT_UNSET = Sizeable.Unit.PIXELS; /** * Add component w/o giving positions, assert that everything is unset @@ -51,11 +52,11 @@ public class ComponentPosition extends TestCase { assertEquals(CSS_VALUE, layout.getPosition(b).getLeftValue()); assertEquals(CSS_VALUE, layout.getPosition(b).getRightValue()); - assertEquals(Sizeable.UNITS_PIXELS, layout.getPosition(b).getTopUnits()); - assertEquals(Sizeable.UNITS_PICAS, layout.getPosition(b) + assertEquals(Sizeable.Unit.PIXELS, layout.getPosition(b).getTopUnits()); + assertEquals(Sizeable.Unit.PICAS, layout.getPosition(b) .getBottomUnits()); - assertEquals(Sizeable.UNITS_EM, layout.getPosition(b).getLeftUnits()); - assertEquals(Sizeable.UNITS_PERCENTAGE, layout.getPosition(b) + assertEquals(Sizeable.Unit.EM, layout.getPosition(b).getLeftUnits()); + assertEquals(Sizeable.Unit.PERCENTAGE, layout.getPosition(b) .getRightUnits()); assertEquals(7, layout.getPosition(b).getZIndex()); @@ -77,9 +78,9 @@ public class ComponentPosition extends TestCase { assertEquals(CSS_VALUE, layout.getPosition(b).getLeftValue()); assertNull(layout.getPosition(b).getRightValue()); - assertEquals(Sizeable.UNITS_PIXELS, layout.getPosition(b).getTopUnits()); + assertEquals(Sizeable.Unit.PIXELS, layout.getPosition(b).getTopUnits()); assertEquals(UNIT_UNSET, layout.getPosition(b).getBottomUnits()); - assertEquals(Sizeable.UNITS_EM, layout.getPosition(b).getLeftUnits()); + assertEquals(Sizeable.Unit.EM, layout.getPosition(b).getLeftUnits()); assertEquals(UNIT_UNSET, layout.getPosition(b).getRightUnits()); assertEquals(-1, layout.getPosition(b).getZIndex()); @@ -104,9 +105,9 @@ public class ComponentPosition extends TestCase { assertEquals(CSS_VALUE, layout.getPosition(b).getLeftValue()); assertNull(layout.getPosition(b).getRightValue()); - assertEquals(Sizeable.UNITS_PIXELS, layout.getPosition(b).getTopUnits()); + assertEquals(Sizeable.Unit.PIXELS, layout.getPosition(b).getTopUnits()); assertEquals(UNIT_UNSET, layout.getPosition(b).getBottomUnits()); - assertEquals(Sizeable.UNITS_EM, layout.getPosition(b).getLeftUnits()); + assertEquals(Sizeable.Unit.EM, layout.getPosition(b).getLeftUnits()); assertEquals(UNIT_UNSET, layout.getPosition(b).getRightUnits()); assertEquals(-1, layout.getPosition(b).getZIndex()); @@ -131,21 +132,20 @@ public class ComponentPosition extends TestCase { layout.getPosition(b).setBottomValue(SIZE); layout.getPosition(b).setLeftValue(SIZE); - layout.getPosition(b).setTopUnits(Sizeable.UNITS_CM); - layout.getPosition(b).setRightUnits(Sizeable.UNITS_EX); - layout.getPosition(b).setBottomUnits(Sizeable.UNITS_INCH); - layout.getPosition(b).setLeftUnits(Sizeable.UNITS_MM); + layout.getPosition(b).setTopUnits(Sizeable.Unit.CM); + layout.getPosition(b).setRightUnits(Sizeable.Unit.EX); + layout.getPosition(b).setBottomUnits(Sizeable.Unit.INCH); + layout.getPosition(b).setLeftUnits(Sizeable.Unit.MM); assertEquals(SIZE, layout.getPosition(b).getTopValue()); assertEquals(SIZE, layout.getPosition(b).getRightValue()); assertEquals(SIZE, layout.getPosition(b).getBottomValue()); assertEquals(SIZE, layout.getPosition(b).getLeftValue()); - assertEquals(Sizeable.UNITS_CM, layout.getPosition(b).getTopUnits()); - assertEquals(Sizeable.UNITS_EX, layout.getPosition(b).getRightUnits()); - assertEquals(Sizeable.UNITS_INCH, layout.getPosition(b) - .getBottomUnits()); - assertEquals(Sizeable.UNITS_MM, layout.getPosition(b).getLeftUnits()); + assertEquals(Sizeable.Unit.CM, layout.getPosition(b).getTopUnits()); + assertEquals(Sizeable.Unit.EX, layout.getPosition(b).getRightUnits()); + assertEquals(Sizeable.Unit.INCH, layout.getPosition(b).getBottomUnits()); + assertEquals(Sizeable.Unit.MM, layout.getPosition(b).getLeftUnits()); } @@ -159,21 +159,20 @@ public class ComponentPosition extends TestCase { Button b = new Button(); layout.addComponent(b); - layout.getPosition(b).setTop(SIZE, Sizeable.UNITS_CM); - layout.getPosition(b).setRight(SIZE, Sizeable.UNITS_EX); - layout.getPosition(b).setBottom(SIZE, Sizeable.UNITS_INCH); - layout.getPosition(b).setLeft(SIZE, Sizeable.UNITS_MM); + layout.getPosition(b).setTop(SIZE, Sizeable.Unit.CM); + layout.getPosition(b).setRight(SIZE, Sizeable.Unit.EX); + layout.getPosition(b).setBottom(SIZE, Sizeable.Unit.INCH); + layout.getPosition(b).setLeft(SIZE, Sizeable.Unit.MM); assertEquals(SIZE, layout.getPosition(b).getTopValue()); assertEquals(SIZE, layout.getPosition(b).getRightValue()); assertEquals(SIZE, layout.getPosition(b).getBottomValue()); assertEquals(SIZE, layout.getPosition(b).getLeftValue()); - assertEquals(Sizeable.UNITS_CM, layout.getPosition(b).getTopUnits()); - assertEquals(Sizeable.UNITS_EX, layout.getPosition(b).getRightUnits()); - assertEquals(Sizeable.UNITS_INCH, layout.getPosition(b) - .getBottomUnits()); - assertEquals(Sizeable.UNITS_MM, layout.getPosition(b).getLeftUnits()); + assertEquals(Sizeable.Unit.CM, layout.getPosition(b).getTopUnits()); + assertEquals(Sizeable.Unit.EX, layout.getPosition(b).getRightUnits()); + assertEquals(Sizeable.Unit.INCH, layout.getPosition(b).getBottomUnits()); + assertEquals(Sizeable.Unit.MM, layout.getPosition(b).getLeftUnits()); } diff --git a/tests/testbench/com/vaadin/tests/components/splitpanel/AbstractSplitPanelTest.java b/tests/testbench/com/vaadin/tests/components/splitpanel/AbstractSplitPanelTest.java index 0b702db33a..ba3c210006 100644 --- a/tests/testbench/com/vaadin/tests/components/splitpanel/AbstractSplitPanelTest.java +++ b/tests/testbench/com/vaadin/tests/components/splitpanel/AbstractSplitPanelTest.java @@ -1,6 +1,7 @@ package com.vaadin.tests.components.splitpanel; import com.vaadin.terminal.Sizeable; +import com.vaadin.terminal.Sizeable.Unit; import com.vaadin.tests.components.AbstractLayoutTest; import com.vaadin.ui.AbstractSplitPanel; import com.vaadin.ui.AbstractSplitPanel.SplitterClickEvent; @@ -55,7 +56,7 @@ public abstract class AbstractSplitPanelTest private boolean reverse = false; private int position; - private int unit; + private Unit unit; private String posString; public SplitPosition(String pos) { @@ -67,10 +68,10 @@ public abstract class AbstractSplitPanelTest if (pos.endsWith("px")) { position = Integer.parseInt(pos.substring(0, pos.length() - 2)); - unit = Sizeable.UNITS_PIXELS; + unit = Sizeable.Unit.PIXELS; } else if (pos.endsWith("%")) { position = Integer.parseInt(pos.substring(0, pos.length() - 1)); - unit = Sizeable.UNITS_PERCENTAGE; + unit = Sizeable.Unit.PERCENTAGE; } else { throw new RuntimeException("Could not parse " + pos); } diff --git a/tests/testbench/com/vaadin/tests/components/table/TablePageLengthUpdate.java b/tests/testbench/com/vaadin/tests/components/table/TablePageLengthUpdate.java index 71dc987e56..2ba1475cc8 100644 --- a/tests/testbench/com/vaadin/tests/components/table/TablePageLengthUpdate.java +++ b/tests/testbench/com/vaadin/tests/components/table/TablePageLengthUpdate.java @@ -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) { diff --git a/tests/testbench/com/vaadin/tests/components/window/WindowResizeListener.java b/tests/testbench/com/vaadin/tests/components/window/WindowResizeListener.java index 9526fd7aec..c6563f200f 100644 --- a/tests/testbench/com/vaadin/tests/components/window/WindowResizeListener.java +++ b/tests/testbench/com/vaadin/tests/components/window/WindowResizeListener.java @@ -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()); } } diff --git a/tests/testbench/com/vaadin/tests/layouts/TestAbsoluteLayout.java b/tests/testbench/com/vaadin/tests/layouts/TestAbsoluteLayout.java index 4f7269284d..8bdbc2b02e 100644 --- a/tests/testbench/com/vaadin/tests/layouts/TestAbsoluteLayout.java +++ b/tests/testbench/com/vaadin/tests/layouts/TestAbsoluteLayout.java @@ -47,10 +47,10 @@ public class TestAbsoluteLayout extends TestBase { s.addContainerProperty("caption", String.class, ""); s.setItemCaptionPropertyId("caption"); s.setNullSelectionAllowed(false); - for (int i = 0; i < Layout.UNIT_SYMBOLS.length; i++) { + for (int i = 0; i < Layout.Unit.values().length; i++) { Item unitItem = s.addItem(i); unitItem.getItemProperty("caption").setValue( - Layout.UNIT_SYMBOLS[i]); + Layout.Unit.values()[i]); } return s; } diff --git a/tests/testbench/com/vaadin/tests/tickets/Ticket1435.java b/tests/testbench/com/vaadin/tests/tickets/Ticket1435.java index 7c0b336a43..c280be4e4a 100644 --- a/tests/testbench/com/vaadin/tests/tickets/Ticket1435.java +++ b/tests/testbench/com/vaadin/tests/tickets/Ticket1435.java @@ -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); } diff --git a/tests/testbench/com/vaadin/tests/tickets/Ticket2090.java b/tests/testbench/com/vaadin/tests/tickets/Ticket2090.java index b0bc546065..b4171b9bac 100644 --- a/tests/testbench/com/vaadin/tests/tickets/Ticket2090.java +++ b/tests/testbench/com/vaadin/tests/tickets/Ticket2090.java @@ -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()); } } -- 2.39.5