Author: andyhot
Date: Tue Nov 28 18:11:11 2006
New Revision: 480334

URL: http://svn.apache.org/viewvc?view=rev&rev=480334
Log:
TAPESTRY-1131: Make ids of form elements unique in page, by prefixing with form 
clientId

Added:
    
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/MultipleFormSupport.java
    
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/MultipleFormSupportFactory.java

Added: 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/MultipleFormSupport.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/MultipleFormSupport.java?view=auto&rev=480334
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/MultipleFormSupport.java
 (added)
+++ 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/MultipleFormSupport.java
 Tue Nov 28 18:11:11 2006
@@ -0,0 +1,68 @@
+// Copyright 2006 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.form;
+
+import org.apache.tapestry.IForm;
+import org.apache.tapestry.IMarkupWriter;
+import org.apache.tapestry.IRequestCycle;
+
+/**
+ * A [EMAIL PROTECTED] FormSupport} implementation that can work when a form is
+ * included multiple times in a given page ( due to it being in a loop
+ * or in a component that's included many times in the page).
+ * <p/>
+ * This is achieved by prefixing the ids of all form elements with the 
+ * form's id.
+ *
+ * @since 4.1.1
+ */
+public class MultipleFormSupport extends FormSupportImpl
+{   
+    /** 
+     * The prefix to use for the form elements. On render, this is the
+     * clientId of the form. On rewind, it's computed from the posted data.
+     */
+    private String _prefix;
+    
+    public MultipleFormSupport(IMarkupWriter writer, IRequestCycle cycle, 
IForm form)
+    {
+        super(writer, cycle, form);
+        _prefix = form.getClientId();
+    }
+    
+    /**
+     * Constructs a unique identifier (within the page). The identifier 
consists of the component's
+     * id, with an index number added to ensure uniqueness.
+     */
+
+    public String getElementId(IFormComponent component, String baseId)
+    {
+        return super.getElementId(component, _prefix + ":" + baseId);        
+    }
+    
+    public String rewind()
+    {
+        findIdPrefix();
+        return super.rewind();
+    }
+    
+    private void findIdPrefix()
+    {
+        String allocatedFormIds = _cycle.getParameter(FORM_IDS);
+        int pos = allocatedFormIds.indexOf(':');
+        if (pos>=0)
+            _prefix = allocatedFormIds.substring(0, pos);
+    }    
+}

Added: 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/MultipleFormSupportFactory.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/MultipleFormSupportFactory.java?view=auto&rev=480334
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/MultipleFormSupportFactory.java
 (added)
+++ 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/MultipleFormSupportFactory.java
 Tue Nov 28 18:11:11 2006
@@ -0,0 +1,37 @@
+// Copyright 2006 The Apache Software Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package org.apache.tapestry.form;
+
+import org.apache.tapestry.IForm;
+import org.apache.tapestry.IMarkupWriter;
+import org.apache.tapestry.IRequestCycle;
+
+/**
+ * Factory class for creating form support to be used with multiple forms.
+ *
+ * @since 4.1.1
+ */
+public class MultipleFormSupportFactory implements FormSupportFactory
+{    
+    public MultipleFormSupportFactory()
+    {
+    }
+
+    public FormSupport createFormSupport(IMarkupWriter writer, IRequestCycle 
cycle, IForm form)
+    {
+        return new MultipleFormSupport(writer, cycle, form);
+    }
+    
+}


Reply via email to