Coverage Report - org.apache.tapestry.form.RadioGroup
 
Classes in this File Line Coverage Branch Coverage Complexity
RadioGroup
24% 
62% 
2.077
 
 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.IMarkupWriter;
 19  
 import org.apache.tapestry.IRequestCycle;
 20  
 import org.apache.tapestry.Tapestry;
 21  
 import org.apache.tapestry.valid.ValidatorException;
 22  
 
 23  
 /**
 24  
  * A special type of form component that is used to contain {@link Radio}components. The Radio and
 25  
  * {@link Radio}group components work together to update a property of some other object, much like
 26  
  * a more flexible version of a {@link PropertySelection}. [ <a
 27  
  * href="../../../../../ComponentReference/RadioGroup.html">Component Reference </a>]
 28  
  * <p>
 29  
  * As of 4.0, this component can be validated.
 30  
  *
 31  
  * @author Howard Lewis Ship
 32  
  * @author Paul Ferraro
 33  
  */
 34  4
 public abstract class RadioGroup extends AbstractFormComponent implements ValidatableField
 35  
 {
 36  
     /**
 37  
      * A <code>RadioGroup</code> places itself into the {@link IRequestCycle}as an attribute, so
 38  
      * that its wrapped {@link Radio}components can identify thier state.
 39  
      */
 40  
 
 41  
     static final String ATTRIBUTE_NAME = "org.apache.tapestry.active.RadioGroup";
 42  
 
 43  
     // Cached copy of the value from the selectedBinding
 44  
     Object _selection;
 45  
 
 46  
     // The value from the HTTP request indicating which
 47  
     // Radio was selected by the user.
 48  
     int _selectedOption;
 49  
 
 50  
     boolean _rewinding;
 51  
 
 52  
     boolean _rendering;
 53  
 
 54  
     private int _nextOptionId;
 55  
 
 56  
     public static RadioGroup get(IRequestCycle cycle)
 57  
     {
 58  4
         return (RadioGroup) cycle.getAttribute(ATTRIBUTE_NAME);
 59  
     }
 60  
 
 61  
     public int getNextOptionId()
 62  
     {
 63  4
         if (!_rendering)
 64  0
             throw Tapestry.createRenderOnlyPropertyException(this, "nextOptionId");
 65  
 
 66  4
         return _nextOptionId++;
 67  
     }
 68  
 
 69  
     public boolean isRewinding()
 70  
     {
 71  0
         if (!_rendering)
 72  0
             throw Tapestry.createRenderOnlyPropertyException(this, "rewinding");
 73  
 
 74  0
         return _rewinding;
 75  
     }
 76  
 
 77  
     /**
 78  
      * Returns true if the value is equal to the current selection for the group. This is invoked by
 79  
      * a {@link Radio}during rendering to determine if it should be marked 'checked'.
 80  
      */
 81  
 
 82  
     public boolean isSelection(Object value)
 83  
     {
 84  2
         if (!_rendering)
 85  0
             throw Tapestry.createRenderOnlyPropertyException(this, "selection");
 86  
 
 87  2
         if (_selection == value)
 88  1
             return true;
 89  
 
 90  1
         if (_selection == null || value == null)
 91  0
             return false;
 92  
 
 93  1
         return _selection.equals(value);
 94  
     }
 95  
 
 96  
     /**
 97  
      * Invoked by the {@link Radio}which is selected to update the property bound to the selected
 98  
      * parameter.
 99  
      */
 100  
 
 101  
     public void updateSelection(Object value)
 102  
     {
 103  1
         getBinding("selected").setObject(value);
 104  
 
 105  1
         _selection = value;
 106  1
     }
 107  
 
 108  
     /**
 109  
      * Used by {@link Radio}components when rewinding to see if their value was submitted.
 110  
      */
 111  
 
 112  
     public boolean isSelected(int option)
 113  
     {
 114  2
         return _selectedOption == option;
 115  
     }
 116  
 
 117  
     /**
 118  
      * @see org.apache.tapestry.AbstractComponent#prepareForRender(org.apache.tapestry.IRequestCycle)
 119  
      */
 120  
     protected void prepareForRender(IRequestCycle cycle)
 121  
     {
 122  0
         super.prepareForRender(cycle);
 123  
 
 124  0
         if (cycle.getAttribute(ATTRIBUTE_NAME) != null)
 125  0
             throw new ApplicationRuntimeException(Tapestry.getMessage("RadioGroup.may-not-nest"),
 126  
                                                   this, null, null);
 127  
 
 128  0
         cycle.setAttribute(ATTRIBUTE_NAME, this);
 129  
 
 130  0
         _rendering = true;
 131  0
         _nextOptionId = 0;
 132  0
     }
 133  
 
 134  
     /**
 135  
      * @see org.apache.tapestry.AbstractComponent#cleanupAfterRender(org.apache.tapestry.IRequestCycle)
 136  
      */
 137  
     protected void cleanupAfterRender(IRequestCycle cycle)
 138  
     {
 139  0
         super.cleanupAfterRender(cycle);
 140  
 
 141  0
         _rendering = false;
 142  0
         _selection = null;
 143  
 
 144  0
         cycle.removeAttribute(ATTRIBUTE_NAME);
 145  0
     }
 146  
 
 147  
     /**
 148  
      * @see org.apache.tapestry.form.AbstractRequirableField#renderFormComponent(org.apache.tapestry.IMarkupWriter,
 149  
      *      org.apache.tapestry.IRequestCycle)
 150  
      */
 151  
     protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 152  
     {
 153  0
         _rewinding = false;
 154  
 
 155  
         // For rendering, the Radio components need to know what the current
 156  
         // selection is, so that the correct one can mark itself 'checked'.
 157  0
         _selection = getBinding("selected").getObject();
 158  
 
 159  0
         renderDelegatePrefix(writer, cycle);
 160  
 
 161  0
         writer.begin(getTemplateTagName());
 162  
 
 163  0
         renderInformalParameters(writer, cycle);
 164  
 
 165  0
         renderDelegateAttributes(writer, cycle);
 166  
 
 167  0
         renderBody(writer, cycle);
 168  
 
 169  0
         writer.end();
 170  
 
 171  0
         renderDelegateSuffix(writer, cycle);
 172  
 
 173  0
         getValidatableFieldSupport().renderContributions(this, writer, cycle);
 174  0
     }
 175  
 
 176  
     /**
 177  
      * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter,
 178  
      *      org.apache.tapestry.IRequestCycle)
 179  
      */
 180  
     protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 181  
     {
 182  0
         String value = cycle.getParameter(getName());
 183  
 
 184  0
         if (value == null)
 185  0
             _selectedOption = -1;
 186  
         else
 187  0
             _selectedOption = Integer.parseInt(value);
 188  
 
 189  0
         _rewinding = true;
 190  
 
 191  0
         renderBody(writer, cycle);
 192  
 
 193  
         try
 194  
         {
 195  0
             getValidatableFieldSupport().validate(this, writer, cycle, _selection);
 196  
         }
 197  0
         catch (ValidatorException e)
 198  
         {
 199  0
             getForm().getDelegate().record(e);
 200  0
         }
 201  0
     }
 202  
 
 203  
     /**
 204  
      * Injected.
 205  
      */
 206  
     public abstract ValidatableFieldSupport getValidatableFieldSupport();
 207  
 
 208  
     /**
 209  
      * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
 210  
      */
 211  
     public boolean isRequired()
 212  
     {
 213  0
         return getValidatableFieldSupport().isRequired(this);
 214  
     }
 215  
 
 216  
     /**
 217  
      * This component can not take focus.
 218  
      */
 219  
     protected boolean getCanTakeFocus()
 220  
     {
 221  0
         return false;
 222  
     }
 223  
 }