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.wml;
016
017import org.apache.hivemind.ApplicationRuntimeException;
018import org.apache.hivemind.HiveMind;
019import org.apache.tapestry.AbstractComponent;
020import org.apache.tapestry.IMarkupWriter;
021import org.apache.tapestry.IRequestCycle;
022import org.apache.tapestry.Tapestry;
023
024/**
025 * The Select element lets users pick from a list of options. Each option is specified by an Option
026 * element. Each Option element may have one line of formatted text (which may be wrapped or
027 * truncated by the user agent if too long). Unless multiple selections are required it is generally
028 * easier to use the {@link PropertySelection}component.
029 * 
030 * @author David Solis
031 * @since 3.0
032 */
033public abstract class Select extends AbstractComponent
034{
035    /**
036     * Used by the <code>Select</code> to record itself as a {@link IRequestCycle}attribute, so
037     * that the {@link Option}components it wraps can have access to it.
038     */
039
040    private final static String ATTRIBUTE_NAME = "org.apache.tapestry.active.Select";
041
042    /**
043     * @see org.apache.tapestry.AbstractComponent#renderComponent(IMarkupWriter, IRequestCycle)
044     */
045
046    protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
047    {
048        if (cycle.getAttribute(ATTRIBUTE_NAME) != null)
049            throw new ApplicationRuntimeException(Tapestry.getMessage("Select.may-not-nest"), this,
050                    null, null);
051
052        cycle.setAttribute(ATTRIBUTE_NAME, this);
053
054        boolean render = !cycle.isRewinding();
055
056        if (render)
057        {
058            writer.begin("select");
059
060            writer.attribute("name", getName());
061
062            String value = getValue();
063            if (HiveMind.isNonBlank(value))
064                writer.attribute("value", value);
065
066            String title = getTitle();
067            if (HiveMind.isNonBlank(title))
068                writer.attribute("title", title);
069
070            boolean multiple = isMultiple();
071            if (multiple)
072                writer.attribute("multiple", multiple);
073
074            renderInformalParameters(writer, cycle);
075        }
076
077        renderBody(writer, cycle);
078
079        if (render)
080        {
081            writer.end();
082        }
083
084        cycle.removeAttribute(ATTRIBUTE_NAME);
085    }
086
087    public abstract boolean isMultiple();
088
089    public abstract String getName();
090
091    public abstract String getValue();
092
093    public abstract String getTitle();
094}