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 <textarea> form element.
023 *
024 * [<a href="../../../../../ComponentReference/TextArea.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 TextArea extends AbstractFormComponent implements TranslatedField
032{
033    public abstract String getValue();
034    
035    public abstract void setValue(String value);
036    
037    /**
038     * @see org.apache.tapestry.form.AbstractFormComponent#renderFormComponent(org.apache.tapestry.IMarkupWriter, 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, org.apache.tapestry.IRequestCycle)
072     */
073    protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
074    {
075        String value = cycle.getParameter(getName());
076        
077        try
078        {
079            String text = (String) getTranslatedFieldSupport().parse(this, value);
080            
081            getValidatableFieldSupport().validate(this, writer, cycle, text);
082            
083            setValue(text);
084        }
085        catch (ValidatorException e)
086        {
087            getForm().getDelegate().record(e);
088        }
089    }
090
091    /**
092     * Injected.
093     */
094    public abstract ValidatableFieldSupport getValidatableFieldSupport();
095
096    /**
097     * Injected.
098     */
099    public abstract TranslatedFieldSupport getTranslatedFieldSupport();
100
101    /**
102     * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
103     */
104    public boolean isRequired()
105    {
106        return getValidatableFieldSupport().isRequired(this);
107    }
108}