From: Artur Signell Date: Wed, 21 Dec 2011 08:48:27 +0000 (+0200) Subject: #8110 Allow Integers and Doubles to pass through X-Git-Tag: 7.0.0.alpha1~49 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=fb30d772b56f2fd80ad08bc0bd73bc068dd3e085;p=vaadin-framework.git #8110 Allow Integers and Doubles to pass through IntegerValidator/DoubleValidator for easier merge from Vaadin 6 --- diff --git a/src/com/vaadin/data/validator/DoubleValidator.java b/src/com/vaadin/data/validator/DoubleValidator.java index f603f57480..18f1909add 100644 --- a/src/com/vaadin/data/validator/DoubleValidator.java +++ b/src/com/vaadin/data/validator/DoubleValidator.java @@ -43,4 +43,16 @@ public class DoubleValidator extends AbstractStringValidator { } } + @Override + public void validate(Object value) throws InvalidValueException { + if (value != null && value instanceof Double) { + // Allow Doubles to pass through the validator for easier + // migration. Otherwise a TextField connected to an double property + // with a DoubleValidator will fail. + return; + } + + super.validate(value); + } + } diff --git a/src/com/vaadin/data/validator/IntegerValidator.java b/src/com/vaadin/data/validator/IntegerValidator.java index bf46497594..88ae9f3f0b 100644 --- a/src/com/vaadin/data/validator/IntegerValidator.java +++ b/src/com/vaadin/data/validator/IntegerValidator.java @@ -44,4 +44,15 @@ public class IntegerValidator extends AbstractStringValidator { } } + @Override + public void validate(Object value) throws InvalidValueException { + if (value != null && value instanceof Integer) { + // Allow Integers to pass through the validator for easier + // migration. Otherwise a TextField connected to an integer property + // with an IntegerValidator will fail. + return; + } + + super.validate(value); + } } diff --git a/tests/testbench/com/vaadin/tests/components/abstractfield/IntegerDoubleFieldsWithDataSource.java b/tests/testbench/com/vaadin/tests/components/abstractfield/IntegerDoubleFieldsWithDataSource.java new file mode 100644 index 0000000000..d9a731ae4d --- /dev/null +++ b/tests/testbench/com/vaadin/tests/components/abstractfield/IntegerDoubleFieldsWithDataSource.java @@ -0,0 +1,64 @@ +package com.vaadin.tests.components.abstractfield; + +import com.vaadin.data.Property.ValueChangeEvent; +import com.vaadin.data.Property.ValueChangeListener; +import com.vaadin.data.util.ObjectProperty; +import com.vaadin.data.validator.DoubleValidator; +import com.vaadin.data.validator.IntegerValidator; +import com.vaadin.tests.components.TestBase; +import com.vaadin.tests.util.Log; +import com.vaadin.ui.TextField; + +public class IntegerDoubleFieldsWithDataSource extends TestBase { + + private Log log = new Log(5); + + @Override + protected void setup() { + addComponent(log); + + TextField tf = createIntegerTextField(); + tf.addValidator(new IntegerValidator("Must be an Integer")); + addComponent(tf); + + tf = createIntegerTextField(); + tf.setCaption("Enter a double"); + tf.setPropertyDataSource(new ObjectProperty(2.1)); + tf.addValidator(new DoubleValidator("Must be a Double")); + addComponent(tf); + } + + private TextField createIntegerTextField() { + final TextField tf = new TextField("Enter an integer"); + tf.setPropertyDataSource(new ObjectProperty(new Integer(2))); + tf.setImmediate(true); + tf.addListener(new ValueChangeListener() { + + public void valueChange(ValueChangeEvent event) { + try { + log.log("Value for " + tf.getCaption() + " changed to " + + tf.getValue()); + log.log("Converted value is " + tf.getConvertedFieldValue()); + } catch (Exception e) { + // TODO: handle exception + e.printStackTrace(); + } + } + }); + + return tf; + } + + @Override + protected String getDescription() { + // TODO Auto-generated method stub + return null; + } + + @Override + protected Integer getTicketNumber() { + // TODO Auto-generated method stub + return null; + } + +}