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