Author: mschulte
Date: Fri Mar 28 12:34:05 2008
New Revision: 642344

URL: http://svn.apache.org/viewvc?rev=642344&view=rev
Log:
fixes TAPESTRY-1039: EventListeners for RadioGroup component

Added:
    tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/context/ajax/
    
tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/context/ajax/AsyncForm.html
    
tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/java/org/apache/tapestry/workbench/ajax/
    
tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/java/org/apache/tapestry/workbench/ajax/AsyncForm.java
    
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/RadioGroup.script
      - copied, changed from r641326, 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/DatePicker.script
Modified:
    
tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/context/WEB-INF/Border.properties
    
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/Radio.java
    
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/RadioGroup.java
    
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/RadioGroup.jwc
    
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/TestRadio.java

Modified: 
tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/context/WEB-INF/Border.properties
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/context/WEB-INF/Border.properties?rev=642344&r1=642343&r2=642344&view=diff
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/context/WEB-INF/Border.properties
 (original)
+++ 
tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/context/WEB-INF/Border.properties
 Fri Mar 28 12:34:05 2008
@@ -14,6 +14,7 @@
 
 Home=Home
 localization/Localization=L10N
+ajax/AsyncForm=Ajax Requests
 Fields=Fields
 Palette=Palette
 Upload=Upload
@@ -27,7 +28,7 @@
 # Order of tabs across the top, space separated
 # These are the page names.
 
-tab-order=Home localization/Localization Fields Dates Chart Palette Upload 
Redirect ExceptionTab Table TreeHome
+tab-order=Home localization/Localization Fields ajax/AsyncForm Dates Chart 
Palette Upload Redirect ExceptionTab Table TreeHome
 
 window.title=Tapestry Component Workbench
 prompt.display-request-debug=Display Request Debug Information

Added: 
tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/context/ajax/AsyncForm.html
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/context/ajax/AsyncForm.html?rev=642344&view=auto
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/context/ajax/AsyncForm.html
 (added)
+++ 
tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/context/ajax/AsyncForm.html
 Fri Mar 28 12:34:05 2008
@@ -0,0 +1,29 @@
+<span jwcid="@Border">
+
+<h2>Intercepting client-side events on the server</h2>
+    <div jwcid="@contrib:AjaxStatus" 
style="display:none;color:white;background:red;position:absolute;top:0;right:0">
+        Loading ...
+    </div>
+    <p>
+        This page shows how to intercept client-side events. Using the 
@EventListener annotation a server-side
+        method can be triggered by virtually any event occuring in the Browser.
+    </p>
+    <p>
+        The following example submits a form asynchronously whenever a radio 
button is clicked.
+        As a result, the yellow phrase at the bottom will be updated.
+    </p>
+
+    <form jwcid="@Form" >
+        <span  jwcid="[EMAIL PROTECTED]" selected="ognl:choice"
+               displayName="Choose" validators="validators:required" >
+            <input jwcid="@Radio" value="Yes" />&nbsp;Yes&nbsp;
+            <input jwcid="@Radio" value="No" />&nbsp;No
+            <input jwcid="@Radio" value="Perhaps" />&nbsp;Perhaps
+        </span>
+    </form>
+    <p jwcid="[EMAIL PROTECTED]" style="background:yellow;">
+       You've chosen
+        <span jwcid="@Insert" value="ognl:choice==null ? 'nothing' : 
'\''+choice+'\''"/>
+    </p>
+
+</span>
\ No newline at end of file

Added: 
tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/java/org/apache/tapestry/workbench/ajax/AsyncForm.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/java/org/apache/tapestry/workbench/ajax/AsyncForm.java?rev=642344&view=auto
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/java/org/apache/tapestry/workbench/ajax/AsyncForm.java
 (added)
+++ 
tapestry/tapestry4/trunk/tapestry-examples/Workbench/src/java/org/apache/tapestry/workbench/ajax/AsyncForm.java
 Fri Mar 28 12:34:05 2008
@@ -0,0 +1,37 @@
+// Copyright 2004, 2008 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.workbench.ajax;
+
+import org.apache.tapestry.html.BasePage;
+import org.apache.tapestry.event.BrowserEvent;
+import org.apache.tapestry.annotations.EventListener;
+
+/**
+ * Page-class demonstrating ajaxy RadioGroups
+ * 
+ */
+public abstract class AsyncForm extends BasePage
+{
+    public abstract String getChoice();
+
+    @EventListener( targets = "choose", events = "onChange" )
+    public void onRadioButtonPressed( BrowserEvent b )
+    {
+        String msg = "You've chosen option " + b.getMethodArguments().get(0) 
+" -> " + getChoice();
+        System.out.println( msg );
+
+        getRequestCycle().getResponseBuilder().updateComponent("ajaxResponse");
+    }
+}

