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