]> source.dussan.org Git - vaadin-framework.git/commitdiff
Integrated FormBuilder into FieldGroup, renamed
authorArtur Signell <artur@vaadin.com>
Thu, 22 Dec 2011 10:41:24 +0000 (12:41 +0200)
committerArtur Signell <artur@vaadin.com>
Thu, 22 Dec 2011 10:42:03 +0000 (12:42 +0200)
FormBuilderFieldFactory -> FieldGroupFieldFactory

src/com/vaadin/data/fieldbinder/DefaultFormBuilderFieldFactory.java
src/com/vaadin/data/fieldbinder/FieldGroup.java
src/com/vaadin/data/fieldbinder/FieldGroupFieldFactory.java [new file with mode: 0644]
src/com/vaadin/data/fieldbinder/FormBuilder.java [deleted file]
src/com/vaadin/data/fieldbinder/FormBuilderFieldFactory.java [deleted file]
tests/testbench/com/vaadin/tests/fieldbinder/BasicPersonForm.java
tests/testbench/com/vaadin/tests/fieldbinder/FieldBinderWithBeanValidation.java
tests/testbench/com/vaadin/tests/fieldbinder/FormBuilderWithNestedProperties.java
tests/testbench/com/vaadin/tests/fieldbinder/FormWithNestedProperties.java

index 2b372de1441f546289cbbb9c3fedd521cbd71484..3977879267277a6ad2666508825e94e65eeb6f3f 100644 (file)
@@ -6,6 +6,7 @@ package com.vaadin.data.fieldbinder;
 import java.util.EnumSet;\r
 \r
 import com.vaadin.data.Item;\r
+import com.vaadin.data.fieldbinder.FieldGroup.BindException;\r
 import com.vaadin.ui.AbstractSelect;\r
 import com.vaadin.ui.AbstractTextField;\r
 import com.vaadin.ui.CheckBox;\r
@@ -14,10 +15,11 @@ import com.vaadin.ui.Field;
 import com.vaadin.ui.ListSelect;\r
 import com.vaadin.ui.NativeSelect;\r
 import com.vaadin.ui.OptionGroup;\r
+import com.vaadin.ui.RichTextArea;\r
 import com.vaadin.ui.Table;\r
 import com.vaadin.ui.TextField;\r
 \r
