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