Clover coverage report - Code Coverage for tapestry-contrib release 4.0-beta-10
Coverage timestamp: Sat Oct 8 2005 19:13:41 EDT
file stats: LOC: 202   Methods: 4
NCLOC: 70   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MultiplePropertySelection.java 0% 0% 0% 0%
coverage
 1    // Copyright 2004, 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.tapestry.contrib.form;
 16   
 17    import java.util.ArrayList;
 18    import java.util.List;
 19   
 20    import org.apache.tapestry.IMarkupWriter;
 21    import org.apache.tapestry.IRequestCycle;
 22    import org.apache.tapestry.Tapestry;
 23    import org.apache.tapestry.form.AbstractFormComponent;
 24    import org.apache.tapestry.form.IPropertySelectionModel;
 25    import org.apache.tapestry.form.ValidatableField;
 26    import org.apache.tapestry.form.ValidatableFieldSupport;
 27    import org.apache.tapestry.valid.ValidatorException;
 28   
 29    /**
 30    * A component which uses <input type=checkbox> to set a property of some object. Typically,
 31    * the values for the object are defined using an {@link org.apache.commons.lang.enum.Enum}. A
 32    * MultiplePropertySelection is dependent on an {link IPropertySelectionModel} to provide the list
 33    * of possible values.
 34    * <p>
 35    * Often, this is used to select one or more {@link org.apache.commons.lang.enum.Enum}to assign to
 36    * a property; the {@link org.apache.tapestry.form.EnumPropertySelectionModel}class simplifies
 37    * this.
 38    * <p>
 39    * The {@link org.apache.tapestry.contrib.palette.Palette}component is more powerful, but requires
 40    * client-side JavaScript and is not fully cross-browser compatible.
 41    * <p>
 42    * <table border=1>
 43    * <tr>
 44    * <td>Parameter</td>
 45    * <td>Type</td>
 46    * <td>Direction</td>
 47    * <td>Required</td>
 48    * <td>Default</td>
 49    * <td>Description</td>
 50    * </tr>
 51    * <tr>
 52    * <td>selectedList</td>
 53    * <td>java.util.List</td>
 54    * <td>in-out</td>
 55    * <td>yes</td>
 56    * <td>&nbsp;</td>
 57    * <td>The property to set. During rendering, this property is read, and sets the default value of
 58    * the options in the select. When the form is submitted, list is cleared, then has each selected
 59    * option added to it.</td>
 60    * </tr>
 61    * <tr>
 62    * <td>renderer</td>
 63    * <td>{@link IMultiplePropertySelectionRenderer}</td>
 64    * <td>in</td>
 65    * <td>no</td>
 66    * <td>shared instance of {@link CheckBoxMultiplePropertySelectionRenderer}</td>
 67    * <td>Defines the object used to render this component. The default renders a table of checkboxes.
 68    * </td>
 69    * </tr>
 70    * <tr>
 71    * <td>model</td>
 72    * <td>{@link IPropertySelectionModel}</td>
 73    * <td>in</td>
 74    * <td>yes</td>
 75    * <td>&nbsp;</td>
 76    * <td>The model provides a list of possible labels, and matches those labels against possible
 77    * values that can be assigned back to the property.</td>
 78    * </tr>
 79    * <tr>
 80    * <td>disabled</td>
 81    * <td>boolean</td>
 82    * <td>in</td>
 83    * <td>no</td>
 84    * <td>false</td>
 85    * <td>Controls whether the &lt;select&gt; is active or not. A disabled PropertySelection does not
 86    * update its value parameter.
 87    * <p>
 88    * Corresponds to the <code>disabled</code> HTML attribute.</td>
 89    * </tr>
 90    * </table>
 91    * <p>
 92    * Informal parameters are not allowed.
 93    * <p>
 94    * As of 4.0, this component can be validated.
 95    *
 96    * @author Sanjay Munjal
 97    */
 98   
 99    public abstract class MultiplePropertySelection extends AbstractFormComponent implements ValidatableField
 100    {
 101    /**
 102    * A shared instance of {@link CheckBoxMultiplePropertySelectionRenderer}.
 103    */
 104    public static final IMultiplePropertySelectionRenderer DEFAULT_CHECKBOX_RENDERER = new CheckBoxMultiplePropertySelectionRenderer();
 105   
 106    public abstract List getSelectedList();
 107   
 108    public abstract void setSelectedList(List selectedList);
 109   
 110  0 protected void finishLoad()
 111    {
 112  0 setRenderer(DEFAULT_CHECKBOX_RENDERER);
 113    }
 114   
 115  0 protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 116    {
 117  0 List selectedList = getSelectedList();
 118   
 119  0 if (selectedList == null)
 120  0 throw Tapestry.createRequiredParameterException(this, "selectedList");
 121   
 122  0 IPropertySelectionModel model = getModel();
 123   
 124  0 if (model == null)
 125  0 throw Tapestry.createRequiredParameterException(this, "model");
 126   
 127  0 IMultiplePropertySelectionRenderer renderer = getRenderer();
 128   
 129    // Start rendering
 130  0 renderer.beginRender(this, writer, cycle);
 131   
 132  0 int count = model.getOptionCount();
 133   
 134  0 for (int i = 0; i < count; i++)
 135    {
 136  0 Object option = model.getOption(i);
 137   
 138    // Try to find the option in the list and if yes, then it is checked.
 139  0 boolean optionSelected = selectedList.contains(option);
 140   
 141  0 renderer.renderOption(this, writer, cycle, model, option, i, optionSelected);
 142    }
 143   
 144    // A PropertySelection doesn't allow a body, so no need to worry about
 145    // wrapped components.
 146  0 renderer.endRender(this, writer, cycle);
 147    }
 148   
 149    /**
 150    * @see org.apache.tapestry.form.AbstractRequirableField#rewindFormComponent(org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle)
 151    */
 152  0 protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
 153    {
 154    // get all the values
 155  0 String[] optionValues = cycle.getParameters(getName());
 156   
 157  0 IPropertySelectionModel model = getModel();
 158   
 159  0 List selectedList = new ArrayList(getModel().getOptionCount());
 160   
 161    // Nothing was selected
 162  0 if (optionValues != null)
 163    {
 164    // Go through the array and translate and put back in the list
 165  0 for (int i = 0; i < optionValues.length; i++)
 166    {
 167    // Translate the new value
 168  0 Object selectedValue = model.translateValue(optionValues[i]);
 169   
 170    // Add this element in the list back
 171  0 selectedList.add(selectedValue);
 172    }
 173    }
 174   
 175  0 try
 176    {
 177  0 getValidatableFieldSupport().validate(this, writer, cycle, selectedList);
 178   
 179  0 setSelectedList(selectedList);
 180    }
 181    catch (ValidatorException e)
 182    {
 183  0 getForm().getDelegate().record(e);
 184    }
 185    }
 186   
 187    public abstract IPropertySelectionModel getModel();
 188   
 189    public abstract IMultiplePropertySelectionRenderer getRenderer();
 190   
 191    public abstract void setRenderer(IMultiplePropertySelectionRenderer renderer);
 192   
 193    public abstract ValidatableFieldSupport getValidatableFieldSupport();
 194   
 195    /**
 196    * @see org.apache.tapestry.form.AbstractFormComponent#isRequired()
 197    */
 198  0 public boolean isRequired()
 199    {
 200  0 return getValidatableFieldSupport().isRequired(this);
 201    }
 202    }