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