Modified: 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/Radio.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/Radio.java?rev=642344&r1=642343&r2=642344&view=diff
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/Radio.java
 (original)
+++ 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/Radio.java
 Fri Mar 28 12:34:05 2008
@@ -83,6 +83,13 @@
 
         writer.attribute("value", option);
 
+        // don't make it a reserved parameter to preserve backwards 
compatibility
+        if ( !isParameterBound("onclick") )
+        {
+            String onclickCall = 
"tapestry.byId('"+group.getClientId()+"').onChange("+option+");";
+            writer.attribute("onclick", onclickCall);
+        }
+
         renderInformalParameters(writer, cycle);
 
         writer.closeTag();

Modified: 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/RadioGroup.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/RadioGroup.java?rev=642344&r1=642343&r2=642344&view=diff
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/RadioGroup.java
 (original)
+++ 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/RadioGroup.java
 Fri Mar 28 12:34:05 2008
@@ -15,11 +15,12 @@
 package org.apache.tapestry.form;
 
 import org.apache.hivemind.ApplicationRuntimeException;
-import org.apache.tapestry.IMarkupWriter;
-import org.apache.tapestry.IRequestCycle;
-import org.apache.tapestry.Tapestry;
+import org.apache.tapestry.*;
 import org.apache.tapestry.valid.ValidatorException;
 
