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 TranslatedField {
031    
032        public abstract Object getValue();
033    
034        public abstract void setValue(Object value);
035    
036        /**
037         * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter,
038         *      org.apache.tapestry.IRequestCycle)
039         */
040        protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
041        {
042            String value = getTranslatedFieldSupport().format(this, getValue());
043    
044            renderDelegatePrefix(writer, cycle);
045    
046            writer.begin("textarea");
047    
048            writer.attribute("name", getName());
049    
050            if (isDisabled())
051                writer.attribute("disabled", "disabled");
052    
053            renderIdAttribute(writer, cycle);
054    
055            renderDelegateAttributes(writer, cycle);
056    
057            getTranslatedFieldSupport().renderContributions(this, writer, cycle);
058            getValidatableFieldSupport().renderContributions(this, writer, cycle);
059    
060            renderInformalParameters(writer, cycle);
061    
062            if (value != null)
063                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, value);
081    
082                getValidatableFieldSupport().validate(this, writer, cycle, text);
083    
084                setValue(text);
085            }
086            catch (ValidatorException e)
087            {
088                getForm().getDelegate().record(e);
089            }
090        }
091    
092        /**
093         * Injected.
094         */
095        public abstract ValidatableFieldSupport getValidatableFieldSupport();
096    
097        /**
098         * Injected.
099         */
100        public abstract TranslatedFieldSupport getTranslatedFieldSupport();
101    
102        /**
103         * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
104         */
105        public boolean isRequired()
106        {
107            return getValidatableFieldSupport().isRequired(this);
108        }
109    }