diff options
author | Artur Signell <artur@vaadin.com> | 2014-12-10 09:01:29 +0200 |
---|---|---|
committer | Leif Åstrand <leif@vaadin.com> | 2014-12-18 22:09:48 +0000 |
commit | 62d7830a7702691e6a262171ced119e0e7fa55fc (patch) | |
tree | cdae8c88ed231e81ee4100e690df9c56c0dd540b | |
parent | 42ef01bfdb2c93e03b8b8e54b7422cbc9b1eea32 (diff) | |
download | vaadin-framework-62d7830a7702691e6a262171ced119e0e7fa55fc.tar.gz vaadin-framework-62d7830a7702691e6a262171ced119e0e7fa55fc.zip |
Ensure enum fields are created when not specifying a strict type (#10975)
Change-Id: I64cfc5b315444d7135738a8f3870bf50050f4678
-rw-r--r-- | server/src/com/vaadin/data/fieldgroup/DefaultFieldGroupFieldFactory.java | 24 | ||||
-rw-r--r-- | server/tests/src/com/vaadin/data/DefaultFieldGroupFieldFactoryTest.java | 28 |
2 files changed, 49 insertions, 3 deletions
diff --git a/server/src/com/vaadin/data/fieldgroup/DefaultFieldGroupFieldFactory.java b/server/src/com/vaadin/data/fieldgroup/DefaultFieldGroupFieldFactory.java index 98fed3ad01..8e32585a47 100644 --- a/server/src/com/vaadin/data/fieldgroup/DefaultFieldGroupFieldFactory.java +++ b/server/src/com/vaadin/data/fieldgroup/DefaultFieldGroupFieldFactory.java @@ -91,10 +91,20 @@ public class DefaultFieldGroupFieldFactory implements FieldGroupFieldFactory { private <T extends Field> T createEnumField(Class<?> type, Class<T> fieldType) { + // Determine first if we should (or can) create a select for the enum + Class<AbstractSelect> selectClass = null; if (AbstractSelect.class.isAssignableFrom(fieldType)) { - AbstractSelect s = createCompatibleSelect((Class<? extends AbstractSelect>) fieldType); + selectClass = (Class<AbstractSelect>) fieldType; + } else if (anySelect(fieldType)) { + selectClass = AbstractSelect.class; + } + + if (selectClass != null) { + AbstractSelect s = createCompatibleSelect(selectClass); populateWithEnumData(s, (Class<? extends Enum>) type); return (T) s; + } else if (AbstractTextField.class.isAssignableFrom(fieldType)) { + return (T) createAbstractTextField((Class<? extends AbstractTextField>) fieldType); } return null; @@ -106,8 +116,8 @@ public class DefaultFieldGroupFieldFactory implements FieldGroupFieldFactory { if (InlineDateField.class.isAssignableFrom(fieldType)) { field = new InlineDateField(); - } else if (DateField.class.isAssignableFrom(fieldType) - || fieldType == Field.class) { + } else if (anyField(fieldType) + || DateField.class.isAssignableFrom(fieldType)) { field = new PopupDateField(); } else if (AbstractTextField.class.isAssignableFrom(fieldType)) { field = createAbstractTextField((Class<? extends AbstractTextField>) fieldType); @@ -119,6 +129,10 @@ public class DefaultFieldGroupFieldFactory implements FieldGroupFieldFactory { return (T) field; } + private boolean anyField(Class<?> fieldType) { + return fieldType == Field.class || fieldType == AbstractField.class; + } + protected AbstractSelect createCompatibleSelect( Class<? extends AbstractSelect> fieldType) { AbstractSelect select; @@ -143,6 +157,10 @@ public class DefaultFieldGroupFieldFactory implements FieldGroupFieldFactory { return select; } + private boolean anySelect(Class<? extends Field> fieldType) { + return anyField(fieldType) || fieldType == AbstractSelect.class; + } + protected <T extends Field> T createBooleanField(Class<T> fieldType) { if (fieldType.isAssignableFrom(CheckBox.class)) { CheckBox cb = new CheckBox(null); diff --git a/server/tests/src/com/vaadin/data/DefaultFieldGroupFieldFactoryTest.java b/server/tests/src/com/vaadin/data/DefaultFieldGroupFieldFactoryTest.java index bbf74bfb21..fc258ab138 100644 --- a/server/tests/src/com/vaadin/data/DefaultFieldGroupFieldFactoryTest.java +++ b/server/tests/src/com/vaadin/data/DefaultFieldGroupFieldFactoryTest.java @@ -23,9 +23,12 @@ import org.junit.Before; import org.junit.Test; import com.vaadin.data.fieldgroup.DefaultFieldGroupFieldFactory; +import com.vaadin.ui.AbstractSelect; +import com.vaadin.ui.ComboBox; import com.vaadin.ui.DateField; import com.vaadin.ui.Field; import com.vaadin.ui.InlineDateField; +import com.vaadin.ui.ListSelect; import com.vaadin.ui.PopupDateField; import com.vaadin.ui.TextField; @@ -89,4 +92,29 @@ public class DefaultFieldGroupFieldFactoryTest { Assert.assertEquals(PopupDateField.class, f.getClass()); } + public enum SomeEnum { + FOO, BAR; + } + + @Test + public void testEnumComboBox() { + Field f = fieldFactory.createField(SomeEnum.class, ComboBox.class); + Assert.assertNotNull(f); + Assert.assertEquals(ComboBox.class, f.getClass()); + } + + @Test + public void testEnumAnySelect() { + Field f = fieldFactory + .createField(SomeEnum.class, AbstractSelect.class); + Assert.assertNotNull(f); + Assert.assertEquals(ListSelect.class, f.getClass()); + } + + @Test + public void testEnumAnyField() { + Field f = fieldFactory.createField(SomeEnum.class, Field.class); + Assert.assertNotNull(f); + Assert.assertEquals(ListSelect.class, f.getClass()); + } } |