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.form;
016
017import org.apache.tapestry.IMarkupWriter;
018import org.apache.tapestry.IRequestCycle;
019
020/**
021 *  Implementation of {@link IPropertySelectionRenderer} that
022 *  produces a table of radio (<input type=radio>) elements.
023 *
024 *  @author Howard Lewis Ship
025 *
026 **/
027
028public class RadioPropertySelectionRenderer implements IPropertySelectionRenderer
029{
030
031    /**
032     *  Writes the <table> element.
033     *
034     **/
035
036    public void beginRender(PropertySelection component, IMarkupWriter writer, IRequestCycle cycle)
037    {
038        writer.begin("table");
039        writer.attribute("border", 0);
040        writer.attribute("cellpadding", 0);
041        writer.attribute("cellspacing", 2);
042    }
043
044    /**
045     *  Closes the <table> element.
046     *
047     **/
048
049    public void endRender(PropertySelection component, IMarkupWriter writer, IRequestCycle cycle)
050    {
051        writer.end(); // <table>
052    }
053
054    /**
055     *  Writes a row of the table.  The table contains two cells; the first is the radio
056     *  button, the second is the label for the radio button.
057     *
058     **/
059
060    public void renderOption(
061        PropertySelection component,
062        IMarkupWriter writer,
063        IRequestCycle cycle,
064        IPropertySelectionModel model,
065        Object option,
066        int index,
067        boolean selected)
068    {
069        writer.begin("tr");
070        writer.begin("td");
071
072        writer.beginEmpty("input");
073        writer.attribute("type", "radio");
074        writer.attribute("name", component.getName());
075        writer.attribute("value", model.getValue(index));
076
077        if (component.isDisabled())
078            writer.attribute("disabled", "disabled");
079
080        if (selected)
081            writer.attribute("checked", "checked");
082
083        writer.end(); // <td>
084
085        writer.println();
086
087        writer.begin("td");
088        writer.print(model.getLabel(index));
089        writer.end(); // <td>
090        writer.end(); // <tr>     
091
092        writer.println();
093    }
094}