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