001// Copyright 2004, 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry.form;
016
017import org.apache.tapestry.IMarkupWriter;
018import org.apache.tapestry.IRequestCycle;
019import org.apache.tapestry.valid.ValidatorException;
020
021/**
022 * Implements a component that manages an HTML <input type=text> or <input
023 * type=password&gt; form element. [ <a
024 * href="../../../../../ComponentReference/TextField.html">Component Reference </a>]
025 * <p>
026 * As of 4.0, this component can be configurably translated and validated.
027 * 
028 * @author Howard Lewis Ship
029 * @author Paul Ferraro
030 */
031public abstract class TextField extends AbstractFormComponent implements TranslatedField
032{
033    public abstract boolean isHidden();
034
035    public abstract Object getValue();
036    public abstract void setValue(Object value);
037
038    /**
039     * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
040     */
041    protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
042    {
043        String value = getTranslatedFieldSupport().format(this, getValue());
044        
045        renderDelegatePrefix(writer, cycle);
046
047        writer.beginEmpty("input");
048
049        writer.attribute("type", isHidden() ? "password" : "text");
050
051        writer.attribute("name", getName());
052
053        if (isDisabled())
054            writer.attribute("disabled", "disabled");
055
056        if (value != null)
057            writer.attribute("value", value);
058
059        renderIdAttribute(writer, cycle);
060
061        renderDelegateAttributes(writer, cycle);
062
063        getTranslatedFieldSupport().renderContributions(this, writer, cycle);
064        getValidatableFieldSupport().renderContributions(this, writer, cycle);
065
066        renderInformalParameters(writer, cycle);
067
068        writer.closeTag();
069
070        renderDelegateSuffix(writer, cycle);
071    }
072
073    /**
074     * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
075     */
076    protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
077    {
078        String value = cycle.getParameter(getName());
079        
080        try
081        {
082            Object object = getTranslatedFieldSupport().parse(this, value);
083            
084            getValidatableFieldSupport().validate(this, writer, cycle, object);
085            
086            setValue(object);
087        }
088        catch (ValidatorException e)
089        {
090            getForm().getDelegate().record(e);
091        }
092    }
093
094    /**
095     * Injected.
096     */
097    public abstract ValidatableFieldSupport getValidatableFieldSupport();
098
099    /**
100     * Injected.
101     */
102    public abstract TranslatedFieldSupport getTranslatedFieldSupport();
103
104    /**
105     * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
106     */
107    public boolean isRequired()
108    {
109        return getValidatableFieldSupport().isRequired(this);
110    }
111}