Coverage Report - org.apache.tapestry.form.Radio
 
Classes in this File Line Coverage Branch Coverage Complexity
Radio
0% 
0% 
3.333
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.form;
 16  
 
 17  
 import org.apache.hivemind.ApplicationRuntimeException;
 18  
 import org.apache.tapestry.AbstractComponent;
 19  
 import org.apache.tapestry.IMarkupWriter;
 20  
 import org.apache.tapestry.IRequestCycle;
 21  
 import org.apache.tapestry.Tapestry;
 22  
 
 23  
 /**
 24  
  *  Implements a component that manages an HTML <input type=radio> form element.
 25  
  *  Such a component must be wrapped (possibly indirectly)
 26  
  *  inside a {@link RadioGroup} component.
 27  
  *
 28  
  *  [<a href="../../../../../ComponentReference/Radio.html">Component Reference</a>]
 29  
  *
 30  
  * 
 31  
  *  <p>{@link Radio} and {@link RadioGroup} are generally not used (except
 32  
  *  for very special cases).  Instead, a {@link PropertySelection} component is used.
 33  
  *
 34  
  *
 35  
  *  @author Howard Lewis Ship
 36  
  * 
 37  
  **/
 38  
 
 39  0
 public abstract class Radio extends AbstractComponent
 40  
 {
 41  
     /**
 42  
      *  Renders the form element, or responds when the form containing the element
 43  
      *  is submitted (by checking {@link Form#isRewinding()}.
 44  
      *
 45  
      *
 46  
      **/
 47  
 
 48  
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 49  
     {
 50  
 
 51  0
         RadioGroup group = RadioGroup.get(cycle);
 52  
         
 53  0
         if (group == null)
 54  0
             throw new ApplicationRuntimeException(
 55  
                 Tapestry.getMessage("Radio.must-be-contained-by-group"),
 56  
                 this,
 57  
                 null,
 58  
                 null);
 59  
 
 60  
         // The group determines rewinding from the form.
 61  
 
 62  0
         boolean rewinding = group.isRewinding();
 63  
 
 64  0
         int option = group.getNextOptionId();
 65  
 
 66  0
         if (rewinding)
 67  
         {
 68  
             // If not disabled and this is the selected button within the radio group,
 69  
             // then update set the selection from the group to the value for this
 70  
             // radio button.  This will update the selected parameter of the RadioGroup.
 71  
 
 72  0
             if (!isDisabled() && !group.isDisabled() && group.isSelected(option))
 73  0
                 group.updateSelection(getValue());
 74  
             
 75  0
             return;
 76  
         }
 77  
         
 78  0
         setClientId(group.getName()+option);
 79  
         
 80  0
         writer.beginEmpty("input");
 81  
         
 82  0
         writer.attribute("type", "radio");
 83  
         
 84  0
         writer.attribute("name", group.getName());
 85  
         
 86  0
         renderIdAttribute(writer, cycle);
 87  
         
 88  
         // As the group if the value for this Radio matches the selection
 89  
         // for the group as a whole; if so this is the default radio and is checked.
 90  
         
 91  0
         if (group.isSelection(getValue()))
 92  0
             writer.attribute("checked", "checked");
 93  
 
 94  0
         if (isDisabled() || group.isDisabled())
 95  0
             writer.attribute("disabled", "disabled");
 96  
 
 97  
         // The value for the Radio matches the option number (provided by the RadioGroup).
 98  
         // When the form is submitted, the RadioGroup will know which option was,
 99  
         // in fact, selected by the user.
 100  
 
 101  0
         writer.attribute("value", option);
 102  
 
 103  0
         renderInformalParameters(writer, cycle);
 104  
 
 105  0
     }
 106  
 
 107  
     public abstract boolean isDisabled();
 108  
 
 109  
     public abstract Object getValue();
 110  
 }