]> source.dussan.org Git - vaadin-framework.git/commitdiff
#2205 Make Form layouting more flexible
authorArtur Signell <artur.signell@itmill.com>
Tue, 16 Feb 2010 08:16:34 +0000 (08:16 +0000)
committerArtur Signell <artur.signell@itmill.com>
Tue, 16 Feb 2010 08:16:34 +0000 (08:16 +0000)
Split addField into registerField and attachField to allow overriding.

Added FormAdvancedLayout sample demonstrating the feature.

svn changeset:11326/svn branch:6.3

src/com/vaadin/ui/Form.java

index c6c9a5a738113ceb6ac495c06c9765b9d7eaa7a7..678c323dc3d0dc04c203de0fd21adc7a20842d35 100644 (file)
@@ -479,54 +479,99 @@ public class Form extends AbstractField implements Item.Editor, Buffered, Item,
             return false;
         }
 
+        // Register and attach the created field
         addField(id, field);
 
         return true;
     }
 
     /**
-     * Adds the field to form.
+     * Registers the field with the form and adds the field to the form layout.
      * 
      * <p>
      * The property id must not be already used in the form.
      * </p>
      * 
      * <p>
-     * This field is added to the form layout in the default position (the
-     * position used by {@link Layout#addComponent(Component)} method. In the
-     * special case that the underlying layout is a custom layout, string
-     * representation of the property id is used instead of the default
-     * location.
+     * This field is added to the layout using the
+     * {@link #attachField(Object, Field)} method.
      * </p>
      * 
      * @param propertyId
      *            the Property id the the field.
      * @param field
-     *            the New field added to the form.
+     *            the field which should be added to the form.
      */
     public void addField(Object propertyId, Field field) {
+        registerField(propertyId, field);
+        attachField(propertyId, field);
+        requestRepaint();
+    }
 
-        if (propertyId != null && field != null) {
-            fields.put(propertyId, field);
-            field.addListener(fieldValueChangeListener);
-            if (!propertyIds.contains(propertyId)) {
-                // adding a field directly
-                propertyIds.addLast(propertyId);
-            }
-            field.setReadThrough(readThrough);
-            field.setWriteThrough(writeThrough);
-            if (isImmediate() && field instanceof AbstractComponent) {
-                ((AbstractComponent) field).setImmediate(true);
-            }
-            if (layout instanceof CustomLayout) {
-                ((CustomLayout) layout).addComponent(field, propertyId
-                        .toString());
-            } else {
-                layout.addComponent(field);
-            }
+    /**
+     * Register the field with the form. All registered fields are validated
+     * when the form is validated and also committed when the form is committed.
+     * 
+     * <p>
+     * The property id must not be already used in the form.
+     * </p>
+     * 
+     * 
+     * @param propertyId
+     *            the Property id of the field.
+     * @param field
+     *            the Field that should be registered
+     */
+    private void registerField(Object propertyId, Field field) {
+        if (propertyId == null || field == null) {
+            return;
+        }
 
-            requestRepaint();
+        fields.put(propertyId, field);
+        field.addListener(fieldValueChangeListener);
+        if (!propertyIds.contains(propertyId)) {
+            // adding a field directly
+            propertyIds.addLast(propertyId);
         }
+
+        // Update the read and write through status and immediate to match the
+        // form.
+        // Should this also include invalidCommitted (#3993)?
+        field.setReadThrough(readThrough);
+        field.setWriteThrough(writeThrough);
+        if (isImmediate() && field instanceof AbstractComponent) {
+            ((AbstractComponent) field).setImmediate(true);
+        }
+    }
+
+    /**
+     * Adds the field to the form layout.
+     * <p>
+     * The field is added to the form layout in the default position (the
+     * position used by {@link Layout#addComponent(Component)}. If the
+     * underlying layout is a {@link CustomLayout} the field is added to the
+     * CustomLayout location given by the string representation of the property
+     * id using {@link CustomLayout#addComponent(Component, String)}.
+     * </p>
+     * 
+     * <p>
+     * Override this method to control how the fields are added to the layout.
+     * </p>
+     * 
+     * @param propertyId
+     * @param field
+     */
+    protected void attachField(Object propertyId, Field field) {
+        if (propertyId == null || field == null) {
+            return;
+        }
+
+        if (layout instanceof CustomLayout) {
+            ((CustomLayout) layout).addComponent(field, propertyId.toString());
+        } else {
+            layout.addComponent(field);
+        }
+
     }
 
     /**