Coverage Report - org.apache.tapestry.form.Radio
 
Classes in this File Line Coverage Branch Coverage Complexity
Radio
90% 
100% 
2.4
 
 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.IForm;
 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  4
 public abstract class Radio extends AbstractFormComponent
 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 renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 49  
     {
 50  2
         RadioGroup group = RadioGroup.get(cycle);
 51  
 
 52  2
         if (group == null)
 53  0
             throw new ApplicationRuntimeException(Tapestry.getMessage("Radio.must-be-contained-by-group"),
 54  
                                                   this,
 55  
                                                   null,
 56  
                                                   null);
 57  
 
 58  2
         int option = group.getNextOptionId();
 59  
 
 60  2
         setClientId(group.getName()+option);
 61  2
         setName(group.getName());
 62  
 
 63  2
         writer.beginEmpty("input");
 64  
 
 65  2
         writer.attribute("type", "radio");
 66  
 
 67  2
         writer.attribute("name", getName());
 68  
 
 69  2
         renderIdAttribute(writer, cycle);
 70  
 
 71  
         // As the group if the value for this Radio matches the selection
 72  
         // for the group as a whole; if so this is the default radio and is checked.
 73  
 
 74  2
         if (group.isSelection(getValue()))
 75  1
             writer.attribute("checked", "checked");
 76  
 
 77  2
         if (isDisabled() || group.isDisabled())
 78  0
             writer.attribute("disabled", "disabled");
 79  
 
 80  
         // The value for the Radio matches the option number (provided by the RadioGroup).
 81  
         // When the form is submitted, the RadioGroup will know which option was,
 82  
         // in fact, selected by the user.
 83  
 
 84  2
         writer.attribute("value", option);
 85  
 
 86  2
         renderInformalParameters(writer, cycle);
 87  
 
 88  2
         writer.closeTag();
 89  2
     }
 90  
 
 91  
     protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 92  
     {
 93  2
         RadioGroup group = RadioGroup.get(cycle);
 94  
 
 95  2
         if (group == null)
 96  0
             throw new ApplicationRuntimeException(
 97  
                     Tapestry.getMessage("Radio.must-be-contained-by-group"),
 98  
                     this,
 99  
                     null,
 100  
                     null);
 101  
 
 102  2
         int option = group.getNextOptionId();
 103  
 
 104  2
         setClientId(group.getName()+option);
 105  2
         setName(group.getName());
 106  
 
 107  
         // If not disabled and this is the selected button within the radio group,
 108  
         // then update set the selection from the group to the value for this
 109  
         // radio button.  This will update the selected parameter of the RadioGroup.
 110  
 
 111  2
         if (!isDisabled() && !group.isDisabled() && group.isSelected(option))
 112  1
             group.updateSelection(getValue());
 113  2
     }
 114  
 
 115  
     /**
 116  
      * Overridden to do nothing so that special {@link RadioGroup} semantics are handled properly.
 117  
      * @param form The form to set the name on.
 118  
      */
 119  
     protected void setName(IForm form)
 120  
     {
 121  4
     }
 122  
 
 123  
     public abstract boolean isDisabled();
 124  
 
 125  
     public abstract Object getValue();
 126  
 }