You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tapestry.apache.org by hl...@apache.org on 2010/06/25 02:06:22 UTC

svn commit: r957766 - in /tapestry/tapestry5/trunk: src/site/apt/ tapestry-core/src/main/java/org/apache/tapestry5/ tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/ tapestry-core/src/main/java/org/apache/tapestry5/internal/transform...

Author: hlship
Date: Fri Jun 25 00:06:22 2010
New Revision: 957766

URL: http://svn.apache.org/viewvc?rev=957766&view=rev
Log:
TAP5-1190: Allow the active page to pre-allocate form names to ensure that there aren't conflicts between form controls and mapped query parameters

Modified:
    tapestry/tapestry5/trunk/src/site/apt/index.apt
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/EventConstants.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Form.java
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/QueryParameterMappedWorker.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/app1/QueryParameterMappedDemo.tml
    tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/QueryParameterMappedTests.groovy
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/QueryParameterMappedDemo.java

Modified: tapestry/tapestry5/trunk/src/site/apt/index.apt
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/src/site/apt/index.apt?rev=957766&r1=957765&r2=957766&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/src/site/apt/index.apt (original)
+++ tapestry/tapestry5/trunk/src/site/apt/index.apt Fri Jun 25 00:06:22 2010
@@ -34,6 +34,9 @@ What is Apache Tapestry?
 
 New And Of Note
 
+  * New {{{apidocs/org/apache/tapestry5/annotations/QueryParameterMapped.html}QueryParameterMapped}} annotation to
+    automatically map fields of a page to query parameters.
+  
   * Service <implementation> classes may now be {{{tapestry-ioc/reload.html}live reloaded}}, much like page and component classes. 
 
   * New {{{apidocs/org/apache/tapestry5/annotations/QueryParameter.html}QueryParameter}} annotation for parameters to event handler methods.

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/EventConstants.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/EventConstants.java?rev=957766&r1=957765&r2=957766&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/EventConstants.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/EventConstants.java Fri Jun 25 00:06:22 2010
@@ -15,7 +15,9 @@
 package org.apache.tapestry5;
 
 import org.apache.tapestry5.corelib.components.BeanEditForm;
+import org.apache.tapestry5.ioc.util.IdAllocator;
 import org.apache.tapestry5.services.ComponentEventRequestParameters;
+import org.apache.tapestry5.services.ComponentSource;
 import org.apache.tapestry5.services.PageRenderRequestParameters;
 
 /**
@@ -210,4 +212,13 @@ public class EventConstants
      */
     public static final String DECORATE_COMPONENT_EVENT_LINK = "decoreateComponentEventLink";
 
+    /**
+     * Name of a event triggered by the form component on the {@linkplain ComponentSource#getActivePage() active page}
+     * to allow it to pre-allocate the names of any query parameters that might be used by the page for its own purposes
+     * and should not be allocated to components. An {@link IdAllocator} is passed as the event context.
+     * 
+     * @since 5.2.0
+     */
+    public static final String PREALLOCATE_FORM_CONTROL_NAMES = "preallocateFormControlNames";
+
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Form.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Form.java?rev=957766&r1=957765&r2=957766&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Form.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Form.java Fri Jun 25 00:06:22 2010
@@ -363,10 +363,7 @@ public class Form implements ClientEleme
 
         IdAllocator allocator = new IdAllocator();
 
-        for (String name : TapestryInternalUtils.splitAtCommas(preselectedFormNames))
-        {
-            allocator.allocateId(name);
-        }
+        preallocateNames(allocator);
 
         formSupport = createRenderTimeFormSupport(clientId, actionSink, allocator);
 
@@ -704,4 +701,20 @@ public class Form implements ClientEleme
     {
         return clientId;
     }
+
+    @Inject
+    private ComponentSource componentSource;
+
+    private void preallocateNames(IdAllocator idAllocator)
+    {
+        for (String name : TapestryInternalUtils.splitAtCommas(preselectedFormNames))
+        {
+            idAllocator.allocateId(name);
+        }
+
+        ComponentResources activePageResources = componentSource.getActivePage().getComponentResources();
+
+        activePageResources.triggerEvent(EventConstants.PREALLOCATE_FORM_CONTROL_NAMES, new Object[]
+        { idAllocator }, null);
+    }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/QueryParameterMappedWorker.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/QueryParameterMappedWorker.java?rev=957766&r1=957765&r2=957766&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/QueryParameterMappedWorker.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/internal/transform/QueryParameterMappedWorker.java Fri Jun 25 00:06:22 2010
@@ -19,6 +19,7 @@ import org.apache.tapestry5.Link;
 import org.apache.tapestry5.ValueEncoder;
 import org.apache.tapestry5.annotations.QueryParameterMapped;
 import org.apache.tapestry5.internal.services.ComponentClassCache;
+import org.apache.tapestry5.ioc.util.IdAllocator;
 import org.apache.tapestry5.model.MutableComponentModel;
 import org.apache.tapestry5.runtime.Component;
 import org.apache.tapestry5.runtime.ComponentEvent;
@@ -82,6 +83,22 @@ public class QueryParameterMappedWorker 
 
         setValueFromInitializeEventHandler(access, parameterName, encoder, dispatchMethod, model);
         decorateLinks(access, parameterName, encoder, dispatchMethod, model);
+        preallocateName(parameterName, dispatchMethod, model);
+    }
+
+    private void preallocateName(final String parameterName, TransformMethod dispatchMethod, MutableComponentModel model)
+    {
+        EventHandler handler = new EventHandler()
+        {
+            public void invoke(Component component, ComponentEvent event)
+            {
+                IdAllocator idAllocator = event.getEventContext().get(IdAllocator.class, 0);
+
+                idAllocator.allocateId(parameterName);
+            }
+        };
+
+        add(dispatchMethod, model, EventConstants.PREALLOCATE_FORM_CONTROL_NAMES, handler);
     }
 
     @SuppressWarnings("unchecked")
