You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by sa...@apache.org on 2009/05/27 18:18:47 UTC

svn commit: r779229 - /incubator/click/trunk/click/framework/src/org/apache/click/control/Form.java

Author: sabob
Date: Wed May 27 16:18:47 2009
New Revision: 779229

URL: http://svn.apache.org/viewvc?rev=779229&view=rev
Log:
improved Form to track the insert offset of the two HiddenFields which Form adds. CLK-447

Modified:
    incubator/click/trunk/click/framework/src/org/apache/click/control/Form.java

Modified: incubator/click/trunk/click/framework/src/org/apache/click/control/Form.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/framework/src/org/apache/click/control/Form.java?rev=779229&r1=779228&r2=779229&view=diff
==============================================================================
--- incubator/click/trunk/click/framework/src/org/apache/click/control/Form.java (original)
+++ incubator/click/trunk/click/framework/src/org/apache/click/control/Form.java Wed May 27 16:18:47 2009
@@ -626,6 +626,12 @@
     /** The label <td> "style" attribute value. */
     protected String labelStyle;
 
+    /**
+     * Track the index offset when adding Controls. This ensures HiddenFields
+     * added by Form does not interfere with Controls added by users.
+     */
+    private int insertIndexOffset;
+
     // ----------------------------------------------------------- Constructors
 
     /**
@@ -687,7 +693,10 @@
      */
     public Control insert(Control control, int index) {
 
-        super.insert(control, index);
+        // Adjust index for hidden fields added by Form. CLK-447
+        int realIndex = Math.min(index, getControls().size() - insertIndexOffset);
+
+        super.insert(control, realIndex);
 
         if (control instanceof Field) {
             Field field = (Field) control;
@@ -697,7 +706,9 @@
                 getButtonList().add(field);
 
             } else {
-                getFieldList().add(field);
+                // Adjust index for hidden fields added by Form
+                realIndex = Math.min(index, getFieldList().size() - insertIndexOffset);
+                getFieldList().add(realIndex, field);
             }
 
             field.setForm(this);
@@ -1151,18 +1162,9 @@
 
         HiddenField nameField = (HiddenField) getField(FORM_NAME);
         if (nameField == null) {
-
-            nameField = new HiddenField(FORM_NAME, String.class) {
-
-                // Override setName to ensure name cannot be changed once set
-                public void setName(String name) {
-                    if (this.name != null) {
-                        return;
-                    }
-                    super.setName(name);
-                }
-            };
+            nameField = new ImmutableNameHiddenField(FORM_NAME, String.class);
             add(nameField);
+            insertIndexOffset++;
         }
         nameField.setValue(name);
     }
@@ -2154,18 +2156,9 @@
         // CLK-267: check against adding a duplicate field
         HiddenField field = (HiddenField) getField(submitTokenName);
         if (field == null) {
-
-            field = new HiddenField(submitTokenName, Long.class) {
-
-                // Override setName to ensure name cannot be changed once set
-                public void setName(String name) {
-                    if (this.name != null) {
-                        return;
-                    }
-                    super.setName(name);
-                }
-            };
+            field = new ImmutableNameHiddenField(submitTokenName, Long.class);
             add(field);
+            insertIndexOffset++;
         }
 
         // Save state info to form and session
@@ -2720,4 +2713,34 @@
             return ((Field) control).isHidden();
         }
     }
+
+    // ---------------------------------------------------------- Inner Classes
+
+    /**
+     * Provides a HiddenField which name cannot be changed, once it is set.
+     */
+    private class ImmutableNameHiddenField extends HiddenField {
+
+        /**
+         * Create a field with the given name and value.
+         *
+         * @param name the field name
+         * @param valueClass the Class of the value Object
+         */
+        public ImmutableNameHiddenField(String name, Class valueClass) {
+            super(name, valueClass);
+        }
+
+        /**
+         * Set the field name. The field name cannot be changed once it is set.
+         *
+         * @param name the name of the field
+         */
+        public void setName(String name) {
+            if (this.name != null) {
+                return;
+            }
+            super.setName(name);
+        }
+    }
 }