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: 152   Methods: 4
NCLOC: 76   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ValidField.java 77.8% 88.6% 100% 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.valid;
 16   
 17    import org.apache.tapestry.IMarkupWriter;
 18    import org.apache.tapestry.IRequestCycle;
 19    import org.apache.tapestry.Tapestry;
 20    import org.apache.tapestry.form.AbstractFormComponent;
 21    import org.apache.tapestry.form.Form;
 22   
 23    /**
 24    * A {@link Form}component that creates a text field that allows for validation of user input and
 25    * conversion between string and object values. [ <a
 26    * href="../../../../../ComponentReference/ValidField.html">Component Reference </a>]
 27    * <p>
 28    * A ValidatingTextField uses an {@link IValidationDelegate} to track errors and an
 29    * {@link IValidator}to convert between strings and objects (as well as perform validations). The
 30    * validation delegate is shared by all validating text fields in a form, the validator may be
 31    * shared my multiple elements as desired.
 32    *
 33    * @author Howard Lewis Ship
 34    */
 35   
 36    public abstract class ValidField extends AbstractFormComponent
 37    {
 38    public abstract boolean isHidden();
 39   
 40    public abstract boolean isDisabled();
 41   
 42    public abstract Object getValue();
 43   
 44    public abstract void setValue(Object value);
 45   
 46    /** Parameter */
 47   
 48    public abstract String getDisplayName();
 49   
 50    /**
 51    * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter,
 52    * org.apache.tapestry.IRequestCycle)
 53    */
 54  3 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 55    {
 56  3 IValidationDelegate delegate = getForm().getDelegate();
 57   
 58  3 delegate.registerForFocus(this, delegate.isInError() ? ValidationConstants.ERROR_FIELD
 59    : ValidationConstants.NORMAL_FIELD);
 60   
 61  3 delegate.writePrefix(writer, cycle, this, null);
 62   
 63  3 writer.beginEmpty("input");
 64   
 65  3 writer.attribute("type", isHidden() ? "password" : "text");
 66   
 67  3 if (isDisabled())
 68  0 writer.attribute("disabled", "disabled");
 69   
 70  3 writer.attribute("name", getName());
 71   
 72  3 String value = readValue();
 73  3 if (value != null)
 74  2 writer.attribute("value", value);
 75   
 76  3 renderIdAttribute(writer, cycle);
 77   
 78  3 renderInformalParameters(writer, cycle);
 79   
 80  3 delegate.writeAttributes(writer, cycle, this, null);
 81   
 82  3 IValidator validator = getValidator();
 83   
 84  3 if (validator == null)
 85  0 throw Tapestry.createRequiredParameterException(this, "validator");
 86   
 87  3 if (validator.isRequired())
 88  1 delegate.registerForFocus(this, ValidationConstants.REQUIRED_FIELD);
 89   
 90  3 validator.renderValidatorContribution(this, writer, cycle);
 91   
 92  3 writer.closeTag();
 93   
 94  3 delegate.writeSuffix(writer, cycle, this, null);
 95    }
 96   
 97    /**
 98    * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter,
 99    * org.apache.tapestry.IRequestCycle)
 100    */
 101  2 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 102    {
 103  2 String value = cycle.getParameter(getName());
 104   
 105  2 updateValue(value);
 106    }
 107   
 108  3 protected String readValue()
 109    {
 110  3 IValidator validator = getValidator();
 111  3 if (validator == null)
 112  0 throw Tapestry.createRequiredParameterException(this, "validator");
 113   
 114  3 IValidationDelegate delegate = getForm().getDelegate();
 115   
 116  3 if (delegate.isInError())
 117  1 return delegate.getFieldInputValue();
 118   
 119  2 Object value = getValue();
 120   
 121  2 String result = validator.toString(this, value);
 122   
 123  2 return result;
 124    }
 125   
 126  2 protected void updateValue(String value)
 127    {
 128  2 Object objectValue = null;
 129   
 130  2 IValidator validator = getValidator();
 131  2 if (validator == null)
 132  1 throw Tapestry.createRequiredParameterException(this, "validator");
 133   
 134  1 IValidationDelegate delegate = getForm().getDelegate();
 135   
 136  1 delegate.recordFieldInputValue(value);
 137   
 138  1 try
 139    {
 140  1 objectValue = validator.toObject(this, value);
 141    }
 142    catch (ValidatorException ex)
 143    {
 144  0 delegate.record(ex);
 145  0 return;
 146    }
 147   
 148  1 setValue(objectValue);
 149    }
 150   
 151    public abstract IValidator getValidator();
 152    }