diff options
author | Matti Tahvonen <matti.tahvonen@itmill.com> | 2011-10-11 10:49:49 +0000 |
---|---|---|
committer | Matti Tahvonen <matti.tahvonen@itmill.com> | 2011-10-11 10:49:49 +0000 |
commit | 4d32522972cde371f6ae8786b45df9dde68eef95 (patch) | |
tree | f39130e1b70673d8719323cdcd21d1a9f0188024 | |
parent | 626d8981096123dcb9dd9df5d5a68465279180ba (diff) | |
download | vaadin-framework-4d32522972cde371f6ae8786b45df9dde68eef95.tar.gz vaadin-framework-4d32522972cde371f6ae8786b45df9dde68eef95.zip |
test case related to #4394 and possibly to #7724
svn changeset:21677/svn branch:6.7
-rw-r--r-- | tests/testbench/com/vaadin/tests/components/textfield/TextFieldWithPropertyFormatter.java | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/tests/testbench/com/vaadin/tests/components/textfield/TextFieldWithPropertyFormatter.java b/tests/testbench/com/vaadin/tests/components/textfield/TextFieldWithPropertyFormatter.java new file mode 100644 index 0000000000..8c3bfd654c --- /dev/null +++ b/tests/testbench/com/vaadin/tests/components/textfield/TextFieldWithPropertyFormatter.java @@ -0,0 +1,125 @@ +package com.vaadin.tests.components.textfield; + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.util.Locale; + +import com.vaadin.data.Property; +import com.vaadin.data.util.PropertyFormatter; +import com.vaadin.tests.components.TestBase; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Button.ClickListener; +import com.vaadin.ui.TextField; + +@SuppressWarnings("unchecked") +public class TextFieldWithPropertyFormatter extends TestBase { + + private PropertyFormatter formatter; + private Property property; + + @Override + protected void setup() { + /* + * Formatter that: - formats in UK currency style - scales to 2 fraction + * digits - rounds half up + */ + // Property containing BigDecimal + property = new Property() { + private BigDecimal value; + + public Object getValue() { + return value; + } + + public void setValue(Object newValue) throws ReadOnlyException, + ConversionException { + if (newValue == null) { + value = null; + } else if (newValue instanceof BigDecimal) { + value = (BigDecimal) newValue; + } else { + throw new ConversionException(); + } + } + + public Class<?> getType() { + return BigDecimal.class; + } + + public boolean isReadOnly() { + return false; + } + + public void setReadOnly(boolean newStatus) { + // ignore + } + }; + + formatter = new PropertyFormatter(property) { + private final DecimalFormat df = new DecimalFormat("#,##0.00", + DecimalFormatSymbols.getInstance(Locale.UK)); + { + df.setParseBigDecimal(true); + df.setRoundingMode(RoundingMode.HALF_UP); + } + + @Override + public String format(Object value) { + final String retVal; + if (value == null) { + retVal = ""; + } else { + retVal = df.format(value); + } + return retVal; + } + + @Override + public Object parse(String formattedValue) throws Exception { + if (formattedValue != null + && formattedValue.trim().length() != 0) { + BigDecimal value = (BigDecimal) df.parse(formattedValue); + value = value.setScale(2, BigDecimal.ROUND_HALF_UP); + return value; + } + return null; + } + }; + + final TextField tf1 = new TextField(); + + tf1.setPropertyDataSource(formatter); + + addComponent(tf1); + + Button b = new Button( + "Sync (typing 12345.6789 and clicking this should format field)"); + b.addListener(new ClickListener() { + public void buttonClick(ClickEvent event) { + } + }); + addComponent(b); + b = new Button("Set '12345.6789' to textfield on the server side"); + b.addListener(new ClickListener() { + public void buttonClick(ClickEvent event) { + tf1.setValue("12345.6789"); + } + }); + addComponent(b); + + } + + @Override + protected String getDescription() { + return "Should work"; + } + + @Override + protected Integer getTicketNumber() { + return 4394; + } + +} |