@@ -144,7 +161,7 @@ public class QueryParameterMappedWorker 
                 {
                     handler.invoke(invocation.getInstance(), event);
                 }
-                
+
                 invocation.proceed();
             }
         });

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/app1/QueryParameterMappedDemo.tml
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/app1/QueryParameterMappedDemo.tml?rev=957766&r1=957765&r2=957766&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/app1/QueryParameterMappedDemo.tml (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/app1/QueryParameterMappedDemo.tml Fri Jun 25 00:06:22 2010
@@ -4,9 +4,11 @@
 
   <dl>
     <dt>Click count:</dt>
-    <dd id="clickCount">${clickCount}</dd>
+    <dd id="click-count">${clickCount}</dd>
     <dt>Click count set:</dt>
-    <dd id="clickCountSet">${clickCountSet}</dd>
+    <dd id="click-count-set">${clickCountSet}</dd>
+    <dt>Selected click count (via form):</dt>
+    <dd id="selected-click-count">${selectedClickCount}</dd>
     <dt>Message:</dt>
     <dd id="message">${message}</dd>
   </dl>
@@ -22,4 +24,12 @@
       <t:actionlink t:id="reset">reset</t:actionlink>
     </li>
   </ul>
+  
+  <t:form>
+    <t:label for="clickCount">
+    <!-- See, it matches the query parameter name. -->
+    <t:select t:id="clickCount" value="selectedClickCount" model="clickCountModel"/>
+    <input type="submit" value="Update"/>
+    </t:label>
+  </t:form>
 </html>
\ No newline at end of file

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/QueryParameterMappedTests.groovy
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/QueryParameterMappedTests.groovy?rev=957766&r1=957765&r2=957766&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/QueryParameterMappedTests.groovy (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/groovy/org/apache/tapestry5/integration/app1/QueryParameterMappedTests.groovy Fri Jun 25 00:06:22 2010
@@ -14,29 +14,44 @@
 
 package org.apache.tapestry5.integration.app1;
 
+import org.apache.tapestry5.corelib.components.Submit;
 import org.apache.tapestry5.integration.TapestryCoreTestCase 
 import org.testng.annotations.Test 
 
 class QueryParameterMappedTests extends TapestryCoreTestCase
 {
     @Test
-    void basic_links()
-    {
+    void basic_links() {
         clickThru "@QueryParameterMapped Demo"
         
-        assertText("clickCount", "")
-        assertText("clickCountSet", "false")
-        assertText("message", "")
+        assertText "click-count", ""
+        assertText "click-count-set", "false"
+        assertText "message", ""
         
         clickAndWait "link=increment count"
         
-        assertText("clickCount", "1")
-        assertText("clickCountSet", "true")
+        assertText "click-count", "1"
+        assertText "click-count-set", "true"
         
         clickAndWait "link=set message"
         
-        assertText("clickCount", "1")
-        assertText("clickCountSet", "true")
-        assertText("message", "Link clicked!")        
+        assertText "click-count", "1"
+        assertText "click-count-set", "true"
+        assertText "message", "Link clicked!"        
+    }
+    
+    @Test
+    public void form_components_do_not_conflict_with_mapped_field_names() {
+        
+        clickThru "@QueryParameterMapped Demo"
+        
+        clickAndWait "link=increment count"
+        
+        select "clickCount", "two"
+        
+        clickAndWait SUBMIT
+        
+        assertText "click-count", "1"
+        assertText "selected-click-count", "2"        
     }
 }

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/QueryParameterMappedDemo.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/QueryParameterMappedDemo.java?rev=957766&r1=957765&r2=957766&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/QueryParameterMappedDemo.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/integration/app1/pages/QueryParameterMappedDemo.java Fri Jun 25 00:06:22 2010
@@ -14,28 +14,38 @@
 
 package org.apache.tapestry5.integration.app1.pages;
 
+import org.apache.tapestry5.OptionModel;
+import org.apache.tapestry5.PersistenceConstants;
+import org.apache.tapestry5.SelectModel;
+import org.apache.tapestry5.annotations.Persist;
 import org.apache.tapestry5.annotations.Property;
 import org.apache.tapestry5.annotations.QueryParameterMapped;
+import org.apache.tapestry5.internal.OptionModelImpl;
+import org.apache.tapestry5.internal.SelectModelImpl;
 
 public class QueryParameterMappedDemo
 {
-    
+
+    @Property
+    @Persist(PersistenceConstants.FLASH)
+    private Integer selectedClickCount;
+
     @Property
     private boolean clickCountSet;
-    
+
     @Property
     @QueryParameterMapped
     private Integer clickCount;
 
     @Property
-    @QueryParameterMapped
+    @QueryParameterMapped("status-message")
     private String message;
 
     void onActivate()
     {
         clickCountSet = clickCount != null;
     }
-    
+
     void onActionFromIncrement()
     {
         clickCount = clickCount == null ? 1 : clickCount + 1;
@@ -51,4 +61,13 @@ public class QueryParameterMappedDemo
         clickCount = null;
         message = null;
     }
+
+    public SelectModel getClickCountModel()
+    {
+        OptionModel one = new OptionModelImpl("one", 1);
+        OptionModel two = new OptionModelImpl("two", 2);
+        OptionModel three = new OptionModelImpl("three", 3);
+
+        return new SelectModelImpl(one, two, three);
+    }
 }