Clover coverage report - Code Coverage for tapestry release 4.0-beta-4
Coverage timestamp: Wed Aug 10 2005 21:19:31 EDT
file stats: LOC: 192   Methods: 9
NCLOC: 97   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Select.java 83.3% 91.1% 88.9% 88.9%
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 java.util.HashSet;
 18    import java.util.Set;
 19   
 20    import org.apache.hivemind.ApplicationRuntimeException;
 21    import org.apache.tapestry.IMarkupWriter;
 22    import org.apache.tapestry.IRequestCycle;
 23    import org.apache.tapestry.Tapestry;
 24    import org.apache.tapestry.valid.ValidatorException;
 25   
 26    /**
 27    * Implements a component that manages an HTML <select> form element. The most common
 28    * situation, using a <select> to set a specific property of some object, is best handled
 29    * using a {@link PropertySelection}component. [ <a
 30    * href="../../../../../ComponentReference/Select.html">Component Reference </a>]
 31    * <p>
 32    * Otherwise, this component is very similar to {@link RadioGroup}.
 33    * <p>
 34    * As of 4.0, this component can be validated.
 35    *
 36    * @author Howard Lewis Ship
 37    * @author Paul Ferraro
 38    */
 39    public abstract class Select extends AbstractFormComponent implements ValidatableField
 40    {
 41    private boolean _rewinding;
 42   
 43    private boolean _rendering;
 44   
 45    private Set _selections;
 46   
 47    private int _nextOptionId;
 48   
 49    /**
 50    * Used by the <code>Select</code> to record itself as a {@link IRequestCycle}attribute, so
 51    * that the {@link Option}components it wraps can have access to it.
 52    */
 53   
 54    private final static String ATTRIBUTE_NAME = "org.apache.tapestry.active.Select";
 55   
 56  31 public static Select get(IRequestCycle cycle)
 57    {
 58  31 return (Select) cycle.getAttribute(ATTRIBUTE_NAME);
 59    }
 60   
 61    public abstract boolean isMultiple();
 62   
 63  30 public boolean isRewinding()
 64    {
 65  30 if (!_rendering)
 66  0 throw Tapestry.createRenderOnlyPropertyException(this, "rewinding");
 67   
 68  30 return _rewinding;
 69    }
 70   
 71  30 public String getNextOptionId()
 72    {
 73  30 if (!_rendering)
 74  0 throw Tapestry.createRenderOnlyPropertyException(this, "nextOptionId");
 75   
 76    // Return it as a hex value.
 77   
 78  30 return Integer.toString(_nextOptionId++);
 79    }
 80   
 81  11 public boolean isSelected(String value)
 82    {
 83  11 if (_selections == null)
 84  3 return false;
 85   
 86  8 return _selections.contains(value);
 87    }
 88   
 89    /**
 90    * @see org.apache.tapestry.AbstractComponent#prepareForRender(org.apache.tapestry.IRequestCycle)
 91    */
 92  15 protected void prepareForRender(IRequestCycle cycle)
 93    {
 94  15 if (cycle.getAttribute(ATTRIBUTE_NAME) != null)
 95  1 throw new ApplicationRuntimeException(Tapestry.getMessage("Select.may-not-nest"), this,
 96    null, null);
 97   
 98  14 cycle.setAttribute(ATTRIBUTE_NAME, this);
 99   
 100  14 _rendering = true;
 101  14 _nextOptionId = 0;
 102    }
 103   
 104    /**
 105    * @see org.apache.tapestry.AbstractComponent#cleanupAfterRender(org.apache.tapestry.IRequestCycle)
 106    */
 107  15 protected void cleanupAfterRender(IRequestCycle cycle)
 108    {
 109  15 _rendering = false;
 110  15 _selections = null;
 111    }
 112   
 113    /**
 114    * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
 115    */
 116  8 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 117    {
 118  8 _rewinding = false;
 119   
 120  8 renderDelegatePrefix(writer, cycle);
 121   
 122  8 writer.begin("select");
 123   
 124  8 writer.attribute("name", getName());
 125   
 126  8 if (isMultiple())
 127  4 writer.attribute("multiple", "multiple");
 128   
 129  8 if (isDisabled())
 130  1 writer.attribute("disabled", "disabled");
 131   
 132  8 renderIdAttribute(writer, cycle);
 133   
 134  8 renderDelegateAttributes(writer, cycle);
 135   
 136  8 getValidatableFieldSupport().renderContributions(this, writer, cycle);
 137   
 138  8 renderInformalParameters(writer, cycle);
 139   
 140  8 renderBody(writer, cycle);
 141   
 142  7 writer.end();
 143   
 144  7 renderDelegateSuffix(writer, cycle);
 145    }
 146   
 147    /**
 148    * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
 149    */
 150  4 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 151    {
 152  4 _selections = null;
 153  4 _rewinding = true;
 154   
 155  4 String[] parameters = cycle.getParameters(getName());
 156   
 157  4 try
 158    {
 159  4 if (parameters != null)
 160    {
 161  3 int length = parameters.length;
 162   
 163  3 _selections = new HashSet((length > 30) ? 101 : 7);
 164   
 165  3 for (int i = 0; i < length; i++)
 166  5 _selections.add(parameters[i]);
 167    }
 168   
 169  4 renderBody(writer, cycle);
 170   
 171    // This is atypical validation - since this component does not explicitly bind to an object
 172  4 getValidatableFieldSupport().validate(this, writer, cycle, parameters);
 173    }
 174    catch (ValidatorException e)
 175    {
 176  0 getForm().getDelegate().record(e);
 177    }
 178    }
 179   
 180    /**
 181    * Injected.
 182    */
 183    public abstract ValidatableFieldSupport getValidatableFieldSupport();
 184   
 185    /**
 186    * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
 187    */
 188  0 public boolean isRequired()
 189    {
 190  0 return getValidatableFieldSupport().isRequired(this);
 191    }
 192    }