]> source.dussan.org Git - vaadin-framework.git/commitdiff
Ensure enum fields are created when not specifying a strict type (#10975)
authorArtur Signell <artur@vaadin.com>
Wed, 10 Dec 2014 07:01:29 +0000 (09:01 +0200)
committerLeif Åstrand <leif@vaadin.com>
Thu, 18 Dec 2014 22:09:48 +0000 (22:09 +0000)
Change-Id: I64cfc5b315444d7135738a8f3870bf50050f4678

server/src/com/vaadin/data/fieldgroup/DefaultFieldGroupFieldFactory.java
server/tests/src/com/vaadin/data/DefaultFieldGroupFieldFactoryTest.java

index 98fed3ad01305dc1eb1e351cd84fbbe60ec24a10..8e32585a47c26e0cb99bb7233f39df5e2fced028 100644 (file)
@@ -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);
index bbf74bfb21d7fa334ebebb5b6ce83b52acd656ed..fc258ab1384a53710d9d9d73b3c9b7cb2c7005b2 100644 (file)
@@ -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());
+    }
 }