]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix Declarative support for BigDecimal properties (#17205)
authorJohannes Dahlström <johannesd@vaadin.com>
Wed, 18 Mar 2015 13:35:47 +0000 (15:35 +0200)
committerJohannes Dahlström <johannesd@vaadin.com>
Fri, 20 Mar 2015 09:32:06 +0000 (11:32 +0200)
Change-Id: I766172c125b7e771b4bc8e2db1f39d1ae828fea6

server/src/com/vaadin/ui/declarative/DesignFormatter.java
server/tests/src/com/vaadin/tests/design/DesignFormatterTest.java

index cc5071fe9dd9f1e88939982316a480bc4b4af7c5..728b3d1077a084a17cd96d4fdae29dd40dccf2a3 100644 (file)
@@ -29,6 +29,7 @@ import java.util.TimeZone;
 import java.util.concurrent.ConcurrentHashMap;
 
 import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.StringToBigDecimalConverter;
 import com.vaadin.data.util.converter.StringToDoubleConverter;
 import com.vaadin.data.util.converter.StringToFloatConverter;
 import com.vaadin.event.ShortcutAction;
@@ -68,8 +69,8 @@ public class DesignFormatter implements Serializable {
      */
     protected void mapDefaultTypes() {
         // numbers use standard toString/valueOf approach
-        for (Class<?> c : new Class<?>[] { Integer.class, Byte.class,
-                Short.class, Long.class, BigDecimal.class }) {
+        for (Class<?> c : new Class<?>[] { Byte.class, Short.class,
+                Integer.class, Long.class }) {
             DesignToStringConverter<?> conv = new DesignToStringConverter(c);
             converterMap.put(c, conv);
             try {
@@ -134,6 +135,16 @@ public class DesignFormatter implements Serializable {
         converterMap.put(Double.class, doubleConverter);
         converterMap.put(double.class, doubleConverter);
 
+        final DecimalFormat bigDecimalFmt = new DecimalFormat("0.###", symbols);
+        bigDecimalFmt.setGroupingUsed(false);
+        bigDecimalFmt.setParseBigDecimal(true);
+        converterMap.put(BigDecimal.class, new StringToBigDecimalConverter() {
+            @Override
+            protected NumberFormat getFormat(Locale locale) {
+                return bigDecimalFmt;
+            };
+        });
+
         // strings do nothing
         converterMap.put(String.class, new Converter<String, String>() {
 
index 1e6d44642738988150f3e78aa2d57c7b19dae9ea..05b2484767e416ffb7ac09caf120449d3f72add3 100644 (file)
@@ -18,6 +18,7 @@ package com.vaadin.tests.design;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 
+import java.math.BigDecimal;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.HashSet;
@@ -57,9 +58,10 @@ public class DesignFormatterTest {
                 byte.class, short.class, int.class, long.class, float.class,
                 double.class, Boolean.class, Character.class, Byte.class,
                 Short.class, Integer.class, Long.class, Float.class,
-                Double.class, String.class, ShortcutAction.class, Date.class,
-                FileResource.class, ExternalResource.class,
-                ThemeResource.class, Resource.class, TimeZone.class }) {
+                Double.class, BigDecimal.class, String.class,
+                ShortcutAction.class, Date.class, FileResource.class,
+                ExternalResource.class, ThemeResource.class, Resource.class,
+                TimeZone.class }) {
             assertTrue("not supported " + type.getSimpleName(),
                     formatter.canConvert(type));
         }
@@ -144,6 +146,14 @@ public class DesignFormatterTest {
 
     }
 
+    @Test
+    public void testBigDecimal() {
+        BigDecimal bd = new BigDecimal("123456789123456789.123456789123456789");
+        assertEquals("123456789123456789.123", formatter.format(bd));
+        assertEquals(bd, formatter.parse(
+                "123456789123456789.123456789123456789", BigDecimal.class));
+    }
+
     @Test
     public void testChar() {
         char c = '\uABCD';