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    
015    package org.apache.tapestry.form;
016    
017    import org.apache.tapestry.IMarkupWriter;
018    import org.apache.tapestry.IRequestCycle;
019    import org.apache.tapestry.valid.ValidatorException;
020    
021    /**
022     * Implements a component that manages an HTML <input type=text> or
023     * &lt;input type=password&gt; form element. [ <a
024     * href="../../../../../ComponentReference/TextField.html">Component Reference
025     * </a>]
026     * <p>
027     * As of 4.0, this component can be configurably translated and validated.
028     * 
029     * @author Howard Lewis Ship
030     * @author Paul Ferraro
031     */
032    public abstract class TextField extends AbstractFormComponent implements
033            TranslatedField
034    {
035    
036        public abstract boolean isHidden();
037    
038        public abstract Object getValue();
039    
040        public abstract void setValue(Object value);
041    
042        /**
043         * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter,
044         *      org.apache.tapestry.IRequestCycle)
045         */
046        protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
047        {
048            String value = getTranslatedFieldSupport().format(this, getValue());
049    
050            renderDelegatePrefix(writer, cycle);
051    
052            writer.beginEmpty("input");
053    
054            writer.attribute("type", isHidden() ? "password" : "text");
055    
056            writer.attribute("name", getName());
057    
058            if (isDisabled()) writer.attribute("disabled", "disabled");
059    
060            if (value != null) writer.attribute("value", value);
061    
062            renderIdAttribute(writer, cycle);
063    
064            renderDelegateAttributes(writer, cycle);
065    
066            getTranslatedFieldSupport().renderContributions(this, writer, cycle);
067            getValidatableFieldSupport().renderContributions(this, writer, cycle);
068    
069            renderInformalParameters(writer, cycle);
070    
071            writer.closeTag();
072    
073            renderDelegateSuffix(writer, cycle);
074        }
075    
076        /**
077         * @see org.apache.tapestry.form.AbstractFormComponent#rewindFormComponent(org.apache.tapestry.IMarkupWriter,
078         *      org.apache.tapestry.IRequestCycle)
079         */
080        protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
081        {
082            String value = cycle.getParameter(getName());
083    
084            try
085            {
086                Object object = getTranslatedFieldSupport().parse(this, value);
087    
088                getValidatableFieldSupport().validate(this, writer, cycle, object);
089    
090                setValue(object);
091            }
092            catch (ValidatorException e)
093            {
094                getForm().getDelegate().record(e);
095            }
096        }
097    
098        /**
099         * Injected.
100         */
101        public abstract ValidatableFieldSupport getValidatableFieldSupport();
102    
103        /**
104         * Injected.
105         */
106        public abstract TranslatedFieldSupport getTranslatedFieldSupport();
107    
108        /**
109         * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
110         */
111        public boolean isRequired()
112        {
113            return getValidatableFieldSupport().isRequired(this);
114        }
115    }