Clover coverage report - Code Coverage for tapestry release 4.0-beta-10
Coverage timestamp: Sat Oct 8 2005 19:08:05 EDT
file stats: LOC: 207   Methods: 12
NCLOC: 98   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RadioGroup.java 78.6% 87.5% 91.7% 86.4%
coverage coverage
 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    public abstract class RadioGroup extends AbstractFormComponent implements ValidatableField
 35    {
 36    // Cached copy of the value from the selectedBinding
 37    private Object _selection;
 38   
 39    // The value from the HTTP request indicating which
 40    // Radio was selected by the user.
 41    private int _selectedOption;
 42   
 43    private boolean _rewinding;
 44   
 45    private boolean _rendering;
 46   
 47    private int _nextOptionId;
 48   
 49    /**
 50    * A <code>RadioGroup</code> places itself into the {@link IRequestCycle}as an attribute, so
 51    * that its wrapped {@link Radio}components can identify thier state.
 52    */
 53   
 54    private static final String ATTRIBUTE_NAME = "org.apache.tapestry.active.RadioGroup";
 55   
 56  70 public static RadioGroup get(IRequestCycle cycle)
 57    {
 58  70 return (RadioGroup) cycle.getAttribute(ATTRIBUTE_NAME);
 59    }
 60   
 61  69 public int getNextOptionId()
 62    {
 63  69 if (!_rendering)
 64  0 throw Tapestry.createRenderOnlyPropertyException(this, "nextOptionId");
 65   
 66  69 return _nextOptionId++;
 67    }
 68   
 69  69 public boolean isRewinding()
 70    {
 71  69 if (!_rendering)
 72  0 throw Tapestry.createRenderOnlyPropertyException(this, "rewinding");
 73   
 74  69 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  46 public boolean isSelection(Object value)
 83    {
 84  46 if (!_rendering)
 85  0 throw Tapestry.createRenderOnlyPropertyException(this, "selection");
 86   
 87  46 if (_selection == value)
 88  4 return true;
 89   
 90  42 if (_selection == null || value == null)
 91  10 return false;
 92   
 93  32 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  3 public void updateSelection(Object value)
 102    {
 103  3 getBinding("selected").setObject(value);
 104   
 105  3 _selection = value;
 106    }
 107   
 108    /**
 109    * Used by {@link Radio}components when rewinding to see if their value was submitted.
 110    */
 111   
 112  20 public boolean isSelected(int option)
 113    {
 114  20 return _selectedOption == option;
 115    }
 116   
 117    /**
 118    * @see org.apache.tapestry.AbstractComponent#prepareForRender(org.apache.tapestry.IRequestCycle)
 119    */
 120  22 protected void prepareForRender(IRequestCycle cycle)
 121    {
 122  22 if (cycle.getAttribute(ATTRIBUTE_NAME) != null)
 123  1 throw new ApplicationRuntimeException(Tapestry.getMessage("RadioGroup.may-not-nest"),
 124    this, null, null);
 125   
 126  21 cycle.setAttribute(ATTRIBUTE_NAME, this);
 127   
 128  21 _rendering = true;
 129  21 _nextOptionId = 0;
 130    }
 131   
 132    /**
 133    * @see org.apache.tapestry.AbstractComponent#cleanupAfterRender(org.apache.tapestry.IRequestCycle)
 134    */
 135  22 protected void cleanupAfterRender(IRequestCycle cycle)
 136    {
 137  22 _rendering = false;
 138  22 _selection = null;
 139   
 140  22 cycle.removeAttribute(ATTRIBUTE_NAME);
 141    }
 142   
 143    /**
 144    * @see org.apache.tapestry.form.AbstractRequirableField#renderFormComponent(org.apache.tapestry.IMarkupWriter,
 145    * org.apache.tapestry.IRequestCycle)
 146    */
 147  13 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 148    {
 149  13 _rewinding = false;
 150   
 151    // For rendering, the Radio components need to know what the current
 152    // selection is, so that the correct one can mark itself 'checked'.
 153  13 _selection = getBinding("selected").getObject();
 154   
 155  13 renderBody(writer, cycle);
 156   
 157  12 getValidatableFieldSupport().renderContributions(this, writer, cycle);
 158    }
 159   
 160    /**
 161    * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter,
 162    * org.apache.tapestry.IRequestCycle)
 163    */
 164  6 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 165    {
 166  6 String value = cycle.getParameter(getName());
 167   
 168  6 if (value == null)
 169  2 _selectedOption = -1;
 170    else
 171  4 _selectedOption = Integer.parseInt(value);
 172   
 173  6 _rewinding = true;
 174   
 175  6 renderBody(writer, cycle);
 176   
 177  6 try
 178    {
 179  6 getValidatableFieldSupport().validate(this, writer, cycle, _selection);
 180    }
 181    catch (ValidatorException e)
 182    {
 183  0 getForm().getDelegate().record(e);
 184    }
 185    }
 186   
 187    /**
 188    * Injected.
 189    */
 190    public abstract ValidatableFieldSupport getValidatableFieldSupport();
 191   
 192    /**
 193    * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
 194    */
 195  0 public boolean isRequired()
 196    {
 197  0 return getValidatableFieldSupport().isRequired(this);
 198    }
 199   
 200    /**
 201    * This component can not take focus.
 202    */
 203  12 protected boolean getCanTakeFocus()
 204    {
 205  12 return false;
 206    }
 207    }