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: 194   Methods: 9
NCLOC: 98   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Select.java 83.3% 91.3% 88.9% 89%
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  15 cycle.removeAttribute(ATTRIBUTE_NAME);
 113    }
 114   
 115    /**
 116    * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
 117    */
 118  8 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 119    {
 120  8 _rewinding = false;
 121   
 122  8 renderDelegatePrefix(writer, cycle);
 123   
 124  8 writer.begin("select");
 125   
 126  8 writer.attribute("name", getName());
 127   
 128  8 if (isMultiple())
 129  4 writer.attribute("multiple", "multiple");
 130   
 131  8 if (isDisabled())
 132  1 writer.attribute("disabled", "disabled");
 133   
 134  8 renderIdAttribute(writer, cycle);
 135   
 136  8 renderDelegateAttributes(writer, cycle);
 137   
 138  8 getValidatableFieldSupport().renderContributions(this, writer, cycle);
 139   
 140  8 renderInformalParameters(writer, cycle);
 141   
 142  8 renderBody(writer, cycle);
 143   
 144  7 writer.end();
 145   
 146  7 renderDelegateSuffix(writer, cycle);
 147    }
 148   
 149    /**
 150    * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
 151    */
 152  4 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 153    {
 154  4 _selections = null;
 155  4 _rewinding = true;
 156   
 157  4 String[] parameters = cycle.getParameters(getName());
 158   
 159  4 try
 160    {
 161  4 if (parameters != null)
 162    {
 163  3 int length = parameters.length;
 164   
 165  3 _selections = new HashSet((length > 30) ? 101 : 7);
 166   
 167  3 for (int i = 0; i < length; i++)
 168  5 _selections.add(parameters[i]);
 169    }
 170   
 171  4 renderBody(writer, cycle);
 172   
 173    // This is atypical validation - since this component does not explicitly bind to an object
 174  4 getValidatableFieldSupport().validate(this, writer, cycle, parameters);
 175    }
 176    catch (ValidatorException e)
 177    {
 178  0 getForm().getDelegate().record(e);
 179    }
 180    }
 181   
 182    /**
 183    * Injected.
 184    */
 185    public abstract ValidatableFieldSupport getValidatableFieldSupport();
 186   
 187    /**
 188    * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
 189    */
 190  0 public boolean isRequired()
 191    {
 192  0 return getValidatableFieldSupport().isRequired(this);
 193    }
 194    }