Package org.apache.tapestry.form

Source Code of org.apache.tapestry.form.AbstractTextField

// Copyright 2004, 2005 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package org.apache.tapestry.form;

import org.apache.tapestry.IForm;
import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;

/**
* Base class for implementing various types of text input fields. This includes {@link TextField}
* and {@link org.apache.tapestry.valid.ValidField}.
*
* @author Howard Lewis Ship
* @since 1.0.2
*/

public abstract class AbstractTextField extends AbstractFormComponent
{
    /**
     * Renders the form element, or responds when the form containing the element is submitted (by
     * checking {@link Form#isRewinding()}.
     */

    protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
    {
        IForm form = getForm(cycle);

        if (form.wasPrerendered(writer, this))
            return;

        // It isn't enough to know whether the cycle in general is rewinding, need to know
        // specifically if the form which contains this component is rewinding.

        boolean rewinding = form.isRewinding();

        // If the cycle is rewinding, but the form containing this field is not,
        // then there's no point in doing more work.

        if (!rewinding && cycle.isRewinding())
            return;

        // Used whether rewinding or not.

        String name = form.getElementId(this);

        if (rewinding)
        {
            if (!isDisabled())
            {
                String value = cycle.getParameter(name);

                updateValue(value);
            }

            return;
        }

        writer.beginEmpty("input");

        writer.attribute("type", isHidden() ? "password" : "text");

        if (isDisabled())
            writer.attribute("disabled", "disabled");

        writer.attribute("name", name);

        String value = readValue();
        if (value != null)
            writer.attribute("value", value);

        renderInformalParameters(writer, cycle);

        beforeCloseTag(writer, cycle);

        writer.closeTag();
    }

    /**
     * Invoked from {@link #render(IMarkupWriter, IRequestCycle)}just before the tag is closed.
     * This implementation does nothing, subclasses may override.
     */

    protected void beforeCloseTag(IMarkupWriter writer, IRequestCycle cycle)
    {
        // Do nothing.
    }

    /**
     * Invoked by {@link #render(IMarkupWriter writer, IRequestCycle cycle)}when a value is
     * obtained from the {@link javax.servlet.http.HttpServletRequest}.
     */

    abstract protected void updateValue(String value);

    /**
     * Invoked by {@link #render(IMarkupWriter writer, IRequestCycle cycle)}when rendering a
     * response.
     *
     * @return the current value for the field, as a String, or null.
     */

    abstract protected String readValue();

    public abstract boolean isHidden();

    public abstract boolean isDisabled();
}
TOP

Related Classes of org.apache.tapestry.form.AbstractTextField

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.