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;
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);
return (T) field;
}
+ private boolean anyField(Class<?> fieldType) {
+ return fieldType == Field.class || fieldType == AbstractField.class;
+ }
+
protected AbstractSelect createCompatibleSelect(
Class<? extends AbstractSelect> fieldType) {
AbstractSelect select;
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);
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;
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());
+ }
}