Clover coverage report - Code Coverage for tapestry release 4.0.2
Coverage timestamp: Thu Apr 13 2006 10:52:06 EDT
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  3 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 54    {
 55  3 IValidationDelegate delegate = getForm().getDelegate();
 56   
 57  3 delegate.registerForFocus(this, delegate.isInError() ? ValidationConstants.ERROR_FIELD
 58    : ValidationConstants.NORMAL_FIELD);
 59   
 60  3 delegate.writePrefix(writer, cycle, this, null);
 61   
 62  3 writer.beginEmpty("input");
 63   
 64  3 writer.attribute("type", isHidden() ? "password" : "text");
 65   
 66  3 if (isDisabled())
 67  0 writer.attribute("disabled", "disabled");
 68   
 69  3 writer.attribute("name", getName());
 70   
 71  3 String value = readValue();
 72  3 if (value != null)
 73  2 writer.attribute("value", value);
 74   
 75  3 renderIdAttribute(writer, cycle);
 76   
 77  3 renderInformalParameters(writer, cycle);
 78   
 79  3 delegate.writeAttributes(writer, cycle, this, null);
 80   
 81  3 IValidator validator = getValidator();
 82   
 83  3 if (validator == null)
 84  0 throw Tapestry.createRequiredParameterException(this, "validator");
 85   
 86  3 if (validator.isRequired())
 87  1 delegate.registerForFocus(this, ValidationConstants.REQUIRED_FIELD);
 88   
 89  3 validator.renderValidatorContribution(this, writer, cycle);
 90   
 91  3 writer.closeTag();
 92   
 93  3 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  2 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 101    {
 102  2 String value = cycle.getParameter(getName());
 103   
 104  2 updateValue(value);
 105    }
 106   
 107  3 protected String readValue()
 108    {
 109  3 IValidator validator = getValidator();
 110  3 if (validator == null)
 111  0 throw Tapestry.createRequiredParameterException(this, "validator");
 112   
 113  3 IValidationDelegate delegate = getForm().getDelegate();
 114   
 115  3 if (delegate.isInError())
 116  1 return delegate.getFieldInputValue();
 117   
 118  2 Object value = getValue();
 119   
 120  2 String result = validator.toString(this, value);
 121   
 122  2 return result;
 123    }
 124   
 125  2 protected void updateValue(String value)
 126    {
 127  2 Object objectValue = null;
 128   
 129  2 IValidator validator = getValidator();
 130  2 if (validator == null)
 131  1 throw Tapestry.createRequiredParameterException(this, "validator");
 132   
 133  1 IValidationDelegate delegate = getForm().getDelegate();
 134   
 135  1 delegate.recordFieldInputValue(value);
 136   
 137  1 try
 138    {
 139  1 objectValue = validator.toObject(this, value);
 140    }
 141    catch (ValidatorException ex)
 142    {
 143  0 delegate.record(ex);
 144  0 return;
 145    }
 146   
 147  1 setValue(objectValue);
 148    }
 149   
 150    public abstract IValidator getValidator();
 151    }