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