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