summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoonas Lehtinen <joonas.lehtinen@itmill.com>2008-09-26 12:55:43 +0000
committerJoonas Lehtinen <joonas.lehtinen@itmill.com>2008-09-26 12:55:43 +0000
commite945f4b6554aa1aa353252109a3b843c89ad64ab (patch)
tree591fe57099e760fdaad98bbe60c6a5199cefbb6a
parentac564d08c08bf7b1399e9a38dc1679211cb8fb2c (diff)
downloadvaadin-framework-e945f4b6554aa1aa353252109a3b843c89ad64ab.tar.gz
vaadin-framework-e945f4b6554aa1aa353252109a3b843c89ad64ab.zip
Fixed #2090 and added test : width/height can be float
svn changeset:5527/svn branch:trunk
-rw-r--r--src/com/itmill/toolkit/terminal/Sizeable.java50
-rw-r--r--src/com/itmill/toolkit/tests/featurebrowser/FeatureBrowser.java8
-rw-r--r--src/com/itmill/toolkit/tests/tickets/Ticket1435.java2
-rw-r--r--src/com/itmill/toolkit/tests/tickets/Ticket2090.java62
-rw-r--r--src/com/itmill/toolkit/ui/AbstractComponent.java182
-rw-r--r--src/com/itmill/toolkit/ui/Panel.java9
-rw-r--r--src/com/itmill/toolkit/ui/Slider.java6
7 files changed, 197 insertions, 122 deletions
diff --git a/src/com/itmill/toolkit/terminal/Sizeable.java b/src/com/itmill/toolkit/terminal/Sizeable.java
index d11b041e83..e0ec15684f 100644
--- a/src/com/itmill/toolkit/terminal/Sizeable.java
+++ b/src/com/itmill/toolkit/terminal/Sizeable.java
@@ -68,7 +68,7 @@ public interface Sizeable {
*/
public static final int UNITS_ROWS = 9;
- public static final int SIZE_UNDEFINED = -1;
+ public static final float SIZE_UNDEFINED = -1;
/**
* Textual representations of units symbols. Supported units and their
@@ -96,7 +96,7 @@ public interface Sizeable {
*
* @return width of the object in units specified by widthUnits property.
*/
- public int getWidth();
+ public float getWidth();
/**
* Sets the width of the object. Negative number implies unspecified size
@@ -110,7 +110,7 @@ public interface Sizeable {
* separately (and components might have different default
* unit).
*/
- public void setWidth(int width);
+ public void setWidth(float width);
/**
* Gets the height of the object. Negative number implies unspecified size
@@ -118,7 +118,7 @@ public interface Sizeable {
*
* @return height of the object in units specified by heightUnits property.
*/
- public int getHeight();
+ public float getHeight();
/**
* Sets the height of the object. Negative number implies unspecified size
@@ -127,12 +127,12 @@ public interface Sizeable {
* @param height
* the height of the object in units specified by heightUnits
* property.
- * @deprecated Consider using {@link #setHeight(String)} instead. This
- * method works, but is error-prone since the unit must be set
- * separately (and components might have different default
- * unit).
+ * @deprecated Consider using {@link #setHeight(String)} or
+ * {@link #setHeight(float, int)} instead. This method works,
+ * but is error-prone since the unit must be set separately (and
+ * components might have different default unit).
*/
- public void setHeight(int height);
+ public void setHeight(float height);
/**
* Gets the width property units.
@@ -147,7 +147,8 @@ public interface Sizeable {
* @param units
* the units used in width property.
* @deprecated Consider setting width and unit simultaneously using
- * {@link #setWidth(String)}, which is less error-prone.
+ * {@link #setWidth(String)} or {@link #setWidth(float, int)},
+ * which is less error-prone.
*/
public void setWidthUnits(int units);
@@ -164,7 +165,8 @@ public interface Sizeable {
* @param units
* the units used in height property.
* @deprecated Consider setting height and unit simultaneously using
- * {@link #setHeight(String)}, which is less error-prone.
+ * {@link #setHeight(String)} or {@link #setHeight(float, int)},
+ * which is less error-prone.
*/
public void setHeightUnits(int units);
@@ -187,6 +189,32 @@ public interface Sizeable {
public void setHeight(String height);
/**
+ * Sets the width of the object. Negative number implies unspecified size
+ * (terminal is free to set the size).
+ *
+ * @param width
+ * the width of the object.
+ * @param unit
+ * the unit used for the width. Possible values include
+ * UNITS_PIXELS, UNITS_POINTS, UNITS_PICAS, UNITS_EM, UNITS_EX,
+ * UNITS_MM, UNITS_CM, UNITS_INCH, UNITS_PERCENTAGE, UNITS_ROWS.
+ */
+ public void setWidth(float width, int unit);
+
+ /**
+ * Sets the height of the object. Negative number implies unspecified size
+ * (terminal is free to set the size).
+ *
+ * @param height
+ * the height of the object.
+ * @param unit
+ * the unit used for the width. Possible values include
+ * UNITS_PIXELS, UNITS_POINTS, UNITS_PICAS, UNITS_EM, UNITS_EX,
+ * UNITS_MM, UNITS_CM, UNITS_INCH, UNITS_PERCENTAGE, UNITS_ROWS.
+ */
+ public void setHeight(float height, int unit);
+
+ /**
* Sets the width of the component using String presentation.
*
* String presentation is similar to what is used in Cascading Style Sheets.
diff --git a/src/com/itmill/toolkit/tests/featurebrowser/FeatureBrowser.java b/src/com/itmill/toolkit/tests/featurebrowser/FeatureBrowser.java
index 1aecfc6d45..6121b3f98b 100644
--- a/src/com/itmill/toolkit/tests/featurebrowser/FeatureBrowser.java
+++ b/src/com/itmill/toolkit/tests/featurebrowser/FeatureBrowser.java
@@ -280,7 +280,7 @@ public class FeatureBrowser extends CustomComponent implements
}
- public int getHeight() {
+ public float getHeight() {
// TODO Auto-generated method stub
return 0;
}
@@ -290,7 +290,7 @@ public class FeatureBrowser extends CustomComponent implements
return 0;
}
- public int getWidth() {
+ public float getWidth() {
// TODO Auto-generated method stub
return 0;
}
@@ -300,7 +300,7 @@ public class FeatureBrowser extends CustomComponent implements
return 0;
}
- public void setHeight(int height) {
+ public void setHeight(float height) {
// TODO Auto-generated method stub
}
@@ -320,7 +320,7 @@ public class FeatureBrowser extends CustomComponent implements
}
- public void setWidth(int width) {
+ public void setWidth(float width) {
// TODO Auto-generated method stub
}
diff --git a/src/com/itmill/toolkit/tests/tickets/Ticket1435.java b/src/com/itmill/toolkit/tests/tickets/Ticket1435.java
index ee4fb9930b..65d06db678 100644
--- a/src/com/itmill/toolkit/tests/tickets/Ticket1435.java
+++ b/src/com/itmill/toolkit/tests/tickets/Ticket1435.java
@@ -52,7 +52,7 @@ public class Ticket1435 extends Application {
Panel container = new Panel();
// Last known height before the panel was collapsed
- private int lastHeight = -1;
+ private float lastHeight = -1;
private int lastHeightUnit = -1;
public ButtonPanel(String labelString) {
diff --git a/src/com/itmill/toolkit/tests/tickets/Ticket2090.java b/src/com/itmill/toolkit/tests/tickets/Ticket2090.java
new file mode 100644
index 0000000000..da85e4adfb
--- /dev/null
+++ b/src/com/itmill/toolkit/tests/tickets/Ticket2090.java
@@ -0,0 +1,62 @@
+package com.itmill.toolkit.tests.tickets;
+
+import com.itmill.toolkit.Application;
+import com.itmill.toolkit.data.Property;
+import com.itmill.toolkit.data.Property.ValueChangeEvent;
+import com.itmill.toolkit.terminal.Sizeable;
+import com.itmill.toolkit.terminal.UserError;
+import com.itmill.toolkit.ui.Button;
+import com.itmill.toolkit.ui.Label;
+import com.itmill.toolkit.ui.TextField;
+import com.itmill.toolkit.ui.Window;
+
+public class Ticket2090 extends Application {
+
+ Label label = new Label();
+ Button target = new Button();
+ Window w = new Window("#2090");
+
+ @Override
+ public void init() {
+ setMainWindow(w);
+ final TextField width = new TextField("Width");
+ width.setImmediate(true);
+ final TextField height = new TextField("Height");
+ height.setImmediate(true);
+ w.addComponent(width);
+ w.addComponent(height);
+ w.addComponent(label);
+ w.addComponent(target);
+ height.addListener(new Property.ValueChangeListener() {
+ public void valueChange(ValueChangeEvent event) {
+ try {
+ target.setHeight(height.toString());
+ height.setComponentError(null);
+ updateLabel();
+ } catch (Exception e) {
+ height.setComponentError(new UserError(e.getMessage()));
+ }
+ }
+ });
+ width.addListener(new Property.ValueChangeListener() {
+ public void valueChange(ValueChangeEvent event) {
+ try {
+ target.setWidth(width.toString());
+ width.setComponentError(null);
+ updateLabel();
+ } catch (Exception e) {
+ width.setComponentError(new UserError(e.getMessage()));
+ }
+ }
+ });
+
+ }
+
+ private void updateLabel() {
+ label.setValue("width: " + target.getWidth()
+ + Sizeable.UNIT_SYMBOLS[target.getWidthUnits()] + ", height: "
+ + target.getHeight()
+ + Sizeable.UNIT_SYMBOLS[target.getHeightUnits()]);
+ }
+
+}
diff --git a/src/com/itmill/toolkit/ui/AbstractComponent.java b/src/com/itmill/toolkit/ui/AbstractComponent.java
index a4408055e4..b4a33468b3 100644
--- a/src/com/itmill/toolkit/ui/AbstractComponent.java
+++ b/src/com/itmill/toolkit/ui/AbstractComponent.java
@@ -21,7 +21,6 @@ import com.itmill.toolkit.terminal.ErrorMessage;
import com.itmill.toolkit.terminal.PaintException;
import com.itmill.toolkit.terminal.PaintTarget;
import com.itmill.toolkit.terminal.Resource;
-import com.itmill.toolkit.terminal.Sizeable;
import com.itmill.toolkit.terminal.Terminal;
/**
@@ -120,12 +119,12 @@ public abstract class AbstractComponent implements Component, MethodEventSource
/* Sizeable fields */
- private int width = SIZE_UNDEFINED;
- private int height = SIZE_UNDEFINED;
+ private float width = SIZE_UNDEFINED;
+ private float height = SIZE_UNDEFINED;
private int widthUnit = UNITS_PIXELS;
private int heightUnit = UNITS_PIXELS;
private static final Pattern sizePattern = Pattern
- .compile("^(\\d+)(%|px|em|ex|in|cm|mm|pt|pc)$");
+ .compile("^(-?\\d+(\\.\\d+)?)(%|px|em|ex|in|cm|mm|pt|pc)?$");
private ComponentErrorHandler errorHandler = null;
@@ -1008,77 +1007,59 @@ public abstract class AbstractComponent implements Component, MethodEventSource
/* Sizeable and other size related methods */
- /*
- * (non-Javadoc)
- *
- * @see com.itmill.toolkit.terminal.Sizeable#getHeight()
- */
- public int getHeight() {
+ public float getHeight() {
return height;
}
- /*
- * (non-Javadoc)
- *
- * @see com.itmill.toolkit.terminal.Sizeable#getHeightUnits()
- */
public int getHeightUnits() {
return heightUnit;
}
- /*
- * (non-Javadoc)
- *
- * @see com.itmill.toolkit.terminal.Sizeable#getWidth()
- */
- public int getWidth() {
+ public float getWidth() {
return width;
}
- /*
- * (non-Javadoc)
- *
- * @see com.itmill.toolkit.terminal.Sizeable#getWidthUnits()
- */
public int getWidthUnits() {
return widthUnit;
}
- /**
- * Sets the height without setting the height unit.
- *
- * @see link {@link Sizeable#setHeight(int)}
- * @see link {@link Sizeable#setHeight(String)}
- * @see link {@link Sizeable#setHeightUnits(int)}
- * @deprecated Error-prone; consider using {link {@link #setHeight(String)}
- * or {link {@link #setHeight(int, int)} instead.
- */
-
- public void setHeight(int height) {
+ public void setHeight(float height) {
this.height = height;
requestRepaint();
}
- /*
- * (non-Javadoc)
+ /**
+ * Sets the height property units.
*
- * @see com.itmill.toolkit.terminal.Sizeable#setHeightUnits(int)
+ * @param units
+ * the units used in height property.
+ * @deprecated Consider setting height and unit simultaneously using
+ * {@link #setHeight(String)} or {@link #setHeight(float, int)},
+ * which is less error-prone.
*/
public void setHeightUnits(int unit) {
heightUnit = unit;
requestRepaint();
}
- public void setHeight(int height, int unit) {
- setHeight(height);
- setHeightUnits(unit);
+ /**
+ * Sets the height of the object. Negative number implies unspecified size
+ * (terminal is free to set the size).
+ *
+ * @param height
+ * the height of the object in units specified by heightUnits
+ * property.
+ * @deprecated Consider using {@link #setHeight(String)} or
+ * {@link #setHeight(float, int)} instead. This method works,
+ * but is error-prone since the unit must be set separately (and
+ * components might have different default unit).
+ */
+ public void setHeight(float height, int unit) {
+ this.height = height;
+ heightUnit = unit;
+ requestRepaint();
}
- /*
- * (non-Javadoc)
- *
- * @see com.itmill.toolkit.terminal.Sizeable#setSizeFull()
- */
public void setSizeFull() {
height = 100;
width = 100;
@@ -1087,11 +1068,6 @@ public abstract class AbstractComponent implements Component, MethodEventSource
requestRepaint();
}
- /*
- * (non-Javadoc)
- *
- * @see com.itmill.toolkit.terminal.Sizeable#setSizeUndefined()
- */
public void setSizeUndefined() {
height = -1;
width = -1;
@@ -1101,52 +1077,62 @@ public abstract class AbstractComponent implements Component, MethodEventSource
}
/**
- * Sets the width without setting the width unit.
+ * Sets the width of the object. Negative number implies unspecified size
+ * (terminal is free to set the size).
*
- * @see link {@link Sizeable#setWidth(int)}
- * @see link {@link Sizeable#setWidth(String)}
- * @see link {@link Sizeable#setWidthUnits(int)}
- * @deprecated Error-prone; consider using {link {@link #setWidth(String)}
- * or {link {@link #setWidth(int, int)} instead.
- */
- public void setWidth(int width) {
+ * @param width
+ * the width of the object in units specified by widthUnits
+ * property.
+ * @deprecated Consider using {@link #setWidth(String)} instead. This method
+ * works, but is error-prone since the unit must be set
+ * separately (and components might have different default
+ * unit).
+ */
+ public void setWidth(float width) {
this.width = width;
requestRepaint();
}
- /*
- * (non-Javadoc)
+ /**
+ * Sets the width property units.
*
- * @see com.itmill.toolkit.terminal.Sizeable#setWidthUnits(int)
+ * @param units
+ * the units used in width property.
+ * @deprecated Consider setting width and unit simultaneously using
+ * {@link #setWidth(String)} or {@link #setWidth(float, int)},
+ * which is less error-prone.
*/
public void setWidthUnits(int unit) {
widthUnit = unit;
requestRepaint();
}
- public void setWidth(int width, int unit) {
- setWidth(width);
- setWidthUnits(unit);
+ public void setWidth(float width, int unit) {
+ this.width = width;
+ widthUnit = unit;
+ requestRepaint();
}
public void setWidth(String width) {
- int[] p = parseStringSize(width);
- setWidth(p[0]);
- setWidthUnits(p[1]);
+ float[] p = parseStringSize(width);
+ this.width = p[0];
+ widthUnit = (int) p[1];
+ requestRepaint();
}
public void setHeight(String height) {
- int[] p = parseStringSize(height);
- setHeight(p[0]);
- setHeightUnits(p[1]);
+ float[] p = parseStringSize(height);
+ this.height = p[0];
+ heightUnit = (int) p[1];
+ requestRepaint();
}
/*
* Returns array with size in index 0 unit in index 1. Null or empty string
* will produce {-1,UNITS_PIXELS}
*/
- private static int[] parseStringSize(String s) {
- int[] values = { -1, UNITS_PIXELS };
+ private static float[] parseStringSize(String s) {
+ float[] values = { -1, UNITS_PIXELS };
if (s == null) {
return values;
}
@@ -1157,26 +1143,32 @@ public abstract class AbstractComponent implements Component, MethodEventSource
Matcher matcher = sizePattern.matcher(s);
if (matcher.find()) {
- values[0] = Integer.parseInt(matcher.group(1));
- String unit = matcher.group(2);
- if (unit.equals("%")) {
- values[1] = UNITS_PERCENTAGE;
- } else if (unit.equals("px")) {
- values[1] = UNITS_PIXELS;
- } 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;
+ values[0] = Float.parseFloat(matcher.group(1));
+ if (values[0] < 0) {
+ values[0] = -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;
+ }
}
} else {
throw new IllegalArgumentException("Invalid size argument: \"" + s
diff --git a/src/com/itmill/toolkit/ui/Panel.java b/src/com/itmill/toolkit/ui/Panel.java
index 8a37fb6a83..42ff6e42ae 100644
--- a/src/com/itmill/toolkit/ui/Panel.java
+++ b/src/com/itmill/toolkit/ui/Panel.java
@@ -286,15 +286,10 @@ public class Panel extends AbstractComponentContainer implements Scrollable,
final Integer newWidth = (Integer) variables.get("width");
final Integer newHeight = (Integer) variables.get("height");
if (newWidth != null && newWidth.intValue() != getWidth()) {
- setWidth(newWidth.intValue());
- // ensure units, as we are reading pixels
- setWidthUnits(UNITS_PIXELS);
-
+ setWidth(newWidth.intValue(), UNITS_PIXELS);
}
if (newHeight != null && newHeight.intValue() != getHeight()) {
- setHeight(newHeight.intValue());
- // ensure units, as we are reading pixels
- setHeightUnits(UNITS_PIXELS);
+ setHeight(newHeight.intValue(), UNITS_PIXELS);
}
// Scrolling
diff --git a/src/com/itmill/toolkit/ui/Slider.java b/src/com/itmill/toolkit/ui/Slider.java
index 854430e852..5123232097 100644
--- a/src/com/itmill/toolkit/ui/Slider.java
+++ b/src/com/itmill/toolkit/ui/Slider.java
@@ -341,12 +341,10 @@ public class Slider extends AbstractField {
this.size = size;
switch (orientation) {
case ORIENTATION_HORIZONTAL:
- setWidth(size);
- setWidthUnits(UNITS_PIXELS);
+ setWidth(size, UNITS_PIXELS);
break;
default:
- setHeight(size);
- setHeightUnits(UNITS_PIXELS);
+ setHeight(size, UNITS_PIXELS);
break;
}
requestRepaint();