Clover coverage report - Code Coverage for tapestry release 3.1-alpha-1
Coverage timestamp: Mon Feb 21 2005 09:16:14 EST
file stats: LOC: 185   Methods: 5
NCLOC: 87   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
ValidField.java 83.3% 93.5% 100% 91.3%
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.hivemind.ApplicationRuntimeException;
 18   
 import org.apache.hivemind.HiveMind;
 19   
 import org.apache.tapestry.IForm;
 20   
 import org.apache.tapestry.IMarkupWriter;
 21   
 import org.apache.tapestry.IRequestCycle;
 22   
 import org.apache.tapestry.Tapestry;
 23   
 import org.apache.tapestry.form.AbstractTextField;
 24   
 import org.apache.tapestry.form.Form;
 25   
 import org.apache.tapestry.form.IFormComponent;
 26   
 import org.apache.tapestry.html.Body;
 27   
 
 28   
 /**
 29   
  * A {@link Form}component that creates a text field that allows for validation of user input and
 30   
  * conversion between string and object values. [ <a
 31   
  * href="../../../../../ComponentReference/ValidField.html">Component Reference </a>]
 32   
  * <p>
 33   
  * A ValidatingTextField uses an {@link IValidationDelegate}to track errors and an
 34   
  * {@link IValidator}to convert between strings and objects (as well as perform validations). The
 35   
  * validation delegate is shared by all validating text fields in a form, the validator may be
 36   
  * shared my multiple elements as desired.
 37   
  * 
 38   
  * @author Howard Lewis Ship
 39   
  */
 40   
 
 41   
 public abstract class ValidField extends AbstractTextField implements IFormComponent
 42   
 {
 43   
     public abstract Object getValue();
 44   
 
 45   
     public abstract void setValue(Object value);
 46   
 
 47   
     public abstract String getDisplayName();
 48   
 
 49   
     /**
 50   
      * Renders the component, which involves the {@link IValidationDelegate delegate}.
 51   
      * <p>
 52   
      * During a render, the <em>first</em> field rendered that is either in error, or required but
 53   
      * null gets special treatment. JavaScript is added to select that field (such that the cursor
 54   
      * jumps right to the field when the page loads).
 55   
      */
 56   
 
 57  20
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 58   
     {
 59  20
         IForm form = getForm(cycle);
 60  20
         IValidationDelegate delegate = form.getDelegate();
 61   
 
 62  20
         if (delegate == null)
 63  0
             throw new ApplicationRuntimeException(Tapestry.format(
 64   
                     "ValidField.no-delegate",
 65   
                     getExtendedId(),
 66   
                     getForm().getExtendedId()), this, null, null);
 67   
 
 68  20
         IValidator validator = getValidator();
 69   
 
 70  20
         if (validator == null)
 71  0
             throw Tapestry.createRequiredParameterException(this, "validator");
 72   
 
 73  20
         boolean rendering = !cycle.isRewinding();
 74   
 
 75  20
         if (rendering)
 76  9
             delegate.writePrefix(writer, cycle, this, validator);
 77   
 
 78  20
         super.renderComponent(writer, cycle);
 79   
 
 80  19
         if (rendering)
 81  8
             delegate.writeSuffix(writer, cycle, this, validator);
 82   
 
 83   
         // If rendering and there's either an error in the field,
 84   
         // then we may have identified the default field (which will
 85   
         // automatically receive focus).
 86   
 
 87  19
         if (rendering && delegate.isInError())
 88  1
             addSelect(cycle);
 89   
 
 90   
         // That's OK, but an ideal situation would know about non-validating
 91   
         // text fields, and also be able to put the cursor in the
 92   
         // first field, period (even if there are no required or error fields).
 93   
         // Still, this pretty much rocks!
 94   
 
 95   
     }
 96   
 
 97   
     /**
 98   
      * Invokes
 99   
      * {@link IValidationDelegate#writeAttributes(IMarkupWriter,IRequestCycle, IFormComponent,IValidator)}.
 100   
      */
 101   
 
 102  9
     protected void beforeCloseTag(IMarkupWriter writer, IRequestCycle cycle)
 103   
     {
 104  9
         IValidator validator = getValidator();
 105   
 
 106  9
         validator.renderValidatorContribution(this, writer, cycle);
 107   
 
 108  8
         getForm().getDelegate().writeAttributes(writer, cycle, this, validator);
 109   
     }
 110   
 
 111   
     private static final String SELECTED_ATTRIBUTE_NAME = "org.apache.tapestry.component.html.valid.SelectedFieldSet";
 112   
 
 113   
     /**
 114   
      * Creates JavaScript to set the cursor on the first required or error field encountered while
 115   
      * rendering. This only works if the text field is wrapped by a {@link Body}component (which is
 116   
      * almost always true).
 117   
      */
 118   
 
 119  5
     protected void addSelect(IRequestCycle cycle)
 120   
     {
 121   
         // If some other field has taken the honors, then let it.
 122   
 
 123  5
         if (cycle.getAttribute(SELECTED_ATTRIBUTE_NAME) != null)
 124  2
             return;
 125   
 
 126  3
         Body body = Body.get(cycle);
 127   
 
 128   
         // If not wrapped by a Body, then do nothing.
 129   
 
 130  3
         if (body == null)
 131  0
             return;
 132   
 
 133  3
         IForm form = Form.get(cycle);
 134   
 
 135  3
         String formName = form.getName();
 136  3
         String textFieldName = getName();
 137   
 
 138  3
         String fullName = "document." + formName + "." + textFieldName;
 139   
 
 140  3
         body.addInitializationScript(fullName + ".focus();");
 141  3
         body.addInitializationScript(fullName + ".select();");
 142   
 
 143   
         // Put a marker in, indicating that the selected field is known.
 144   
 
 145  3
         cycle.setAttribute(SELECTED_ATTRIBUTE_NAME, Boolean.TRUE);
 146   
     }
 147   
 
 148  9
     protected String readValue()
 149   
     {
 150  9
         IValidationDelegate delegate = getForm().getDelegate();
 151   
 
 152  9
         if (delegate.isInError())
 153  1
             return delegate.getFieldInputValue();
 154   
 
 155  8
         Object value = getValue();
 156  8
         String result = getValidator().toString(this, value);
 157   
 
 158  8
         if (HiveMind.isBlank(result) && getValidator().isRequired())
 159  4
             addSelect(getPage().getRequestCycle());
 160   
 
 161  8
         return result;
 162   
     }
 163   
 
 164  11
     protected void updateValue(String value)
 165   
     {
 166  11
         Object objectValue = null;
 167  11
         IValidationDelegate delegate = getForm().getDelegate();
 168   
 
 169  11
         delegate.recordFieldInputValue(value);
 170   
 
 171  11
         try
 172   
         {
 173  11
             objectValue = getValidator().toObject(this, value);
 174   
         }
 175   
         catch (ValidatorException ex)
 176   
         {
 177  1
             delegate.record(ex);
 178  1
             return;
 179   
         }
 180   
 
 181  10
         setValue(objectValue);
 182   
     }
 183   
 
 184   
     public abstract IValidator getValidator();
 185   
 }