Package org.apache.tapestry.form

Source Code of org.apache.tapestry.form.ValidatableFieldSupportImpl

// Copyright 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry.form;

import java.util.Iterator;

import org.apache.hivemind.service.ThreadLocale;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.coerce.ValueConverter;
import org.apache.tapestry.form.validator.Validator;
import org.apache.tapestry.valid.ValidatorException;

/**
* Default {@link ValidatableFieldSupport} implementation. This implementation generates calls to a
* static javascript function during render if client-side validation is enabled.
*
* @author Paul Ferraro
* @since 4.0
*/
public class ValidatableFieldSupportImpl implements ValidatableFieldSupport
{
    private ValueConverter _converter;

    private ThreadLocale _threadLocale;

    public void setValueConverter(ValueConverter converter)
    {
        _converter = converter;
    }

    public void setThreadLocale(ThreadLocale threadLocale)
    {
        _threadLocale = threadLocale;
    }

    protected Iterator getValidatorsIterator(ValidatableField component)
    {
        return (Iterator) _converter.coerceValue(component.getValidators(), Iterator.class);
    }

    /**
     * @see org.apache.tapestry.form.ValidatableFieldSupport#renderContributions(ValidatableField, IMarkupWriter, IRequestCycle)
     */
    public void renderContributions(ValidatableField component, IMarkupWriter writer,
            IRequestCycle cycle)
    {
        ValidatableFieldExtension extension = null;
        if (ValidatableFieldExtension.class.isInstance(component))
            extension = (ValidatableFieldExtension)component;
       
        if (component.getForm().isClientValidationEnabled())
        {
            FormComponentContributorContext context = new FormComponentContributorContextImpl(
                    _threadLocale.getLocale(), cycle, component);

            Iterator validators = getValidatorsIterator(component);

            while (validators.hasNext())
            {
                Validator validator = (Validator) validators.next();
               
                if (extension != null && extension.overrideValidator(validator, cycle))
                    extension.overrideContributions(validator, context, writer, cycle);
                else
                    validator.renderContribution(writer, cycle, context, component);
            }
        }
    }

    /**
     * @see org.apache.tapestry.form.ValidatableFieldSupport#validate(org.apache.tapestry.form.ValidatableField, org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle, java.lang.Object)
     */
    public void validate(ValidatableField component, IMarkupWriter writer, IRequestCycle cycle, Object object) throws ValidatorException
    {
        boolean isNonNull = (object != null);

        Iterator validators = getValidatorsIterator(component);

        ValidationMessages messages = new ValidationMessagesImpl(component, _threadLocale.getLocale());

        while (validators.hasNext())
        {
            Validator validator = (Validator) validators.next();

            if (isNonNull || validator.getAcceptsNull())
                validator.validate(component, messages, object);
        }
    }

    public boolean isRequired(ValidatableField field)
    {
        Iterator i = getValidatorsIterator(field);

        while (i.hasNext())
        {
            Validator validator = (Validator) i.next();

            if (validator.isRequired())
                return true;
        }

        return false;
    }
}
TOP

Related Classes of org.apache.tapestry.form.ValidatableFieldSupportImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.