You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2013/02/11 19:20:44 UTC

svn commit: r1444912 - in /pivot/branches/2.0.x: ./ tests/src/org/apache/pivot/tests/ wtk/src/org/apache/pivot/wtk/validation/

Author: rwhitcomb
Date: Mon Feb 11 18:20:43 2013
New Revision: 1444912

URL: http://svn.apache.org/r1444912
Log:
PIVOT-892 (more): The real problem with validating scientific notation was the UPPER/lower case
of the exponent character.  So, moved the Locale into FormattedValidator and doing the Upper case
conversion in there for NumberFormats and also in DecimalValidator.  This means Double and Float
Validators can go back to being simple classes.

Also checking the Float ranges better, and allow Infinity and NaN values in there.

Update the TextInputValidatorTest program to allow basic float / double validation as well.

This is a merge of revision 1444910 from trunk to branches/2.0.x.

Modified:
    pivot/branches/2.0.x/   (props changed)
    pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/TextInputValidatorTest.java
    pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/text_input_validator_test.bxml
    pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/DecimalValidator.java
    pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java
    pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java
    pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java

Propchange: pivot/branches/2.0.x/
------------------------------------------------------------------------------
  Merged /pivot/trunk:r1444910

Modified: pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/TextInputValidatorTest.java
URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/TextInputValidatorTest.java?rev=1444912&r1=1444911&r2=1444912&view=diff
==============================================================================
--- pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/TextInputValidatorTest.java (original)
+++ pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/TextInputValidatorTest.java Mon Feb 11 18:20:43 2013
@@ -23,6 +23,8 @@ import org.apache.pivot.wtk.Label;
 import org.apache.pivot.wtk.TextInput;
 import org.apache.pivot.wtk.TextInputListener;
 import org.apache.pivot.wtk.Window;
+import org.apache.pivot.wtk.validation.DoubleValidator;
+import org.apache.pivot.wtk.validation.FloatValidator;
 import org.apache.pivot.wtk.validation.FloatRangeValidator;
 import org.apache.pivot.wtk.validation.IntRangeValidator;
 import org.apache.pivot.wtk.validation.NotEmptyTextValidator;
@@ -34,6 +36,8 @@ import org.apache.pivot.wtk.validation.V
  */
 public class TextInputValidatorTest  extends Application.Adapter {
     private Window window = null;
+    private TextInput textinputDouble = null;
+    private TextInput textinputFloat = null;
     private TextInput textinputFloatRange = null;
     private Label invalidLabel = null;
     private TextInput textinputIntRange = null;
@@ -47,12 +51,20 @@ public class TextInputValidatorTest  ext
         window = new Window((Component)bxmlSerializer.readObject(
             getClass().getResource("text_input_validator_test.bxml")));
 
+        textinputDouble = (TextInput)bxmlSerializer.getNamespace().get("textinputDouble");
+        textinputFloat = (TextInput)bxmlSerializer.getNamespace().get("textinputFloat");
         textinputFloatRange = (TextInput)bxmlSerializer.getNamespace().get("textinputFloatRange");
         textinputIntRange = (TextInput)bxmlSerializer.getNamespace().get("textinputIntRange");
         textinputDateRegex = (TextInput)bxmlSerializer.getNamespace().get("textinputDateRegex");
         textinputCustomBoolean = (TextInput)bxmlSerializer.getNamespace().get("textinputCustomBoolean");
         textinputNotEmptyText = (TextInput)bxmlSerializer.getNamespace().get("textinputNotEmptyText");
 
+        textinputDouble.setText("\u221E");
+        textinputDouble.setValidator(new DoubleValidator());
+
+        textinputFloat.setText("1.5");
+        textinputFloat.setValidator(new FloatValidator());
+
         // standard float range model
         textinputFloatRange.setText("0.5");
         textinputFloatRange.setValidator(new FloatRangeValidator(0.3f, 2000f));

Modified: pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/text_input_validator_test.bxml
URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/text_input_validator_test.bxml?rev=1444912&r1=1444911&r2=1444912&view=diff
==============================================================================
--- pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/text_input_validator_test.bxml (original)
+++ pivot/branches/2.0.x/tests/src/org/apache/pivot/tests/text_input_validator_test.bxml Mon Feb 11 18:20:43 2013
@@ -24,13 +24,23 @@ limitations under the License.
     </columns>
 
     <TablePane.Row height="-1">
+        <Label text="double"/>
+        <TextInput bxml:id="textinputDouble"/>
+        <Label text="valid value: any double: ~4.94e-324 to ~1.79e+308 or &#x00B1;&#x221E;"/>
+    </TablePane.Row>
+    <TablePane.Row height="-1">
         <Label text="float"/>
+        <TextInput bxml:id="textinputFloat"/>
+        <Label text="valid value: any float: ~1.40e-45 to ~3.40e+38 or &#x00B1;&#x221E;"/>
+    </TablePane.Row>
+    <TablePane.Row height="-1">
+        <Label text="float range"/>
         <TextInput bxml:id="textinputFloatRange"/>
         <Label text="valid range: 0.3 .. 2000"/>
         <Label bxml:id="invalidLabel"/>
     </TablePane.Row>
     <TablePane.Row height="-1">
-        <Label text="int"/>
+        <Label text="int range"/>
         <TextInput bxml:id="textinputIntRange"/>
         <Label text="valid range: 0 .. 100"/>
     </TablePane.Row>

Modified: pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/DecimalValidator.java
URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/DecimalValidator.java?rev=1444912&r1=1444911&r2=1444912&view=diff
==============================================================================
--- pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/DecimalValidator.java (original)
+++ pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/DecimalValidator.java Mon Feb 11 18:20:43 2013
@@ -27,17 +27,23 @@ public class DecimalValidator extends Fo
         super(format);
     }
 
