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    
020    /**
021     * Implementation of {@link IPropertySelectionRenderer} that produces a
022     * <select> element (containing <option> elements).
023     * 
024     * @author Howard Lewis Ship
025     */
026    
027    public class SelectPropertySelectionRenderer implements
028            IPropertySelectionRenderer
029    {
030    
031        /**
032         * Writes the <select> element. If the {@link PropertySelection} is
033         * {@link PropertySelection#isDisabled() disabled} then a
034         * <code>disabled</code> attribute is written into the tag (though
035         * Navigator 4 will ignore this).
036         */
037    
038        public void beginRender(PropertySelection component, IMarkupWriter writer,
039                IRequestCycle cycle)
040        {
041            writer.begin("select");
042            writer.attribute("name", component.getName());
043    
044            if (component.isDisabled()) writer.attribute("disabled", "disabled");
045    
046            writer.println();
047        }
048    
049        /**
050         * Closes the &lt;select&gt; element.
051         */
052    
053        public void endRender(PropertySelection component, IMarkupWriter writer,
054                IRequestCycle cycle)
055        {
056            writer.end(); // <select>
057        }
058    
059        /**
060         * Writes an &lt;option&gt; element.
061         */
062    
063        public void renderOption(PropertySelection component, IMarkupWriter writer,
064                IRequestCycle cycle, IPropertySelectionModel model, Object option,
065                int index, boolean selected)
066        {
067            writer.beginEmpty("option");
068            writer.attribute("value", model.getValue(index));
069    
070            if (selected) writer.attribute("selected", "selected");
071    
072            writer.print(model.getLabel(index));
073    
074            writer.end();
075    
076            writer.println();
077        }
078    }