Ok, here it it.

The CurrencyValidator is a little bit complicated, because if I'd use a plain NumberFormat, it would accept number input with trailing text, e.g. "30.30foo". Therefore I used parse(String,ParsePosition) which checks the parse position for trailing text explicitly.



I wrote:
Greg Brown <[email protected]> wrote:
Yeah, this is a known issue. That demo isn't fully localized. You are more than welcome to update it to support localized currency formats if you like.

I'll have a look at it.

Whoops. Fixed - thanks.

Whoa, fast! Thanks! :)

Dirk.






--
Dirk Möbius

SCOOP GmbH
Am Kielshof 29
D-51105 Köln
Fon   +49 221 801916-0
Fax   +49 221 801916-17
Mobil +49 170 7363035
www.scoop-gmbh.de
Sitz der Gesellschaft: Köln
Handelsregister: Köln
Handelsregisternummer: HRB 36623
Geschäftsführer:
Dr. Oleg Balovnev
Frank Heinen
Dr. Wolfgang Reddig
Roland Scheel
Index: demos/src/org/apache/pivot/demos/roweditor/CurrencyValidator.java
===================================================================
--- demos/src/org/apache/pivot/demos/roweditor/CurrencyValidator.java	(revision 951710)
+++ demos/src/org/apache/pivot/demos/roweditor/CurrencyValidator.java	Fri Dec 03 19:00:11 CET 2010
@@ -17,6 +17,8 @@
 package org.apache.pivot.demos.roweditor;
 
 import java.math.BigDecimal;
+import java.text.DecimalFormat;
+import java.text.ParsePosition;
 
 import org.apache.pivot.wtk.validation.Validator;
 
@@ -24,18 +26,24 @@
  * Validates that text represents a valid dollar value.
  */
 public class CurrencyValidator implements Validator {
+    public static final DecimalFormat FORMAT = new DecimalFormat("0.00");
+    static {
+        FORMAT.setParseBigDecimal(true);
+    }
+
     @Override
     public boolean isValid(String text) {
         boolean valid = true;
 
         if (text.length() > 0) {
-            try {
-                BigDecimal numericAmount = new BigDecimal(text);
-                valid = (numericAmount.scale() <= 2 && numericAmount.signum() >= 0);
-            } catch (NumberFormatException ex) {
-                valid = false;
+            ParsePosition parsePosition = new ParsePosition(0);
+            BigDecimal numericAmount = (BigDecimal) FORMAT.parse(text, parsePosition);
+            valid = (numericAmount != null &&
+                numericAmount.scale() <= 2 &&
+                numericAmount.signum() >= 0 &&
+                parsePosition.getErrorIndex() == -1 &&
+                parsePosition.getIndex() == text.length());
-            }
+        }
-        }
 
         return valid;
     }
Index: demos/src/org/apache/pivot/demos/roweditor/AmountBindMapping.java
===================================================================
--- demos/src/org/apache/pivot/demos/roweditor/AmountBindMapping.java	(revision 980482)
+++ demos/src/org/apache/pivot/demos/roweditor/AmountBindMapping.java	Fri Dec 03 19:05:01 CET 2010
@@ -17,6 +17,7 @@
 package org.apache.pivot.demos.roweditor;
 
 import java.text.DecimalFormat;
+import java.text.ParseException;
 
 import org.apache.pivot.wtk.TextInput;
 
@@ -30,6 +31,10 @@
 
     @Override
     public Object valueOf(String text) {
-        return Float.valueOf(text);
+        try {
+            return FORMAT.parse(text);
+        } catch (ParseException ex) {
+            throw new NumberFormatException(ex.getMessage());
-    }
-}
+        }
+    }
+}

Reply via email to