Clover coverage report - Code Coverage for tapestry release 4.0-beta-12
Coverage timestamp: Sun Oct 30 2005 16:22:01 EST
file stats: LOC: 172   Methods: 4
NCLOC: 75   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PropertySelection.java 71.4% 85.7% 75% 81.7%
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.tapestry.IMarkupWriter;
 18    import org.apache.tapestry.IRequestCycle;
 19    import org.apache.tapestry.Tapestry;
 20    import org.apache.tapestry.valid.ValidatorException;
 21   
 22    /**
 23    * A component used to render a drop-down list of options that the user may select. [ <a
 24    * href="../../../../../ComponentReference/PropertySelection.html">Component Reference </a>]
 25    * <p>
 26    * Earlier versions of PropertySelection (through release 2.2) were more flexible, they included a
 27    * <b>renderer </b> property that controlled how the selection was rendered. Ultimately, this proved
 28    * of little value and this portion of functionality was deprecated in 2.3 and will be removed in
 29    * 2.3.
 30    * <p>
 31    * Typically, the values available to be selected are defined using an
 32    * {@link org.apache.commons.lang.enum.Enum}. A PropertySelection is dependent on an
 33    * {@link IPropertySelectionModel} to provide the list of possible values.
 34    * <p>
 35    * Often, this is used to select a particular {@link org.apache.commons.lang.enum.Enum} to assign to
 36    * a property; the {@link EnumPropertySelectionModel} class simplifies this.
 37    * <p>
 38    * Often, a drop-down list will contain an initial option that serves both as a label and to represent
 39    * that nothing is selected. This can behavior can easily be achieved by decorating an existing
 40    * {@link IPropertySelectionModel} with a {@link LabeledPropertySelectionModel}.
 41    * <p>
 42    * As of 4.0, this component can be validated.
 43    *
 44    * @author Howard Lewis Ship
 45    * @author Paul Ferraro
 46    */
 47    public abstract class PropertySelection extends AbstractFormComponent implements ValidatableField
 48    {
 49    /**
 50    * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
 51    */
 52  3 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 53    {
 54  3 renderDelegatePrefix(writer, cycle);
 55   
 56  3 writer.begin("select");
 57  3 writer.attribute("name", getName());
 58   
 59  3 if (isDisabled())
 60  0 writer.attribute("disabled", "disabled");
 61   
 62  3 if (getSubmitOnChange())
 63  0 writer.attribute("onchange", "javascript: this.form.events.submit();");
 64   
 65  3 renderIdAttribute(writer, cycle);
 66   
 67  3 renderDelegateAttributes(writer, cycle);
 68   
 69  3 getValidatableFieldSupport().renderContributions(this, writer, cycle);
 70   
 71    // Apply informal attributes.
 72  3 renderInformalParameters(writer, cycle);
 73   
 74  3 writer.println();
 75   
 76  3 IPropertySelectionModel model = getModel();
 77   
 78  3 if (model == null)
 79  0 throw Tapestry.createRequiredParameterException(this, "model");
 80   
 81  3 int count = model.getOptionCount();
 82  3 boolean foundSelected = false;
 83  3 Object value = getValue();
 84   
 85  3 for (int i = 0; i < count; i++)
 86    {
 87  21 Object option = model.getOption(i);
 88   
 89  21 writer.begin("option");
 90  21 writer.attribute("value", model.getValue(i));
 91   
 92  21 if (!foundSelected && isEqual(option, value))
 93    {
 94  3 writer.attribute("selected", "selected");
 95   
 96  3 foundSelected = true;
 97    }
 98   
 99  21 writer.print(model.getLabel(i));
 100   
 101  21 writer.end();
 102   
 103  21 writer.println();
 104    }
 105   
 106  3 writer.end(); // <select>
 107   
 108  3 renderDelegateSuffix(writer, cycle);
 109    }
 110   
 111    /**
 112    * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
 113    */
 114  3 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 115    {
 116  3 String value = cycle.getParameter(getName());
 117   
 118  3 Object object = getModel().translateValue(value);
 119   
 120  3 try
 121    {
 122  3 getValidatableFieldSupport().validate(this, writer, cycle, object);
 123   
 124  3 setValue(object);
 125    }
 126    catch (ValidatorException e)
 127    {
 128  0 getForm().getDelegate().record(e);
 129    }
 130    }
 131   
 132  9 private boolean isEqual(Object left, Object right)
 133    {
 134    // Both null, or same object, then are equal
 135   
 136  9 if (left == right)
 137  3 return true;
 138   
 139    // If one is null, the other isn't, then not equal.
 140   
 141  6 if (left == null || right == null)
 142  0 return false;
 143   
 144    // Both non-null; use standard comparison.
 145   
 146  6 return left.equals(right);
 147    }
 148   
 149    public abstract IPropertySelectionModel getModel();
 150   
 151    /** @since 2.2 * */
 152    public abstract boolean getSubmitOnChange();
 153   
 154    /** @since 2.2 * */
 155    public abstract Object getValue();
 156   
 157    /** @since 2.2 * */
 158    public abstract void setValue(Object value);
 159   
 160    /**
 161    * Injected.
 162    */
 163    public abstract ValidatableFieldSupport getValidatableFieldSupport();
 164   
 165    /**
 166    * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
 167    */
 168  0 public boolean isRequired()
 169    {
 170  0 return getValidatableFieldSupport().isRequired(this);
 171    }
 172    }