From b84d2feabf34a972b42c82ead780768883591378 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 18 Jun 2013 22:15:54 +0300 Subject: [PATCH] Adds String <-> BigDecimal converter (#9997) * BigDecimal properties connected to String based fields (TextField/TextArea/...) are now supported without custom converters Change-Id: I2dab67875b3cfdb46b17e9d4cc35ffb94c114478 --- .../converter/DefaultConverterFactory.java | 3 + .../StringToBigDecimalConverter.java | 60 +++++++++ .../TestStringToBigDecimalConverter.java | 53 ++++++++ .../textfield/BigDecimalTextField.html | 123 ++++++++++++++++++ .../textfield/BigDecimalTextField.java | 123 ++++++++++++++++++ 5 files changed, 362 insertions(+) create mode 100644 server/src/com/vaadin/data/util/converter/StringToBigDecimalConverter.java create mode 100644 server/tests/src/com/vaadin/tests/data/converter/TestStringToBigDecimalConverter.java create mode 100644 uitest/src/com/vaadin/tests/components/textfield/BigDecimalTextField.html create mode 100644 uitest/src/com/vaadin/tests/components/textfield/BigDecimalTextField.java diff --git a/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java b/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java index 3c365ecdff..0b3cfcd1b0 100644 --- a/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java +++ b/server/src/com/vaadin/data/util/converter/DefaultConverterFactory.java @@ -16,6 +16,7 @@ package com.vaadin.data.util.converter; +import java.math.BigDecimal; import java.util.Date; import java.util.logging.Logger; @@ -101,6 +102,8 @@ public class DefaultConverterFactory implements ConverterFactory { return new StringToFloatConverter(); } else if (Integer.class.isAssignableFrom(sourceType)) { return new StringToIntegerConverter(); + } else if (BigDecimal.class.isAssignableFrom(sourceType)) { + return new StringToBigDecimalConverter(); } else if (Boolean.class.isAssignableFrom(sourceType)) { return new StringToBooleanConverter(); } else if (Date.class.isAssignableFrom(sourceType)) { diff --git a/server/src/com/vaadin/data/util/converter/StringToBigDecimalConverter.java b/server/src/com/vaadin/data/util/converter/StringToBigDecimalConverter.java new file mode 100644 index 0000000000..75d4cedd23 --- /dev/null +++ b/server/src/com/vaadin/data/util/converter/StringToBigDecimalConverter.java @@ -0,0 +1,60 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.data.util.converter; + +import java.math.BigDecimal; +import java.text.DecimalFormat; +import java.text.NumberFormat; +import java.util.Locale; + +/** + * A converter that converts from {@link String} to {@link BigDecimal} and back. + * Uses the given locale and a {@link NumberFormat} instance for formatting and + * parsing. + *

+ * Leading and trailing white spaces are ignored when converting from a String. + *

+ *

+ * Override and overwrite {@link #getFormat(Locale)} to use a different format. + *

+ * + * @author Vaadin Ltd + * @since 7.2 + */ +public class StringToBigDecimalConverter extends + AbstractStringToNumberConverter { + @Override + protected NumberFormat getFormat(Locale locale) { + NumberFormat numberFormat = super.getFormat(locale); + if (numberFormat instanceof DecimalFormat) { + ((DecimalFormat) numberFormat).setParseBigDecimal(true); + } + + return numberFormat; + } + + @Override + public BigDecimal convertToModel(String value, + Class targetType, Locale locale) + throws com.vaadin.data.util.converter.Converter.ConversionException { + return (BigDecimal) convertToNumber(value, BigDecimal.class, locale); + } + + @Override + public Class getModelType() { + return BigDecimal.class; + } +} diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToBigDecimalConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToBigDecimalConverter.java new file mode 100644 index 0000000000..5db33691b6 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToBigDecimalConverter.java @@ -0,0 +1,53 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.data.converter; + +import java.math.BigDecimal; +import java.util.Locale; + +import junit.framework.TestCase; + +import com.vaadin.data.util.converter.StringToBigDecimalConverter; + +public class TestStringToBigDecimalConverter extends TestCase { + + StringToBigDecimalConverter converter = new StringToBigDecimalConverter(); + + public void testNullConversion() { + assertEquals(null, + converter.convertToModel(null, BigDecimal.class, null)); + } + + public void testEmptyStringConversion() { + assertEquals(null, converter.convertToModel("", BigDecimal.class, null)); + } + + public void testValueParsing() { + BigDecimal converted = converter.convertToModel("10", BigDecimal.class, + null); + BigDecimal expected = new BigDecimal(10); + assertEquals(expected, converted); + } + + public void testValueFormatting() { + BigDecimal bd = new BigDecimal(12.5); + String expected = "12,5"; + + String converted = converter.convertToPresentation(bd, String.class, + Locale.GERMAN); + assertEquals(expected, converted); + } +} diff --git a/uitest/src/com/vaadin/tests/components/textfield/BigDecimalTextField.html b/uitest/src/com/vaadin/tests/components/textfield/BigDecimalTextField.html new file mode 100644 index 0000000000..2428e8b4bb --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/textfield/BigDecimalTextField.html @@ -0,0 +1,123 @@ + + + + + + +New Test + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
New Test
open/run/com.vaadin.tests.components.textfield.BigDecimalTextField?restartApplication
clickvaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::/VVerticalLayout[0]/Slot[2]/VButton[0]/domChild[0]/domChild[0]
assertValuevaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::/VVerticalLayout[0]/Slot[1]/VFormLayout[0]/VFormLayout$VFormLayoutTable[0]/VTextField[0]15,2
clickvaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::/VVerticalLayout[0]/Slot[3]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::PID_SLog_row_01. Commit ok. Property value: 15.2
mouseClickvaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::/VVerticalLayout[0]/Slot[1]/VFormLayout[0]/VFormLayout$VFormLayoutTable[0]/VTextField[0]70,12
typevaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::/VVerticalLayout[0]/Slot[1]/VFormLayout[0]/VFormLayout$VFormLayoutTable[0]/VTextField[0]130
mouseClickvaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::160,327
clickvaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::/VVerticalLayout[0]/Slot[3]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::PID_SLog_row_02. Commit ok. Property value: 130
mouseClickvaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::/VVerticalLayout[0]/Slot[1]/VFormLayout[0]/VFormLayout$VFormLayoutTable[0]/VTextField[0]75,16
typevaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::/VVerticalLayout[0]/Slot[1]/VFormLayout[0]/VFormLayout$VFormLayoutTable[0]/VTextField[0]130abc
mouseClickvaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::96,280
clickvaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::/VVerticalLayout[0]/Slot[3]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::PID_SLog_row_03. Commit failed: Commit failed
assertCSSClassvaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::/VVerticalLayout[0]/Slot[1]/VFormLayout[0]/domChild[0]/domChild[1]/domChild[0]/domChild[1]/domChild[0]/domChild[0]v-errorindicator
mouseClickvaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::/VVerticalLayout[0]/Slot[1]/VFormLayout[0]/VFormLayout$VFormLayoutTable[0]/VTextField[0]71,12
typevaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::/VVerticalLayout[0]/Slot[1]/VFormLayout[0]/VFormLayout$VFormLayoutTable[0]/VTextField[0]130,130
mouseClickvaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::118,280
clickvaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::/VVerticalLayout[0]/Slot[3]/VButton[0]/domChild[0]/domChild[0]
assertTextvaadin=runcomvaadintestscomponentstextfieldBigDecimalTextField::PID_SLog_row_04. Commit ok. Property value: 130.13
+ + diff --git a/uitest/src/com/vaadin/tests/components/textfield/BigDecimalTextField.java b/uitest/src/com/vaadin/tests/components/textfield/BigDecimalTextField.java new file mode 100644 index 0000000000..18d8679c2f --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/textfield/BigDecimalTextField.java @@ -0,0 +1,123 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.components.textfield; + +import java.io.Serializable; +import java.math.BigDecimal; +import java.util.Locale; + +import com.vaadin.data.fieldgroup.FieldGroup; +import com.vaadin.data.util.BeanItem; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUIWithLog; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Button.ClickListener; +import com.vaadin.ui.FormLayout; +import com.vaadin.ui.TextField; +import com.vaadin.ui.VerticalLayout; + +/** + * @since 7.2 + * @author Vaadin Ltd + */ +public class BigDecimalTextField extends AbstractTestUIWithLog { + + @Override + protected void setup(VaadinRequest request) { + final VerticalLayout layout = new VerticalLayout(); + layout.setMargin(true); + setLocale(new Locale("fi", "FI")); + + BeanBigDecimal beanBigDecimal = new BeanBigDecimal(); + BeanItem beanItem = new BeanItem( + beanBigDecimal); + + FormLayout formLayout = new FormLayout(); + TextField textField = new TextField("BigDecimal field"); + textField.setImmediate(true); + textField.setValue("12"); + formLayout.addComponent(textField); + + final FieldGroup fieldGroup = new FieldGroup(beanItem); + fieldGroup.bind(textField, "decimal"); + + Button setValue = new Button("Set value to 15,2", new ClickListener() { + @Override + public void buttonClick(ClickEvent event) { + ((TextField) fieldGroup.getField("decimal")).setValue("15,2"); + } + }); + + Button button = new Button("Commit"); + button.addClickListener(new Button.ClickListener() { + @Override + public void buttonClick(Button.ClickEvent event) { + try { + fieldGroup.commit(); + log("Commit ok. Property value: " + + fieldGroup.getItemDataSource() + .getItemProperty("decimal").getValue()); + } catch (FieldGroup.CommitException e) { + log("Commit failed: " + e.getMessage()); + } + } + }); + + layout.addComponent(formLayout); + layout.addComponent(setValue); + layout.addComponent(button); + + setContent(layout); + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.tests.components.AbstractTestUI#getTestDescription() + */ + @Override + protected String getTestDescription() { + return "Tests that BigDecimals work correctly with TextFields"; + } + + /* + * (non-Javadoc) + * + * @see com.vaadin.tests.components.AbstractTestUI#getTicketNumber() + */ + @Override + protected Integer getTicketNumber() { + return 9997; + } + + public static class BeanBigDecimal implements Serializable { + BigDecimal decimal; + + public BeanBigDecimal() { + + } + + public BigDecimal getDecimal() { + return decimal; + } + + public void setDecimal(BigDecimal decimal) { + this.decimal = decimal; + } + } + +} -- 2.39.5