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.contrib.form;
016    
017    import org.apache.tapestry.IMarkupWriter;
018    import org.apache.tapestry.IRequestCycle;
019    import 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    
029    public 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            String id = component.getName() + "." +model.getValue(index);
084            writer.attribute("id", id);
085            writer.attribute("value", model.getValue(index));
086    
087            if (component.isDisabled())
088                writer.attribute("disabled", "disabled");
089    
090            if (selected)
091                writer.attribute("checked", "checked");
092    
093            writer.end(); // <td>
094    
095            writer.println();
096    
097            writer.begin("td");
098            writer.begin("label");
099            writer.attribute("for", id);
100            writer.print(model.getLabel(index));
101            writer.end(); // <label>
102            writer.end(); // <td>
103            writer.end(); // <tr>
104    
105            writer.println();
106        }
107    }