From: Artur Signell Date: Mon, 19 Dec 2011 14:01:11 +0000 (+0200) Subject: Added generics to FieldBinderFieldFactory X-Git-Tag: 7.0.0.alpha1~99 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=465c4770b504e81c8553ff16df313f345129b048;p=vaadin-framework.git Added generics to FieldBinderFieldFactory --- diff --git a/src/com/vaadin/data/fieldbinder/DefaultFieldBinderFieldFactory.java b/src/com/vaadin/data/fieldbinder/DefaultFieldBinderFieldFactory.java index 18a979cf65..95fa24f233 100644 --- a/src/com/vaadin/data/fieldbinder/DefaultFieldBinderFieldFactory.java +++ b/src/com/vaadin/data/fieldbinder/DefaultFieldBinderFieldFactory.java @@ -23,7 +23,7 @@ public class DefaultFieldBinderFieldFactory implements FieldBinderFieldFactory { public static final Object CAPTION_PROPERTY_ID = "Caption"; - public Field createField(Class type, Class fieldType) { + public T createField(Class type, Class fieldType) { if (Enum.class.isAssignableFrom(type)) { return createEnumField(type, fieldType); } else if (Boolean.class.isAssignableFrom(type) @@ -33,12 +33,12 @@ public class DefaultFieldBinderFieldFactory implements FieldBinderFieldFactory { return createDefaultField(type, fieldType); } - private Field createEnumField(Class type, - Class fieldType) { + private T createEnumField(Class type, + Class fieldType) { if (AbstractSelect.class.isAssignableFrom(fieldType)) { AbstractSelect s = createCompatibleSelect((Class) fieldType); populateWithEnumData(s, (Class) type); - return s; + return (T) s; } return null; @@ -68,41 +68,41 @@ public class DefaultFieldBinderFieldFactory implements FieldBinderFieldFactory { return select; } - protected Field createBooleanField(Class fieldType) { + protected T createBooleanField(Class fieldType) { if (fieldType.isAssignableFrom(CheckBox.class)) { CheckBox cb = new CheckBox(null); cb.setImmediate(true); - return cb; + return (T) cb; } else if (AbstractTextField.class.isAssignableFrom(fieldType)) { - return createAbstractTextField((Class) fieldType); + return (T) createAbstractTextField((Class) fieldType); } return null; } - protected AbstractTextField createAbstractTextField( - Class fieldType) { + protected T createAbstractTextField( + Class fieldType) { if (fieldType.isAssignableFrom(PasswordField.class)) { PasswordField pf = new PasswordField(); pf.setImmediate(true); - return pf; + return (T) pf; } else if (fieldType.isAssignableFrom(TextField.class)) { TextField tf = new TextField(); tf.setImmediate(true); - return tf; + return (T) tf; } else if (fieldType.isAssignableFrom(TextArea.class)) { TextArea ta = new TextArea(); ta.setImmediate(true); - return ta; + return (T) ta; } return null; } - protected Field createDefaultField(Class type, - Class fieldType) { + protected T createDefaultField(Class type, + Class fieldType) { if (AbstractTextField.class.isAssignableFrom(fieldType)) { - return createAbstractTextField((Class) fieldType); + return (T) createAbstractTextField((Class) fieldType); } return null; } @@ -113,7 +113,6 @@ public class DefaultFieldBinderFieldFactory implements FieldBinderFieldFactory { */ protected void populateWithEnumData(AbstractSelect select, Class enumClass) { - // TODO EnumContainer? select.removeAllItems(); for (Object p : select.getContainerPropertyIds()) { select.removeContainerProperty(p); diff --git a/src/com/vaadin/data/fieldbinder/FieldBinderFieldFactory.java b/src/com/vaadin/data/fieldbinder/FieldBinderFieldFactory.java index 3d4b0b49a3..5af4d22546 100644 --- a/src/com/vaadin/data/fieldbinder/FieldBinderFieldFactory.java +++ b/src/com/vaadin/data/fieldbinder/FieldBinderFieldFactory.java @@ -27,5 +27,5 @@ public interface FieldBinderFieldFactory extends Serializable { * @return A field that can be assigned to the given fieldType and that is * capable of editing the given type of data */ - Field createField(Class dataType, Class fieldType); + T createField(Class dataType, Class fieldType); } diff --git a/src/com/vaadin/data/fieldbinder/FormBuilder.java b/src/com/vaadin/data/fieldbinder/FormBuilder.java index c5f88185b1..3e78fa85e6 100644 --- a/src/com/vaadin/data/fieldbinder/FormBuilder.java +++ b/src/com/vaadin/data/fieldbinder/FormBuilder.java @@ -90,6 +90,24 @@ public class FormBuilder implements Serializable { } + /** + * Builds a field using the given caption and binds it to the given property + * id using the field binder. Ensures the new field is of the given type. + * + * @param caption + * The caption for the field + * @param propertyId + * The property id to bind to. Must be present in the field + * finder. + * @return The created and bound field. Can be any type of {@link Field}. + */ + public T buildAndBind(String caption, Object propertyId, + Class fieldType) { + Class type = getFieldBinder().getPropertyType(propertyId); + return buildAndBind(caption, propertyId, type, fieldType); + + } + /** * Builds a field with the given type and binds it to the given property id * using the field binder. @@ -105,9 +123,9 @@ public class FormBuilder implements Serializable { * The type of field we want to create * @return The created and bound field */ - protected Field buildAndBind(String caption, Object propertyId, - Class type, Class fieldType) { - Field field = build(caption, type, fieldType); + protected T buildAndBind(String caption, + Object propertyId, Class type, Class fieldType) { + T field = build(caption, type, fieldType); fieldBinder.bind(field, propertyId); return field; } @@ -128,11 +146,11 @@ public class FormBuilder implements Serializable { * The type of field that we want to create * @return A Field capable of editing the given type */ - protected Field build(String caption, Class dataType, - Class fieldType) { + protected T build(String caption, Class dataType, + Class fieldType) { logger.finest("Building a field with caption " + caption + " of type " + dataType.getName()); - Field field = getFieldFactory().createField(dataType, fieldType); + T field = getFieldFactory().createField(dataType, fieldType); if (field == null) { throw new BuildException("Unable to build a field of type " + fieldType.getName() + " for editing "