--- /dev/null
+package com.vaadin.tests.converter;\r
+\r
+import com.vaadin.data.Property.ValueChangeEvent;\r
+import com.vaadin.data.Property.ValueChangeListener;\r
+import com.vaadin.tests.components.TestBase;\r
+import com.vaadin.tests.util.Log;\r
+import com.vaadin.ui.TextField;\r
+\r
+public class ConverterThatEnforcesAFormat extends TestBase {\r
+\r
+ private Log log = new Log(5);\r
+\r
+ @Override\r
+ protected void setup() {\r
+ final TextField tf = new TextField(\r
+ "This field should always be formatted with 3 digits");\r
+ tf.setConverter(new StringToDoubleConverterWithThreeFractionDigits());\r
+ tf.addListener(new ValueChangeListener() {\r
+ public void valueChange(ValueChangeEvent event) {\r
+ log.log("Value changed to "\r
+ + event.getProperty().getValue()\r
+ + "(converted value is "\r
+ + tf.getConvertedValue()\r
+ + "). Two-way conversion gives: "\r
+ + tf.getConverter().convertToPresentation(\r
+ tf.getConverter().convertToModel(tf.getValue(),\r
+ tf.getLocale()), tf.getLocale()) + ")");\r
+ }\r
+ });\r
+ tf.setImmediate(true);\r
+ addComponent(log);\r
+ addComponent(tf);\r
+ tf.setConvertedValue(50.0);\r
+ }\r
+\r
+ @Override\r
+ protected String getDescription() {\r
+ return "Entering a valid double in the field should always cause the field contents to be formatted to contain 3 digits after the decimal point";\r
+ }\r
+\r
+ @Override\r
+ protected Integer getTicketNumber() {\r
+ return 8191;\r
+ }\r
+\r
+}\r
--- /dev/null
+package com.vaadin.tests.converter;\r
+\r
+import java.text.NumberFormat;\r
+import java.util.Locale;\r
+\r
+import com.vaadin.data.util.converter.StringToDoubleConverter;\r
+\r
+public class StringToDoubleConverterWithThreeFractionDigits extends StringToDoubleConverter {\r
+\r
+ @Override\r
+ protected NumberFormat getFormat(Locale locale) {\r
+ NumberFormat format = super.getFormat(locale);\r
+ format.setGroupingUsed(false);\r
+ format.setMaximumFractionDigits(3);\r
+ format.setMinimumFractionDigits(3);\r
+ return format;\r
+ }\r
+}\r