Author: rwhitcomb
Date: Thu Jan 18 16:11:07 2018
New Revision: 1821520

URL: http://svn.apache.org/viewvc?rev=1821520&view=rev
Log:
PIVOT-1025: Fix the Decimal and Formatted Validators to allow a leading
"+" sign in the "isValid" and "parseNumber" methods.

Modified:
    pivot/trunk/tests/src/org/apache/pivot/tests/TextInputValidatorTest.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/DecimalValidator.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java

Modified: 
pivot/trunk/tests/src/org/apache/pivot/tests/TextInputValidatorTest.java
URL: 
http://svn.apache.org/viewvc/pivot/trunk/tests/src/org/apache/pivot/tests/TextInputValidatorTest.java?rev=1821520&r1=1821519&r2=1821520&view=diff
==============================================================================
--- pivot/trunk/tests/src/org/apache/pivot/tests/TextInputValidatorTest.java 
(original)
+++ pivot/trunk/tests/src/org/apache/pivot/tests/TextInputValidatorTest.java 
Thu Jan 18 16:11:07 2018
@@ -123,7 +123,7 @@ public class TextInputValidatorTest exte
         });
         invalidComparableRangeLabel = (Label) 
namespace.get("invalidComparableRangeLabel");
 
-        textinputDouble.setText("\u221E"); // infinite symbol
+        textinputDouble.setText("+\u221E"); // positive infinity text
         textinputDouble.setValidator(new DoubleValidator());
 
         // textinputFloat.setText("123456.789");

Modified: 
pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/DecimalValidator.java
URL: 
http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/DecimalValidator.java?rev=1821520&r1=1821519&r2=1821520&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/DecimalValidator.java 
(original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/DecimalValidator.java 
Thu Jan 18 16:11:07 2018
@@ -51,8 +51,15 @@ public class DecimalValidator extends Fo
     protected final Number parseNumber(final String text) {
         String textToParse;
         try {
+            // The default DecimalFormat doesn't support leading "+" sign,
+            // and there is no setting in DecimalFormatSymbols for a positive
+            // sign either, so just do it ourselves.
             // We have to upper case because of the exponent symbol
-            textToParse = text.toUpperCase(locale);
+            if (text.length() > 1 && text.charAt(0) == '+') {
+                textToParse = text.substring(1).toUpperCase(locale);
+            } else {
+                textToParse = text.toUpperCase(locale);
+            }
             return format.parse(textToParse);
         } catch (ParseException ex) {
             // this should never happen

Modified: 
pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java
URL: 
http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java?rev=1821520&r1=1821519&r2=1821520&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java 
(original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/validation/FormattedValidator.java 
Thu Jan 18 16:11:07 2018
@@ -48,17 +48,21 @@ public class FormattedValidator<F extend
 
     @Override
     public boolean isValid(final String text) {
-        String textToParse = text;
+        String textToParse;
         final ParsePosition pos = new ParsePosition(0);
         if (format instanceof NumberFormat) {
-            // We have to upper case because of the exponent symbol
-            textToParse = textToParse.toUpperCase(locale);
+            if (text.length() > 1 && text.charAt(0) == '+') {
+                textToParse = text.substring(1).toUpperCase(locale);
+            } else {
+                textToParse = text.toUpperCase(locale);
+            }
+        } else {
+            textToParse = text;
         }
         Object obj = format.parseObject(textToParse, pos);
 
         // The text is only valid if we successfully parsed ALL of it. Don't
-        // want trailing bits of
-        // not-valid text.
-        return obj != null && pos.getErrorIndex() == -1 && pos.getIndex() == 
text.length();
+        // want trailing bits of not-valid text.
+        return obj != null && pos.getErrorIndex() == -1 && pos.getIndex() == 
textToParse.length();
     }
 }


Reply via email to