Author: bdotte
Date: Tue Jan 22 12:41:56 2008
New Revision: 614315

URL: http://svn.apache.org/viewvc?rev=614315&view=rev
Log:
Fixes TAPESTRY-1529. Make Radio extend from AbstractFormComponent instead of 
AbstractComponent so it can be used with FieldLabel.

Added:
    
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/TestRadio.java
Modified:
    
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

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=614315&r1=614314&r2=614315&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
 Tue Jan 22 12:41:56 2008
@@ -15,7 +15,6 @@
 package org.apache.tapestry.form;
 
 import org.apache.hivemind.ApplicationRuntimeException;
-import org.apache.tapestry.AbstractComponent;
 import org.apache.tapestry.IMarkupWriter;
 import org.apache.tapestry.IRequestCycle;
 import org.apache.tapestry.Tapestry;
@@ -36,7 +35,7 @@
  * 
  **/
 
-public abstract class Radio extends AbstractComponent
+public abstract class Radio extends AbstractFormComponent
 {
     /**
      *  Renders the form element, or responds when the form containing the 
element
@@ -45,35 +44,18 @@
      *
      **/
 
-    protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
+    protected void renderFormComponent(IMarkupWriter writer, IRequestCycle 
cycle)
     {
+           RadioGroup group = RadioGroup.get(cycle);
 
-        RadioGroup group = RadioGroup.get(cycle);
-        
-        if (group == null)
+           if (group == null)
             throw new ApplicationRuntimeException(
                 Tapestry.getMessage("Radio.must-be-contained-by-group"),
                 this,
                 null,
                 null);
-
-        // The group determines rewinding from the form.
-
-        boolean rewinding = group.isRewinding();
-
-        int option = group.getNextOptionId();
-
-        if (rewinding)
-        {
-            // If not disabled and this is the selected button within the 
radio group,
-            // then update set the selection from the group to the value for 
this
-            // radio button.  This will update the selected parameter of the 
RadioGroup.
-
-            if (!isDisabled() && !group.isDisabled() && 
group.isSelected(option))
-                group.updateSelection(getValue());
-            
-            return;
-        }
+           
+           int option = group.getNextOptionId();
         
         setClientId(group.getName()+option);
         
@@ -102,9 +84,31 @@
 
         renderInformalParameters(writer, cycle);
 
+           writer.closeTag();
     }
 
-    public abstract boolean isDisabled();
+       protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle 
cycle)
+       {
+               RadioGroup group = RadioGroup.get(cycle);
+
+        if (group == null)
+            throw new ApplicationRuntimeException(
+                Tapestry.getMessage("Radio.must-be-contained-by-group"),
+                this,
+                null,
+                null);
+
+        int option = group.getNextOptionId();
+
+               // If not disabled and this is the selected button within the 
radio group,
+               // then update set the selection from the group to the value 
for this
+               // radio button.  This will update the selected parameter of 
the RadioGroup.
+
+               if (!isDisabled() && !group.isDisabled() && 
group.isSelected(option))
+                       group.updateSelection(getValue());
+       }
+
+       public abstract boolean isDisabled();
 
     public abstract Object getValue();
 }

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=614315&r1=614314&r2=614315&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
 Tue Jan 22 12:41:56 2008
@@ -38,18 +38,18 @@
      * that its wrapped [EMAIL PROTECTED] Radio}components can identify thier 
state.
      */
 
-    private static final String ATTRIBUTE_NAME = 
"org.apache.tapestry.active.RadioGroup";
+    static final String ATTRIBUTE_NAME = 
"org.apache.tapestry.active.RadioGroup";
     
     // Cached copy of the value from the selectedBinding
-    private Object _selection;
+    Object _selection;
 
     // The value from the HTTP request indicating which
     // Radio was selected by the user.
-    private int _selectedOption;
+    int _selectedOption;
 
-    private boolean _rewinding;
+    boolean _rewinding;
 
-    private boolean _rendering;
+    boolean _rendering;
 
     private int _nextOptionId;
 

Added: 
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=614315&view=auto
==============================================================================
--- 
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/TestRadio.java
 (added)
+++ 
tapestry/tapestry4/trunk/tapestry-framework/src/test/org/apache/tapestry/form/TestRadio.java
 Tue Jan 22 12:41:56 2008
@@ -0,0 +1,107 @@
+package org.apache.tapestry.form;
+
+import org.apache.tapestry.IBinding;
+import org.apache.tapestry.IForm;
+import org.apache.tapestry.IMarkupWriter;
+import org.apache.tapestry.IRequestCycle;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
+import org.testng.annotations.Test;
+
[EMAIL PROTECTED]
+public class TestRadio extends BaseFormComponentTestCase
+{
+       private Radio radio;
+       private RadioGroup group;
+
+       private IMarkupWriter writer;
+       private IRequestCycle cycle;
+
+       public void testRenderSelected()
+       {
+               setupComponent(0, false);
+
+               replay();
+
+               radio.render(writer, cycle);
+
+               verify();
+
+               assertBuffer("<input type=\"radio\" name=\"group\" 
id=\"group0\" checked=\"checked\" value=\"0\" />");
+       }
+
+       public void testRenderUnselected()
+       {
+               setupComponent(1, false);
+
+               replay();
+
+               radio.render(writer, cycle);
+
+               verify();
+
+               assertBuffer("<input type=\"radio\" name=\"group\" 
id=\"group0\" value=\"0\" />");
+       }
+
+       public void testRewindSelected()
+       {
+               setupComponent(0, true);
+
+               IBinding binding = newMock(IBinding.class);
+        group.setBinding("selected", binding);
+               binding.setObject(0);
+               expectLastCall();
+
+               replay();
+
+               radio.render(writer, cycle);
+
+               verify();
+       }
+
+       public void testRewindUnselected()
+       {
+               setupComponent(1, true);
+
+               replay();
+
+               radio.render(writer, cycle);
+
+               verify();
+       }
+
+       private void setupComponent(int selection, boolean rewinding)
+       {
+               group = newInstance(RadioGroup.class);
+               group.setName("group");
+               group._rendering = true;
+               group._rewinding = rewinding;
+               group._selection = selection;
+               group._selectedOption = selection;
+               radio = newInstance(Radio.class,
+                                                         "value", 0);
+
+               writer = newBufferWriter();
+               cycle = newCycle();
+               IForm form = newMock(IForm.class);
+               MockDelegate delegate = new MockDelegate();
+
+               
expect(cycle.getAttribute(RadioGroup.ATTRIBUTE_NAME)).andReturn(group);
+
+               expect(cycle.renderStackPush(radio)).andReturn(radio);
+
+               trainGetForm(cycle, form);
+               trainWasPrerendered(form, writer, radio, false);
+               trainGetDelegate(form, delegate);
+
+               trainGetElementId(form, radio, "id");
+               trainIsRewinding(form, rewinding);
+               if (!rewinding)
+               {
+                       trainIsRewinding(cycle, rewinding);
+                       form.setFormFieldUpdating(true);
+               }
+
+               expect(cycle.renderStackPop()).andReturn(radio);
+       }
+}


Reply via email to