+    public DecimalValidator(DecimalFormat format, Locale locale) {
+        super(format, locale);
+    }
+
     public DecimalValidator() {
         super(NumberFormat.getInstance());
     }
 
     public DecimalValidator(Locale locale) {
-        super(NumberFormat.getInstance(locale));
+        super(NumberFormat.getInstance(locale), locale);
     }
 
     /** helper method that wraps the ParseException in a RuntimeException. */
     protected final Number parseNumber(String text) {
         try {
+            // We have to upper case because of the exponent symbol
+            text = (locale == null) ? text.toUpperCase() : text.toUpperCase(locale);
             return format.parse(text);
         } catch (ParseException ex) {
             // this should never happen

Modified: pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java
URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java?rev=1444912&r1=1444911&r2=1444912&view=diff
==============================================================================
--- pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java (original)
+++ pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/DoubleValidator.java Mon Feb 11 18:20:43 2013
@@ -21,22 +21,13 @@ import java.util.Locale;
  * A validator for a double value.
  */
 public class DoubleValidator extends DecimalValidator {
-    private Locale locale = null;
 
     public DoubleValidator() {
-        super(new DecimalFormat("0E0"));
+        super();
     }
 
     public DoubleValidator(Locale locale) {
-        this();
-        this.locale = locale;
-        ((DecimalFormat)format).setDecimalFormatSymbols(new DecimalFormatSymbols(locale));
-    }
-
-    @Override
-    public boolean isValid(String text) {
-        // We have to upper case because of the exponent symbol
-        return super.isValid(locale == null ? text.toUpperCase() : text.toUpperCase(locale));
+        super(locale);
     }
 
 }

Modified: pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java
URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java?rev=1444912&r1=1444911&r2=1444912&view=diff
==============================================================================
--- pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java (original)
+++ pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FloatValidator.java Mon Feb 11 18:20:43 2013
@@ -21,22 +21,18 @@ import java.util.Locale;
  * A validator for a float value.
  */
 public class FloatValidator extends DecimalValidator {
-    private Locale locale = null;
 
     public FloatValidator() {
-        super(new DecimalFormat("0E0"));
+        super();
     }
 
     public FloatValidator(Locale locale) {
-        this();
-        this.locale = locale;
-        ((DecimalFormat)format).setDecimalFormatSymbols(new DecimalFormatSymbols(locale));
+        super(locale);
     }
 
     @Override
     public boolean isValid(String text) {
-        // We have to upper case because of the exponent symbol
-        if (!super.isValid(locale == null ? text.toUpperCase() : text.toUpperCase(locale)))
+        if (!super.isValid(text))
             return false;
 
         /*
@@ -44,10 +40,14 @@ public class FloatValidator extends Deci
          * resulting number is withing range for a float.
          */
         Number number = parseNumber(text);
-        if (number.doubleValue() > Float.MAX_VALUE) {
-            return false;
+        double doubleValue = number.doubleValue();
+        if (doubleValue == Double.POSITIVE_INFINITY ||
+            doubleValue == Double.NEGATIVE_INFINITY ||
+            doubleValue == Double.NaN) {
+            return true;
         }
-        if (number.doubleValue() < -Float.MAX_VALUE) {
+        doubleValue = Math.abs(doubleValue);
+        if (doubleValue > Float.MAX_VALUE || doubleValue < Float.MIN_VALUE) {
             return false;
         }
         return true;

Modified: pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java
URL: http://svn.apache.org/viewvc/pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java?rev=1444912&r1=1444911&r2=1444912&view=diff
==============================================================================
--- pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java (original)
+++ pivot/branches/2.0.x/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java Mon Feb 11 18:20:43 2013
@@ -14,7 +14,9 @@
 package org.apache.pivot.wtk.validation;
 
 import java.text.Format;
+import java.text.NumberFormat;
 import java.text.ParsePosition;
+import java.util.Locale;
 
 /**
  * A validator for a {@link java.text.Format}'ed value.
@@ -26,14 +28,25 @@ import java.text.ParsePosition;
  */
 public class FormattedValidator<F extends Format> implements Validator {
     protected final F format;
+    protected final Locale locale;
 
     public FormattedValidator(F format) {
         this.format = format;
+        this.locale = null;
+    }
+
+    public FormattedValidator(F format, Locale locale) {
+        this.format = format;
+        this.locale = locale;
     }
 
     @Override
     public boolean isValid(String text) {
         final ParsePosition pos = new ParsePosition(0);
+        if (format instanceof NumberFormat) {
+            // We have to upper case because of the exponent symbol
+            text = (locale == null) ? text.toUpperCase() : text.toUpperCase(locale);
+        }
         Object obj = format.parseObject(text, pos);
 
         // The text is only valid if we successfully parsed ALL of it. Don't want trailing bits of