Clover coverage report - Code Coverage for tapestry release 4.0-beta-3
Coverage timestamp: Sun Jul 24 2005 08:26:33 EDT
file stats: LOC: 87   Methods: 3
NCLOC: 48   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RequirableFieldSupportImpl.java 100% 100% 100% 100%
coverage
 1    // Copyright 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.form;
 16   
 17    import java.text.MessageFormat;
 18   
 19    import org.apache.hivemind.HiveMind;
 20    import org.apache.tapestry.IForm;
 21    import org.apache.tapestry.IMarkupWriter;
 22    import org.apache.tapestry.IRequestCycle;
 23    import org.apache.tapestry.valid.ValidationConstants;
 24    import org.apache.tapestry.valid.ValidationConstraint;
 25    import org.apache.tapestry.valid.ValidatorException;
 26   
 27    /**
 28    * Default {@link RequirableFieldSupport} implementation. This implementation generates calls to a
 29    * static javascript function during render if client-side validation is enabled.
 30    *
 31    * @author Paul Ferraro
 32    * @since 4.0
 33    */
 34    public class RequirableFieldSupportImpl implements RequirableFieldSupport
 35    {
 36    /**
 37    * @see org.apache.tapestry.form.RequirableFieldSupport#render(org.apache.tapestry.form.RequirableField,
 38    * org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
 39    */
 40  29 public void render(RequirableField component, IMarkupWriter writer, IRequestCycle cycle)
 41    {
 42  29 IForm form = component.getForm();
 43   
 44  29 if (component.isRequired())
 45    {
 46  2 form.registerForFocus(component, ValidationConstants.REQUIRED_FIELD);
 47   
 48  2 if (form.isClientValidationEnabled())
 49    {
 50  1 String function = "require(event, document." + form.getName() + "."
 51    + component.getName() + ",'" + buildRequiredMessage(component) + "')";
 52   
 53  1 form.addEventHandler(FormEventType.SUBMIT, function);
 54    }
 55    }
 56    }
 57   
 58    /**
 59    * @see org.apache.tapestry.form.RequirableFieldSupport#rewind(org.apache.tapestry.form.RequirableField,
 60    * org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
 61    */
 62  19 public void rewind(RequirableField component, IMarkupWriter writer, IRequestCycle cycle)
 63    {
 64  19 try
 65    {
 66  19 String value = component.getSubmittedValue(cycle);
 67   
 68  19 if (component.isRequired() && HiveMind.isBlank(value))
 69    {
 70  1 throw new ValidatorException(buildRequiredMessage(component),
 71    ValidationConstraint.REQUIRED);
 72    }
 73   
 74  18 component.bind(writer, cycle);
 75    }
 76    catch (ValidatorException e)
 77    {
 78  1 component.getForm().getDelegate().record(e);
 79    }
 80    }
 81   
 82  2 protected String buildRequiredMessage(RequirableField component)
 83    {
 84  2 return MessageFormat.format(component.getRequiredMessage(), new Object[]
 85    { component.getDisplayName() });
 86    }
 87    }