+import java.util.Map;
+import java.util.HashMap;
+
 /**
  * A special type of form component that is used to contain [EMAIL PROTECTED] 
Radio}components. The Radio and
  * [EMAIL PROTECTED] Radio}group components work together to update a property 
of some other object, much like
@@ -53,6 +54,11 @@
 
     private int _nextOptionId;
 
+    /** A script providing a method onChange to be called whenever one of the 
enclosed radio-buttons is
+     * clicked 
+     */
+    public abstract IScript getScript();
+
     public static RadioGroup get(IRequestCycle cycle)
     {
         return (RadioGroup) cycle.getAttribute(ATTRIBUTE_NAME);
@@ -145,13 +151,19 @@
     }
 
     /**
-     * @see 
org.apache.tapestry.form.AbstractRequirableField#renderFormComponent(org.apache.tapestry.IMarkupWriter,
+     * @see 
org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter,
      *      org.apache.tapestry.IRequestCycle)
      */
     protected void renderFormComponent(IMarkupWriter writer, IRequestCycle 
cycle)
     {
         _rewinding = false;
 
+        // render script generating the onChange method
+        PageRenderSupport pageRenderSupport = 
TapestryUtils.getPageRenderSupport(cycle, this);
+        Map symbols = new HashMap();
+        symbols.put( "id", getClientId() );
+        getScript().execute(this, cycle, pageRenderSupport, symbols);
+
         // For rendering, the Radio components need to know what the current
         // selection is, so that the correct one can mark itself 'checked'.
         _selection = getBinding("selected").getObject();
@@ -161,6 +173,9 @@
         writer.begin(getTemplateTagName());
 
         renderInformalParameters(writer, cycle);
+        
+        if (getId() != null && !isParameterBound("id"))
+                renderIdAttribute(writer, cycle);
 
         renderDelegateAttributes(writer, cycle);
 

Modified: 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/RadioGroup.jwc
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/RadioGroup.jwc?rev=642344&r1=642343&r2=642344&view=diff
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/RadioGroup.jwc
 (original)
+++ 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/RadioGroup.jwc
 Fri Mar 28 12:34:05 2008
@@ -33,5 +33,5 @@
   <parameter name="validators"/>
   
   <inject property="validatableFieldSupport" 
object="service:tapestry.form.ValidatableFieldSupport"/>
-    
+  <inject property="script" type="script" object="RadioGroup.script"/>
 </component-specification>

Copied: 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/RadioGroup.script
 (from r641326, 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/DatePicker.script)
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/RadioGroup.script?p2=tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/RadioGroup.script&p1=tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/DatePicker.script&r1=641326&r2=642344&rev=642344&view=diff
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/DatePicker.script
 (original)
+++ 
tapestry/tapestry4/trunk/tapestry-framework/src/java/org/apache/tapestry/form/RadioGroup.script
 Fri Mar 28 12:34:05 2008
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <!-- 
-   Copyright 2005 The Apache Software Foundation
+   Copyright 2008 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.
@@ -20,53 +20,22 @@
        "http://tapestry.apache.org/dtd/Script_3_0.dtd";>
   
 <script>
-
-<include-script resource-path="DatePicker.js"/>
-
-<input-symbol key="name"  class="java.lang.String" required="yes"/>
-<input-symbol key="formName" class="java.lang.String" required="yes"/>
-<input-symbol key="monthNames"  required="yes"/>
-<input-symbol key="shortMonthNames" required="yes"/>
-<input-symbol key="weekDayNames" required="yes"/>
-<input-symbol key="shortWeekDayNames"  required="yes"/>
-<input-symbol key="firstDayInWeek" required="yes"/>
-<input-symbol key="minimalDaysInFirstWeek" required="yes"/>
-<input-symbol key="format" required="yes"/>
-<input-symbol key="includeWeek" required="yes"/>
-<input-symbol key="clearButtonLabel" required="yes"/>
-<input-symbol key="value" required="no"/>
-
-<let key="calendarObject" unique="yes">
-       calendar_${name}        
-</let>
-
-<let key="buttonOnclickHandler">
-  javascript:${calendarObject}.toggle(tapestry.byId("${name}"));       
-</let>
-
+  <!--A script providing a method onChange to a RadioGroup which is to be 
called whenever one of
+      the enclosed radio-buttons is clicked -->
+    <input-symbol key="id"  class="java.lang.String" required="yes"/>
+
+    <let key="radioGroupElem" unique="yes">
+        radioGroup_${id}
+    </let>
 <body>
-var ${calendarObject};
 </body>
 
-<initialization>       
-<if expression="value == null">
-${calendarObject} = new Calendar();
-</if>
-<if expression="value != null">
-${calendarObject} = new Calendar(${value.time});
-</if>  
-${calendarObject}.initialize([${monthNames}],
-  [${shortMonthNames}],
-  [${weekDayNames}],
-  [${shortWeekDayNames}],
-  "${format}", ${firstDayInWeek}, ${includeWeek}, ${minimalDaysInFirstWeek}, 
"${clearButtonLabel}");
-${calendarObject}.onchange = function() {
-  var field = tapestry.byId("${formName}").${name};
-  var value = ${calendarObject}.formatDate();
-       if (field.value != value) {
-    field.value = value;
-    if (field.onchange) { field.onchange();}
-  }
-}
+<initialization>
+    var ${radioGroupElem} = tapestry.byId("${id}");
+
+    if ( ! ${radioGroupElem}.onChange )
+    {
+        ${radioGroupElem}.onChange = function( value ) {/* do nothing */ };
+    }
 </initialization>
 </script>

Modified: 
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/TestRadio.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/TestRadio.java?rev=642344&r1=642343&r2=642344&view=diff
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/TestRadio.java
 (original)
+++ 
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/TestRadio.java
 Fri Mar 28 12:34:05 2008
@@ -27,7 +27,7 @@
 
                verify();
 
-               assertBuffer("<input type=\"radio\" name=\"group\" 
id=\"group0\" checked=\"checked\" value=\"0\" />");
+               assertBuffer("<input type=\"radio\" name=\"group\" 
id=\"group0\" checked=\"checked\" value=\"0\" 
onclick=\"tapestry.byId('group').onChange(0);\" />");
        }
 
        public void test_Render_Unselected()
@@ -40,7 +40,7 @@
 
                verify();
 
-               assertBuffer("<input type=\"radio\" name=\"group\" 
id=\"group0\" value=\"0\" />");
+               assertBuffer("<input type=\"radio\" name=\"group\" 
id=\"group0\" value=\"0\" onclick=\"tapestry.byId('group').onChange(0);\" />");
        }
 
        public void test_Rewind_Selected()
@@ -74,7 +74,8 @@
        {
                group = newInstance(RadioGroup.class);
                group.setName("group");
-               group._rendering = true;
+        group.setClientId("group");
+        group._rendering = true;
                group._rewinding = rewinding;
                group._selection = selection;
                group._selectedOption = selection;


Reply via email to