-public class DefaultFormBuilderFieldFactory implements FormBuilderFieldFactory {\r
+public class DefaultFormBuilderFieldFactory implements FieldGroupFieldFactory {\r
 \r
     public static final Object CAPTION_PROPERTY_ID = "Caption";\r
 \r
@@ -31,10 +33,19 @@ public class DefaultFormBuilderFieldFactory implements FormBuilderFieldFactory {
         if (AbstractTextField.class.isAssignableFrom(fieldType)) {\r
             return fieldType.cast(createAbstractTextField(fieldType\r
                     .asSubclass(AbstractTextField.class)));\r
+        } else if (fieldType == RichTextArea.class) {\r
+            return fieldType.cast(createRichTextArea());\r
         }\r
         return createDefaultField(type, fieldType);\r
     }\r
 \r
+    protected RichTextArea createRichTextArea() {\r
+        RichTextArea rta = new RichTextArea();\r
+        rta.setImmediate(true);\r
+\r
+        return rta;\r
+    }\r
+\r
     private <T extends Field> T createEnumField(Class<?> type,\r
             Class<T> fieldType) {\r
         if (AbstractSelect.class.isAssignableFrom(fieldType)) {\r
@@ -92,11 +103,24 @@ public class DefaultFormBuilderFieldFactory implements FormBuilderFieldFactory {
             field.setImmediate(true);\r
             return field;\r
         } catch (Exception e) {\r
-            throw new FormBuilder.BuildException(\r
-                    "Could not create a field of type " + fieldType, e);\r
+            throw new BindException("Could not create a field of type "\r
+                    + fieldType, e);\r
         }\r
     }\r
 \r
+    /**\r
+     * Fallback when no specific field has been created. Typically returns a\r
+     * TextField.\r
+     * \r
+     * @param <T>\r
+     *            The type of field to create\r
+     * @param type\r
+     *            The type of data that should be edited\r
+     * @param fieldType\r
+     *            The type of field to create\r
+     * @return A field capable of editing the data or null if no field could be\r
+     *         created\r
+     */\r
     protected <T extends Field> T createDefaultField(Class<?> type,\r
             Class<T> fieldType) {\r
         if (fieldType.isAssignableFrom(TextField.class)) {\r
index 920849a289651d392936a37d2f787bb713c24bc7..8f5a507470aae67e413cbb168ab05bb9ad071569 100644 (file)
@@ -4,6 +4,7 @@
 package com.vaadin.data.fieldbinder;\r
 \r
 import java.io.Serializable;\r
+import java.lang.reflect.InvocationTargetException;\r
 import java.util.ArrayList;\r
 import java.util.Collection;\r
 import java.util.Collections;\r
@@ -18,6 +19,7 @@ import com.vaadin.data.TransactionalProperty;
 import com.vaadin.data.Validator.InvalidValueException;\r
 import com.vaadin.data.util.TransactionalPropertyWrapper;\r
 import com.vaadin.tools.ReflectTools;\r
+import com.vaadin.ui.DefaultFieldFactory;\r
 import com.vaadin.ui.Field;\r
 import com.vaadin.ui.Form;\r
 \r
@@ -56,6 +58,11 @@ public class FieldGroup implements Serializable {
     private LinkedHashMap<Field<?>, Object> fieldToPropertyId = new LinkedHashMap<Field<?>, Object>();\r
     private List<CommitHandler> commitHandlers = new ArrayList<CommitHandler>();\r
 \r
+    /**\r
+     * The field factory used by builder methods.\r
+     */\r
+    private FieldGroupFieldFactory fieldFactory;\r
+\r
     /**\r
      * Constructs a field binder. Use {@link #setItemDataSource(Item)} to set a\r
      * data source for the field binder.\r
@@ -634,6 +641,28 @@ public class FieldGroup implements Serializable {
         return false;\r
     }\r
 \r
+    /**\r
+     * Gets the field factory for the {@link FieldGroup}. The field factory is\r
+     * only used when {@link FieldGroup} creates a new field.\r
+     * \r
+     * @return The field factory in use\r
+     * \r
+     */\r
+    public FieldGroupFieldFactory getFieldFactory() {\r
+        return fieldFactory;\r
+    }\r
+\r
+    /**\r
+     * Sets the field factory for the {@link FieldGroup}. The field factory is\r
+     * only used when {@link FieldGroup} creates a new field.\r
+     * \r
+     * @param fieldFactory\r
+     *            The field factory to use\r
+     */\r
+    public void setFieldFactory(FieldGroupFieldFactory fieldFactory) {\r
+        this.fieldFactory = fieldFactory;\r
+    }\r
+\r
     /**\r
      * Binds member fields found in the given object.\r
      * <p>\r
@@ -659,9 +688,9 @@ public class FieldGroup implements Serializable {
      * </pre>\r
      * \r
      * </p>\r
-     * This binds the firstName TextField to a "firstName" property id in the\r
-     * item, lastName TextField to a "last" property and the age TextField to a\r
-     * "age" property id.\r
+     * This binds the firstName TextField to a "firstName" property in the item,\r
+     * lastName TextField to a "last" property and the age TextField to a "age"\r
+     * property.\r
      * \r
      * @param objectWithMemberFields\r
      *            The object that contains (Java) member fields to bind\r
@@ -670,18 +699,87 @@ public class FieldGroup implements Serializable {
      */\r
     public void bindMemberFields(Object objectWithMemberFields)\r
             throws BindException {\r
+        buildAndBindMemberFields(objectWithMemberFields, false);\r
+    }\r
+\r
+    /**\r
+     * Binds member fields found in the given object and builds member fields\r
+     * that have not been initialized.\r
+     * <p>\r
+     * This method processes all (Java) member fields whose type extends\r
+     * {@link Field} and that can be mapped to a property id. Property id\r
+     * mapping is done based on the field name or on a @{@link PropertyId}\r
+     * annotation on the field. Fields that are not initialized (null) are built\r
+     * using the field factory. All non-null fields for which a property id can\r
+     * be determined are bound to the property id.\r
+     * </p>\r
+     * <p>\r
+     * For example:\r
+     * \r
+     * <pre>\r
+     * public class MyForm extends VerticalLayout {\r
+     * private TextField firstName = new TextField("First name");\r
+     * @PropertyId("last")\r
+     * private TextField lastName = new TextField("Last name"); \r
+     * private TextField age;\r
+     * \r
+     * MyForm myForm = new MyForm(); \r
+     * ... \r
+     * fieldGroup.buildAndBindMemberFields(myForm);\r
+     * </pre>\r
+     * \r
+     * </p>\r
+     * <p>\r
+     * This binds the firstName TextField to a "firstName" property in the item,\r
+     * lastName TextField to a "last" property and builds an age TextField using\r
+     * the field factory and then binds it to the "age" property.\r
+     * </p>\r
+     * \r
+     * @param objectWithMemberFields\r
+     *            The object that contains (Java) member fields to build and\r
+     *            bind\r
+     * @throws BindException\r
+     *             If there is a problem binding or building a field\r
+     */\r
+    public void buildAndBindMemberFields(Object objectWithMemberFields)\r
+            throws BindException {\r
+        buildAndBindMemberFields(objectWithMemberFields, true);\r
+    }\r
+\r
+    /**\r
+     * Binds member fields found in the given object and optionally builds\r
+     * member fields that have not been initialized.\r
+     * <p>\r
+     * This method processes all (Java) member fields whose type extends\r
+     * {@link Field} and that can be mapped to a property id. Property id\r
+     * mapping is done based on the field name or on a @{@link PropertyId}\r
+     * annotation on the field. Fields that are not initialized (null) are built\r
+     * using the field factory is buildFields is true. All non-null fields for\r
+     * which a property id can be determined are bound to the property id.\r
+     * </p>\r
+     * \r
+     * @param objectWithMemberFields\r
+     *            The object that contains (Java) member fields to build and\r
+     *            bind\r
+     * @throws BindException\r
+     *             If there is a problem binding or building a field\r
+     */\r
+    protected void buildAndBindMemberFields(Object objectWithMemberFields,\r
+            boolean buildFields) throws BindException {\r
         Class<?> objectClass = objectWithMemberFields.getClass();\r
 \r
-        for (java.lang.reflect.Field f : objectClass.getDeclaredFields()) {\r
+        for (java.lang.reflect.Field memberField : objectClass\r
+                .getDeclaredFields()) {\r
 \r
-            if (!Field.class.isAssignableFrom(f.getType())) {\r
+            if (!Field.class.isAssignableFrom(memberField.getType())) {\r
                 // Process next field\r
                 continue;\r
             }\r
 \r
-            PropertyId propertyIdAnnotation = f.getAnnotation(PropertyId.class);\r
+            PropertyId propertyIdAnnotation = memberField\r
+                    .getAnnotation(PropertyId.class);\r
 \r
-            Class<? extends Field> fieldType = (Class<? extends Field>) f\r
+            Class<? extends Field> fieldType = (Class<? extends Field>) memberField\r
                     .getType();\r
 \r
             Object propertyId = null;\r
@@ -689,7 +787,7 @@ public class FieldGroup implements Serializable {
                 // @PropertyId(propertyId) always overrides property id\r
                 propertyId = propertyIdAnnotation.value();\r
             } else {\r
-                propertyId = f.getName();\r
+                propertyId = memberField.getName();\r
             }\r
 \r
             // Ensure that the property id exists\r
@@ -702,18 +800,51 @@ public class FieldGroup implements Serializable {
                 continue;\r
             }\r
 \r
+            Field<?> field;\r
             try {\r
                 // Get the field from the object\r
-                Field<?> field = (Field<?>) ReflectTools.getJavaFieldValue(\r
-                        objectWithMemberFields, f);\r
-                // Bind it to the property id\r
-                bind(field, propertyId);\r
+                field = (Field<?>) ReflectTools.getJavaFieldValue(\r
+                        objectWithMemberFields, memberField);\r
             } catch (Exception e) {\r
                 // If we cannot determine the value, just skip the field and try\r
                 // the next one\r
                 continue;\r
             }\r
 \r
+            if (field == null && buildFields) {\r
+                Caption captionAnnotation = memberField\r
+                        .getAnnotation(Caption.class);\r
+                String caption;\r
+                if (captionAnnotation != null) {\r
+                    caption = captionAnnotation.value();\r
+                } else {\r
+                    caption = DefaultFieldFactory\r
+                            .createCaptionByPropertyId(propertyId);\r
+                }\r
+\r
+                // Create the component (Field)\r
+                field = build(caption, propertyType, fieldType);\r
+\r
+                // Store it in the field\r
+                try {\r
+                    ReflectTools.setJavaFieldValue(objectWithMemberFields,\r
+                            memberField, field);\r
+                } catch (IllegalArgumentException e) {\r
+                    throw new BindException("Could not assign value to field '"\r
+                            + memberField.getName() + "'", e);\r
+                } catch (IllegalAccessException e) {\r
+                    throw new BindException("Could not assign value to field '"\r
+                            + memberField.getName() + "'", e);\r
+                } catch (InvocationTargetException e) {\r
+                    throw new BindException("Could not assign value to field '"\r
+                            + memberField.getName() + "'", e);\r
+                }\r
+            }\r
+\r
+            if (field != null) {\r
+                // Bind it to the property id\r
+                bind(field, propertyId);\r
+            }\r
         }\r
     }\r
 \r
@@ -752,4 +883,96 @@ public class FieldGroup implements Serializable {
         }\r
 \r
     }\r
-}
+\r
+    /**\r
+     * Builds a field and binds it to the given property id using the field\r
+     * binder.\r
+     * \r
+     * @param propertyId\r
+     *            The property id to bind to. Must be present in the field\r
+     *            finder.\r
+     * @throws BindException\r
+     *             If there is a problem while building or binding\r
+     * @return The created and bound field\r
+     */\r
+    public Field<?> buildAndBind(Object propertyId) throws BindException {\r
+        String caption = DefaultFieldFactory\r
+                .createCaptionByPropertyId(propertyId);\r
+        return buildAndBind(caption, propertyId);\r
+    }\r
+\r
+    /**\r
+     * Builds a field using the given caption and binds it to the given property\r
+     * id using the field binder.\r
+     * \r
+     * @param caption\r
+     *            The caption for the field\r
+     * @param propertyId\r
+     *            The property id to bind to. Must be present in the field\r
+     *            finder.\r
+     * @throws BindException\r
+     *             If there is a problem while building or binding\r
+     * @return The created and bound field. Can be any type of {@link Field}.\r
+     */\r
+    public Field<?> buildAndBind(String caption, Object propertyId)\r
+            throws BindException {\r
+        Class<?> type = getPropertyType(propertyId);\r
+        return buildAndBind(caption, propertyId, Field.class);\r
+\r
+    }\r
+\r
+    /**\r
+     * Builds a field using the given caption and binds it to the given property\r
+     * id using the field binder. Ensures the new field is of the given type.\r
+     * \r
+     * @param caption\r
+     *            The caption for the field\r
+     * @param propertyId\r
+     *            The property id to bind to. Must be present in the field\r
+     *            finder.\r
+     * @throws BindException\r
+     *             If the field could not be created\r
+     * @return The created and bound field. Can be any type of {@link Field}.\r
+     */\r
+\r
+    public <T extends Field> T buildAndBind(String caption, Object propertyId,\r
+            Class<T> fieldType) throws BindException {\r
+        Class<?> type = getPropertyType(propertyId);\r
+\r
+        T field = build(caption, type, fieldType);\r
+        bind(field, propertyId);\r
+\r
+        return field;\r
+    }\r
+\r
+    /**\r
+     * Creates a field based on the given data type.\r
+     * <p>\r
+     * The data type is the type that we want to edit using the field. The field\r
+     * type is the type of field we want to create, can be {@link Field} if any\r
+     * Field is good.\r
+     * </p>\r
+     * \r
+     * @param caption\r
+     *            The caption for the new field\r
+     * @param dataType\r
+     *            The data model type that we want to edit using the field\r
+     * @param fieldType\r
+     *            The type of field that we want to create\r
+     * @return A Field capable of editing the given type\r
+     * @throws BindException\r
+     *             If the field could not be created\r
+     */\r
+    protected <T extends Field> T build(String caption, Class<?> dataType,\r
+            Class<T> fieldType) throws BindException {\r
+        T field = getFieldFactory().createField(dataType, fieldType);\r
+        if (field == null) {\r
+            throw new BindException("Unable to build a field of type "\r
+                    + fieldType.getName() + " for editing "\r
+                    + dataType.getName());\r
+        }\r
+\r
+        field.setCaption(caption);\r
+        return field;\r
+    }\r
+}
\ No newline at end of file
diff --git a/src/com/vaadin/data/fieldbinder/FieldGroupFieldFactory.java b/src/com/vaadin/data/fieldbinder/FieldGroupFieldFactory.java
new file mode 100644 (file)
index 0000000..8a8c2b9
--- /dev/null
@@ -0,0 +1,31 @@
+/* \r
+@VaadinApache2LicenseForJavaFiles@\r
+ */\r
+package com.vaadin.data.fieldbinder;\r
+\r
+import java.io.Serializable;\r
+\r
+import com.vaadin.ui.Field;\r
+\r
+/**\r
+ * Factory interface for creating new Field-instances based on the data type\r
+ * that should be edited.\r
+ * \r
+ * @author Vaadin Ltd.\r
+ * @version @version@\r
+ * @since 7.0\r
+ */\r
+public interface FieldGroupFieldFactory extends Serializable {\r
+    /**\r
+     * Creates a field based on the data type that we want to edit\r
+     * \r
+     * @param dataType\r
+     *            The type that we want to edit using the field\r
+     * @param fieldType\r
+     *            The type of field we want to create. If set to {@link Field}\r
+     *            then any type of field is accepted\r
+     * @return A field that can be assigned to the given fieldType and that is\r
+     *         capable of editing the given type of data\r
+     */\r
+    <T extends Field> T createField(Class<?> dataType, Class<T> fieldType);\r
+}\r
diff --git a/src/com/vaadin/data/fieldbinder/FormBuilder.java b/src/com/vaadin/data/fieldbinder/FormBuilder.java
deleted file mode 100644 (file)
index ccf060e..0000000
+++ /dev/null
@@ -1,295 +0,0 @@
-/* \r
-@VaadinApache2LicenseForJavaFiles@\r
- */\r
-package com.vaadin.data.fieldbinder;\r
-\r
-import java.io.Serializable;\r
-import java.lang.reflect.InvocationTargetException;\r
-import java.util.logging.Logger;\r
-\r
-import com.vaadin.data.fieldbinder.FieldGroup.BindException;\r
-import com.vaadin.tools.ReflectTools;\r
-import com.vaadin.ui.DefaultFieldFactory;\r
-import com.vaadin.ui.Field;\r
-\r
-/**\r
- * Class for constructing form fields based on a data type.\r
- * <p>\r
- * \r
- * FIXME Javadoc\r
- */\r
-public class FormBuilder implements Serializable {\r
-\r
-    private FormBuilderFieldFactory fieldFactory = new DefaultFormBuilderFieldFactory();\r
-    private FieldGroup fieldBinder;\r
-    private static final Logger logger = Logger.getLogger(FormBuilder.class\r
-            .getName());\r
-\r
-    /**\r
-     * Constructs a FormBuilder that can be used to build forms automatically.\r
-     * \r
-     * @param fieldBinder\r
-     *            The FieldBinder to use for binding the fields to the data\r
-     *            source\r
-     */\r
-    public FormBuilder(FieldGroup fieldBinder) {\r
-        this.fieldBinder = fieldBinder;\r
-    }\r
-\r
-    /**\r
-     * TODO: javadoc\r
-     */\r
-    protected FieldGroup getFieldBinder() {\r
-        return fieldBinder;\r
-    }\r
-\r
-    /**\r
-     * TODO: javadoc\r
-     */\r
-    public FormBuilderFieldFactory getFieldFactory() {\r
-        return fieldFactory;\r
-    }\r
-\r
-    /**\r
-     * TODO: javadoc\r
-     */\r
-    public void setFieldFactory(FormBuilderFieldFactory fieldFactory) {\r
-        this.fieldFactory = fieldFactory;\r
-    }\r
-\r
-    /**\r
-     * Builds a field and binds it to the given property id using the field\r
-     * binder.\r
-     * \r
-     * @param propertyId\r
-     *            The property id to bind to. Must be present in the field\r
-     *            finder.\r
-     * @return The created and bound field\r
-     */\r
-    public Field<?> buildAndBind(Object propertyId) {\r
-        String caption = DefaultFieldFactory\r
-                .createCaptionByPropertyId(propertyId);\r
-        return buildAndBind(caption, propertyId);\r
-    }\r
-\r
-    /**\r
-     * Builds a field using the given caption and binds it to the given property\r
-     * id using the field binder.\r
-     * \r
-     * @param caption\r
-     *            The caption for the field\r
-     * @param propertyId\r
-     *            The property id to bind to. Must be present in the field\r
-     *            finder.\r
-     * @return The created and bound field. Can be any type of {@link Field}.\r
-     */\r
-    public Field<?> buildAndBind(String caption, Object propertyId) {\r
-        Class<?> type = getFieldBinder().getPropertyType(propertyId);\r
-        return buildAndBind(caption, propertyId, type, Field.class);\r
-\r
-    }\r
-\r
-    /**\r
-     * Builds a field using the given caption and binds it to the given property\r
-     * id using the field binder. Ensures the new field is of the given type.\r
-     * \r
-     * @param caption\r
-     *            The caption for the field\r
-     * @param propertyId\r
-     *            The property id to bind to. Must be present in the field\r
-     *            finder.\r
-     * @return The created and bound field. Can be any type of {@link Field}.\r
-     */\r
-    public <T extends Field> T buildAndBind(String caption, Object propertyId,\r
-            Class<T> fieldType) {\r
-        Class<?> type = getFieldBinder().getPropertyType(propertyId);\r
-        return buildAndBind(caption, propertyId, type, fieldType);\r
-\r
-    }\r
-\r
-    /**\r
-     * Builds a field with the given type and binds it to the given property id\r
-     * using the field binder.\r
-     * \r
-     * @param caption\r
-     *            The caption for the new field\r
-     * @param propertyId\r
-     *            The property id to bind to. Must be present in the field\r
-     *            binder.\r
-     * @param type\r
-     *            The data model type we want to edit using the field\r
-     * @param fieldType\r
-     *            The type of field we want to create\r
-     * @return The created and bound field\r
-     */\r
-    protected <T extends Field> T buildAndBind(String caption,\r
-            Object propertyId, Class<?> type, Class<T> fieldType) {\r
-        T field = build(caption, type, fieldType);\r
-        fieldBinder.bind(field, propertyId);\r
-        return field;\r
-    }\r
-\r
-    /**\r
-     * Creates a field based on the given data type.\r
-     * <p>\r
-     * The data type is the type that we want to edit using the field. The field\r
-     * type is the type of field we want to create, can be {@link Field} if any\r
-     * Field is good.\r
-     * </p>\r
-     * \r
-     * @param caption\r
-     *            The caption for the new field\r
-     * @param dataType\r
-     *            The data model type that we want to edit using the field\r
-     * @param fieldType\r
-     *            The type of field that we want to create\r
-     * @return A Field capable of editing the given type\r
-     */\r
-    protected <T extends Field> T build(String caption, Class<?> dataType,\r
-            Class<T> fieldType) {\r
-        logger.finest("Building a field with caption " + caption + " of type "\r
-                + dataType.getName());\r
-        T field = getFieldFactory().createField(dataType, fieldType);\r
-        if (field == null) {\r
-            throw new BuildException("Unable to build a field of type "\r
-                    + fieldType.getName() + " for editing "\r
-                    + dataType.getName());\r
-        }\r
-\r
-        field.setCaption(caption);\r
-        return field;\r
-    }\r
-\r
-    /**\r
-     * Builds and binds fields for the given class.\r
-     * <p>\r
-     * This method processes all fields whose type extends {@link Field} and\r
-     * that can be mapped to a property id. Property id mapping is done based on\r
-     * the field name or on a {@link PropertyId} annotation on the field. All\r
-     * fields for which a property id can be determined are built if they are\r
-     * null and then bound to the property id. Also existing fields are bound to\r
-     * the corresponding property id.\r
-     * \r
-     * @param object\r
-     *            The object to process\r
-     * @throws FormBuilderException\r
-     *             If there is a problem building or binding a field\r
-     */\r
-    public void buildAndBindFields(Object object) throws FormBuilderException {\r
-        Class<?> objectClass = object.getClass();\r
-\r
-        for (java.lang.reflect.Field f : objectClass.getDeclaredFields()) {\r
-            PropertyId propertyIdAnnotation = f.getAnnotation(PropertyId.class);\r
-\r
-            if (!Field.class.isAssignableFrom(f.getType())) {\r
-                // Process next field\r
-                continue;\r
-            }\r
-            Class<? extends Field> fieldType = (Class<? extends Field>) f\r
-                    .getType();\r
-\r
-            Object propertyId = null;\r
-            if (propertyIdAnnotation != null) {\r
-                // @PropertyId(propertyId) always overrides property id\r
-                propertyId = propertyIdAnnotation.value();\r
-            } else {\r
-                propertyId = f.getName();\r
-            }\r
-\r
-            // Ensure that the property id exists\r
-            Class<?> propertyType;\r
-\r
-            try {\r
-                propertyType = fieldBinder.getPropertyType(propertyId);\r
-            } catch (BindException e) {\r
-                // Property id was not found, skip this field\r
-                continue;\r
-            }\r
-\r
-            Field<?> builtField;\r
-            try {\r
-                builtField = (Field<?>) ReflectTools.getJavaFieldValue(object,\r
-                        f);\r
-            } catch (Exception e) {\r
-                // If we cannot determine the value, just skip the field and try\r
-                // the next one\r
-                continue;\r
-            }\r
-\r
-            if (builtField == null) {\r
-                // Field is null -> build the field\r
-                Caption captionAnnotation = f.getAnnotation(Caption.class);\r
-                String caption;\r
-                if (captionAnnotation != null) {\r
-                    caption = captionAnnotation.value();\r
-                } else {\r
-                    caption = DefaultFieldFactory\r
-                            .createCaptionByPropertyId(propertyId);\r
-                }\r
-\r
-                // Create the component (Field)\r
-                builtField = build(caption, propertyType, fieldType);\r
-\r
-                // Store it in the field\r
-                try {\r
-                    ReflectTools.setJavaFieldValue(object, f, builtField);\r
-                } catch (IllegalArgumentException e) {\r
-                    throw new BuildException(\r
-                            "Could not assign value to field '" + f.getName()\r
-                                    + "'", e);\r
-                } catch (IllegalAccessException e) {\r
-                    throw new BuildException(\r
-                            "Could not assign value to field '" + f.getName()\r
-                                    + "'", e);\r
-                } catch (InvocationTargetException e) {\r
-                    throw new BuildException(\r
-                            "Could not assign value to field '" + f.getName()\r
-                                    + "'", e);\r
-                }\r
-            }\r
-\r
-            // Bind it to the property id\r
-            if (builtField != null) {\r
-                fieldBinder.bind(builtField, propertyId);\r
-            }\r
-\r
-        }\r
-    }\r
-\r
-    public static class FormBuilderException extends RuntimeException {\r
-\r
-        public FormBuilderException() {\r
-            super();\r
-            // TODO Auto-generated constructor stub\r
-        }\r
-\r
-        public FormBuilderException(String message, Throwable cause) {\r
-            super(message, cause);\r
-            // TODO Auto-generated constructor stub\r
-        }\r
-\r
-        public FormBuilderException(String message) {\r
-            super(message);\r
-            // TODO Auto-generated constructor stub\r
-        }\r
-\r
-        public FormBuilderException(Throwable cause) {\r
-            super(cause);\r
-            // TODO Auto-generated constructor stub\r
-        }\r
-\r
-    }\r
-\r
-    public static class BuildException extends RuntimeException {\r
-\r
-        public BuildException(String message) {\r
-            super(message);\r
-        }\r
-\r
-        public BuildException(String message, Throwable t) {\r
-            super(message, t);\r
-        }\r
-\r
-    }\r
-\r
-}\r
diff --git a/src/com/vaadin/data/fieldbinder/FormBuilderFieldFactory.java b/src/com/vaadin/data/fieldbinder/FormBuilderFieldFactory.java
deleted file mode 100644 (file)
index c189fb5..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-/* \r
-@VaadinApache2LicenseForJavaFiles@\r
- */\r
-package com.vaadin.data.fieldbinder;\r
-\r
-import java.io.Serializable;\r
-\r
-import com.vaadin.ui.Field;\r
-\r
-/**\r
- * Factory interface for creating new Field-instances based on the data type\r
- * that should be edited.\r
- * \r
- * @author Vaadin Ltd.\r
- * @version @version@\r
- * @since 7.0\r
- */\r
-public interface FormBuilderFieldFactory extends Serializable {\r
-    /**\r
-     * Creates a field based on the data type that we want to edit\r
-     * \r
-     * @param dataType\r
-     *            The type that we want to edit using the field\r
-     * @param fieldType\r
-     *            The type of field we want to create. If set to {@link Field}\r
-     *            then any type of field is accepted\r
-     * @return A field that can be assigned to the given fieldType and that is\r
-     *         capable of editing the given type of data\r
-     */\r
-    <T extends Field> T createField(Class<?> dataType, Class<T> fieldType);\r
-}\r
index 0a6fa40b3ab2c81ef0f70c999a0419ac55150480..dd3f201f49a80998da6159fb06c6d3f474d0d58f 100644 (file)
@@ -5,7 +5,6 @@ import com.vaadin.data.fieldbinder.FieldGroup;
 import com.vaadin.data.fieldbinder.FieldGroup.CommitEvent;\r
 import com.vaadin.data.fieldbinder.FieldGroup.CommitException;\r
 import com.vaadin.data.fieldbinder.FieldGroup.CommitHandler;\r
-import com.vaadin.data.fieldbinder.FormBuilder;\r
 import com.vaadin.data.util.BeanItem;\r
 import com.vaadin.data.util.converter.StringToBooleanConverter;\r
 import com.vaadin.data.validator.EmailValidator;\r
@@ -65,13 +64,12 @@ public class BasicPersonForm extends TestBase {
             super("Configuration");\r
             BeanItem<Configuration> bi = new BeanItem<BasicPersonForm.Configuration>(\r
                     configuration);\r
-            FieldGroup confBinder = new FieldGroup(bi);\r
-            confBinder.setItemDataSource(bi);\r
-            confBinder.setBuffered(false);\r
+            FieldGroup confFieldGroup = new FieldGroup(bi);\r
+            confFieldGroup.setItemDataSource(bi);\r
+            confFieldGroup.setBuffered(false);\r
 \r
-            FormBuilder builder = new FormBuilder(confBinder);\r
             for (Object propertyId : bi.getItemPropertyIds()) {\r
-                addComponent(builder.buildAndBind(propertyId));\r
+                addComponent(confFieldGroup.buildAndBind(propertyId));\r
             }\r
 \r
         }\r
@@ -83,8 +81,8 @@ public class BasicPersonForm extends TestBase {
         Panel confPanel = new ConfigurationPanel();\r
         addComponent(confPanel);\r
 \r
-        final FieldGroup binder = new BeanFieldGroup<Person>(Person.class);\r
-        binder.addCommitHandler(new CommitHandler() {\r
+        final FieldGroup fieldGroup = new BeanFieldGroup<Person>(Person.class);\r
+        fieldGroup.addCommitHandler(new CommitHandler() {\r
 \r
             public void preCommit(CommitEvent commitEvent)\r
                     throws CommitException {\r
@@ -108,10 +106,9 @@ public class BasicPersonForm extends TestBase {
             }\r
         });\r
 \r
-        binder.setBuffered(true);\r
+        fieldGroup.setBuffered(true);\r
 \r
-        FormBuilder builder = new FormBuilder(binder);\r
-        builder.buildAndBindFields(this);\r
+        fieldGroup.buildAndBindMemberFields(this);\r
         addComponent(firstName);\r
         addComponent(lastName);\r
         addComponent(email);\r
@@ -124,7 +121,7 @@ public class BasicPersonForm extends TestBase {
             public void buttonClick(ClickEvent event) {\r
                 String msg = "Commit succesful";\r
                 try {\r
-                    binder.commit();\r
+                    fieldGroup.commit();\r
                 } catch (CommitException e) {\r
                     msg = "Commit failed: " + e.getMessage();\r
                 }\r
@@ -137,7 +134,7 @@ public class BasicPersonForm extends TestBase {
                 new Button.ClickListener() {\r
 \r
                     public void buttonClick(ClickEvent event) {\r
-                        binder.discard();\r
+                        fieldGroup.discard();\r
                         log.log("Discarded changes");\r
 \r
                     }\r
@@ -146,7 +143,7 @@ public class BasicPersonForm extends TestBase {
                 new Button.ClickListener() {\r
 \r
                     public void buttonClick(ClickEvent event) {\r
-                        log.log(getPerson(binder).toString());\r
+                        log.log(getPerson(fieldGroup).toString());\r
 \r
                     }\r
                 });\r
@@ -173,7 +170,7 @@ public class BasicPersonForm extends TestBase {
         });\r
         Person p = new Person("John", "Doe", "john@doe.com", 64, Sex.MALE,\r
                 new Address("John street", 11223, "John's town", Country.USA));\r
-        binder.setItemDataSource(new BeanItem<Person>(p));\r
+        fieldGroup.setItemDataSource(new BeanItem<Person>(p));\r
     }\r
 \r
     public static Person getPerson(FieldGroup binder) {\r
index 9af14cb9fd63db6312f9d6b3677fb3d70c5be5e5..f6c585d5db6276ccc8f86de107eb28615ced6649 100644 (file)
@@ -3,7 +3,6 @@ package com.vaadin.tests.fieldbinder;
 import com.vaadin.data.fieldbinder.BeanFieldGroup;\r
 import com.vaadin.data.fieldbinder.FieldGroup;\r
 import com.vaadin.data.fieldbinder.FieldGroup.CommitException;\r
-import com.vaadin.data.fieldbinder.FormBuilder;\r
 import com.vaadin.data.util.BeanItem;\r
 import com.vaadin.tests.components.TestBase;\r
 import com.vaadin.tests.data.bean.Address;\r
@@ -33,11 +32,10 @@ public class FieldBinderWithBeanValidation extends TestBase {
     protected void setup() {\r
         addComponent(log);\r
 \r
-        final BeanFieldGroup<PersonWithBeanValidationAnnotations> binder = new BeanFieldGroup<PersonWithBeanValidationAnnotations>(\r
+        final BeanFieldGroup<PersonWithBeanValidationAnnotations> fieldGroup = new BeanFieldGroup<PersonWithBeanValidationAnnotations>(\r
                 PersonWithBeanValidationAnnotations.class);\r
 \r
-        FormBuilder builder = new FormBuilder(binder);\r
-        builder.buildAndBindFields(this);\r
+        fieldGroup.buildAndBindMemberFields(this);\r
         addComponent(firstName);\r
         addComponent(lastName);\r
         addComponent(email);\r
@@ -50,7 +48,7 @@ public class FieldBinderWithBeanValidation extends TestBase {
             public void buttonClick(ClickEvent event) {\r
                 String msg = "Commit succesful";\r
                 try {\r
-                    binder.commit();\r
+                    fieldGroup.commit();\r
                 } catch (CommitException e) {\r
                     msg = "Commit failed: " + e.getMessage();\r
                 }\r
@@ -63,7 +61,7 @@ public class FieldBinderWithBeanValidation extends TestBase {
                 new Button.ClickListener() {\r
 \r
                     public void buttonClick(ClickEvent event) {\r
-                        binder.discard();\r
+                        fieldGroup.discard();\r
                         log.log("Discarded changes");\r
 \r
                     }\r
@@ -72,7 +70,7 @@ public class FieldBinderWithBeanValidation extends TestBase {
                 new Button.ClickListener() {\r
 \r
                     public void buttonClick(ClickEvent event) {\r
-                        log.log(getPerson(binder).toString());\r
+                        log.log(getPerson(fieldGroup).toString());\r
 \r
                     }\r
                 });\r
@@ -84,8 +82,9 @@ public class FieldBinderWithBeanValidation extends TestBase {
         PersonWithBeanValidationAnnotations p = new PersonWithBeanValidationAnnotations(\r
                 "John", "Doe", "john@doe.com", 64, Sex.MALE, new Address(\r
                         "John street", 11223, "John's town", Country.USA));\r
-        binder.setItemDataSource(new BeanItem<PersonWithBeanValidationAnnotations>(\r
-                p));\r
+        fieldGroup\r
+                .setItemDataSource(new BeanItem<PersonWithBeanValidationAnnotations>(\r
+                        p));\r
     }\r
 \r
     public static Person getPerson(FieldGroup binder) {\r
index 284d69aa810bfba5293f5f3d6c893414b95d7cbd..da6a66659036b077ba8dd27d9e85d31d2350ee41 100644 (file)
@@ -2,7 +2,6 @@ package com.vaadin.tests.fieldbinder;
 \r
 import com.vaadin.data.fieldbinder.BeanFieldGroup;\r
 import com.vaadin.data.fieldbinder.FieldGroup;\r
-import com.vaadin.data.fieldbinder.FormBuilder;\r
 import com.vaadin.data.fieldbinder.PropertyId;\r
 import com.vaadin.data.util.BeanItem;\r
 import com.vaadin.tests.components.TestBase;\r
@@ -21,15 +20,14 @@ public class FormBuilderWithNestedProperties extends TestBase {
 \r
     @Override\r
     protected void setup() {\r
-        FieldGroup fieldBinder = new BeanFieldGroup<Person>(Person.class);\r
-        FormBuilder b = new FormBuilder(fieldBinder);\r
-        b.buildAndBindFields(this);\r
+        FieldGroup fieldGroup = new BeanFieldGroup<Person>(Person.class);\r
+        fieldGroup.buildAndBindMemberFields(this);\r
 \r
         addComponent(firstName);\r
         addComponent(lastName);\r
         addComponent(streetAddress);\r
 \r
-        fieldBinder.setItemDataSource(new BeanItem<Person>(new Person("Who",\r
+        fieldGroup.setItemDataSource(new BeanItem<Person>(new Person("Who",\r
                 "me?", "email", 1, Sex.MALE, new Address("street name", 202020,\r
                         "City", Country.FINLAND))));\r
     }\r
index 637cc26a886cc95bfd4cd3a69063ee5df14bf551..9886bc61f514361eb7ce8e4c4a5efcd3389ed6d7 100644 (file)
@@ -1,7 +1,6 @@
 package com.vaadin.tests.fieldbinder;\r
 \r
 import com.vaadin.data.fieldbinder.BeanFieldGroup;\r
-import com.vaadin.data.fieldbinder.FormBuilder;\r
 import com.vaadin.data.fieldbinder.PropertyId;\r
 import com.vaadin.tests.data.bean.Address;\r
 import com.vaadin.tests.data.bean.Country;\r
@@ -32,9 +31,9 @@ public class FormWithNestedProperties extends AbstractBeanFieldBinderTest {
         super.setup();\r
 \r
         setFieldBinder(new BeanFieldGroup<Person>(Person.class));\r
+        country = getFieldBinder().buildAndBind("country", "address.country",\r
+                NativeSelect.class);\r
         getFieldBinder().bindMemberFields(this);\r
-        country = new FormBuilder(getFieldBinder()).buildAndBind("country",\r
-                "address.country", NativeSelect.class);\r
         addComponent(firstName);\r
         addComponent(lastName);\r
         addComponent(streetAddress);\r