From 8db51070931f7f0a36f33aadef65a85b29d90f4f Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 16 Feb 2010 08:16:34 +0000 Subject: [PATCH] #2205 Make Form layouting more flexible 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 | 97 +++++++++++++++++++++++++++---------- 1 file changed, 71 insertions(+), 26 deletions(-) diff --git a/src/com/vaadin/ui/Form.java b/src/com/vaadin/ui/Form.java index c6c9a5a738..678c323dc3 100644 --- a/src/com/vaadin/ui/Form.java +++ b/src/com/vaadin/ui/Form.java @@ -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. * *

* The property id must not be already used in the form. *

* *

- * 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. *

* * @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. + * + *

+ * The property id must not be already used in the form. + *

+ * + * + * @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. + *

+ * 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)}. + *

+ * + *

+ * Override this method to control how the fields are added to the layout. + *

+ * + * @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); + } + } /** -- 2.39.5