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