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