Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 179   Methods: 7
NCLOC: 81   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
RadioGroup.java 83.3% 91.7% 100% 90.2%
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.IForm;
 19   
 import org.apache.tapestry.IMarkupWriter;
 20   
 import org.apache.tapestry.IRequestCycle;
 21   
 import org.apache.tapestry.Tapestry;
 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   
  * 
 29   
  * @author Howard Lewis Ship
 30   
  */
 31   
 
 32   
 public abstract class RadioGroup extends AbstractFormComponent
 33   
 {
 34   
     // Cached copy of the value from the selectedBinding
 35   
     private Object _selection;
 36   
 
 37   
     // The value from the HTTP request indicating which
 38   
     // Radio was selected by the user.
 39   
     private int _selectedOption;
 40   
 
 41   
     private boolean _rewinding;
 42   
 
 43   
     private boolean _rendering;
 44   
 
 45   
     private int _nextOptionId;
 46   
 
 47   
     /**
 48   
      * A <code>RadioGroup</code> places itself into the {@link IRequestCycle}as an attribute, so
 49   
      * that its wrapped {@link Radio}components can identify thier state.
 50   
      */
 51   
 
 52   
     private static final String ATTRIBUTE_NAME = "org.apache.tapestry.active.RadioGroup";
 53   
 
 54  86
     public static RadioGroup get(IRequestCycle cycle)
 55   
     {
 56  86
         return (RadioGroup) cycle.getAttribute(ATTRIBUTE_NAME);
 57   
     }
 58   
 
 59  85
     public int getNextOptionId()
 60   
     {
 61  85
         if (!_rendering)
 62  0
             throw Tapestry.createRenderOnlyPropertyException(this, "nextOptionId");
 63   
 
 64  85
         return _nextOptionId++;
 65   
     }
 66   
 
 67   
     /**
 68   
      * Used by {@link Radio}components wrapped by this <code>RadioGroup</code> to see if the
 69   
      * group as a whole is disabled.
 70   
      */
 71   
 
 72   
     public abstract boolean isDisabled();
 73   
 
 74  85
     public boolean isRewinding()
 75   
     {
 76  85
         if (!_rendering)
 77  0
             throw Tapestry.createRenderOnlyPropertyException(this, "rewinding");
 78   
 
 79  85
         return _rewinding;
 80   
     }
 81   
 
 82   
     /**
 83   
      * Returns true if the value is equal to the current selection for the group. This is invoked by
 84   
      * a {@link Radio}during rendering to determine if it should be marked 'checked'.
 85   
      */
 86   
 
 87  54
     public boolean isSelection(Object value)
 88   
     {
 89  54
         if (!_rendering)
 90  0
             throw Tapestry.createRenderOnlyPropertyException(this, "selection");
 91   
 
 92  54
         if (_selection == value)
 93  4
             return true;
 94   
 
 95  50
         if (_selection == null || value == null)
 96  10
             return false;
 97   
 
 98  40
         return _selection.equals(value);
 99   
     }
 100   
 
 101   
     /**
 102   
      * Invoked by the {@link Radio}which is selected to update the property bound to the selected
 103   
      * parameter.
 104   
      */
 105   
 
 106  5
     public void updateSelection(Object value)
 107   
     {
 108  5
         getBinding("selected").setObject(value);
 109   
     }
 110   
 
 111   
     /**
 112   
      * Used by {@link Radio}components when rewinding to see if their value was submitted.
 113   
      */
 114   
 
 115  24
     public boolean isSelected(int option)
 116   
     {
 117  24
         return _selectedOption == option;
 118   
     }
 119   
 
 120   
     /**
 121   
      * Doesn't actual render an HTML element as there is no direct equivalent for an HTML element. A
 122   
      * <code>RadioGroup</code> component exists to organize the {@link Radio}components it wraps
 123   
      * (directly or indirectly). A {@link Radio}can finds its {@link RadioGroup}as a
 124   
      * {@link IRequestCycle}attribute.
 125   
      */
 126   
 
 127  26
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 128   
     {
 129  26
         IForm form = getForm(cycle);
 130   
 
 131  26
         if (cycle.getAttribute(ATTRIBUTE_NAME) != null)
 132  1
             throw new ApplicationRuntimeException(Tapestry.getMessage("RadioGroup.may-not-nest"),
 133   
                     this, null, null);
 134   
 
 135   
         // It isn't enough to know whether the cycle in general is rewinding, need to know
 136   
         // specifically if the form which contains this component is rewinding.
 137   
 
 138  25
         _rewinding = form.isRewinding();
 139   
 
 140   
         // Used whether rewinding or not.
 141   
 
 142  25
         String name = form.getElementId(this);
 143   
 
 144  25
         cycle.setAttribute(ATTRIBUTE_NAME, this);
 145   
 
 146   
         // When rewinding, find out which (if any) radio was selected by
 147   
         // the user.
 148   
 
 149  25
         if (_rewinding)
 150   
         {
 151  9
             String value = cycle.getParameter(name);
 152  9
             if (value == null)
 153  2
                 _selectedOption = -1;
 154   
             else
 155  7
                 _selectedOption = Integer.parseInt(value);
 156   
         }
 157   
 
 158  25
         try
 159   
         {
 160  25
             _rendering = true;
 161  25
             _nextOptionId = 0;
 162   
 
 163   
             // For rendering, the Radio components need to know what the current
 164   
             // selection is, so that the correct one can mark itself 'checked'.
 165   
 
 166  25
             if (!_rewinding)
 167  16
                 _selection = getBinding("selected").getObject();
 168   
 
 169  25
             renderBody(writer, cycle);
 170   
         }
 171   
         finally
 172   
         {
 173  25
             _rendering = false;
 174  25
             _selection = null;
 175   
         }
 176   
 
 177  24
         cycle.removeAttribute(ATTRIBUTE_NAME);
 178   
     }
 179   
 }