From 62d7830a7702691e6a262171ced119e0e7fa55fc Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Wed, 10 Dec 2014 09:01:29 +0200 Subject: [PATCH] Ensure enum fields are created when not specifying a strict type (#10975) Change-Id: I64cfc5b315444d7135738a8f3870bf50050f4678 --- .../DefaultFieldGroupFieldFactory.java | 24 ++++++++++++++-- .../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 createEnumField(Class type, Class fieldType) { + // Determine first if we should (or can) create a select for the enum + Class selectClass = null; if (AbstractSelect.class.isAssignableFrom(fieldType)) { - AbstractSelect s = createCompatibleSelect((Class) fieldType); + selectClass = (Class) fieldType; + } else if (anySelect(fieldType)) { + selectClass = AbstractSelect.class; + } + + if (selectClass != null) { + AbstractSelect s = createCompatibleSelect(selectClass); populateWithEnumData(s, (Class) type); return (T) s; + } else if (AbstractTextField.class.isAssignableFrom(fieldType)) { + return (T) createAbstractTextField((Class) 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) 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 fieldType) { AbstractSelect select; @@ -143,6 +157,10 @@ public class DefaultFieldGroupFieldFactory implements FieldGroupFieldFactory { return select; } + private boolean anySelect(Class fieldType) { + return anyField(fieldType) || fieldType == AbstractSelect.class; + } + protected T createBooleanField(Class 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()); + } } -- 2.39.5