Clover coverage report - Code Coverage for tapestry-contrib release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:12:41 EDT
file stats: LOC: 203   Methods: 2
NCLOC: 59   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 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.List;
 18   
 
 19   
 import org.apache.tapestry.IForm;
 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   
 
 26   
 /**
 27   
  * A component which uses <input type=checkbox> to set a property of some object. Typically,
 28   
  * the values for the object are defined using an {@link org.apache.commons.lang.enum.Enum}. A
 29   
  * MultiplePropertySelection is dependent on an {link IPropertySelectionModel} to provide the list
 30   
  * of possible values.
 31   
  * <p>
 32   
  * Often, this is used to select one or more {@link org.apache.commons.lang.enum.Enum}to assign to
 33   
  * a property; the {@link org.apache.tapestry.form.EnumPropertySelectionModel}class simplifies
 34   
  * this.
 35   
  * <p>
 36   
  * The {@link org.apache.tapestry.contrib.palette.Palette}component is more powerful, but requires
 37   
  * client-side JavaScript and is not fully cross-browser compatible.
 38   
  * <p>
 39   
  * <table border=1>
 40   
  * <tr>
 41   
  * <td>Parameter</td>
 42   
  * <td>Type</td>
 43   
  * <td>Direction</td>
 44   
  * <td>Required</td>
 45   
  * <td>Default</td>
 46   
  * <td>Description</td>
 47   
  * </tr>
 48   
  * <tr>
 49   
  * <td>selectedList</td>
 50   
  * <td>java.util.List</td>
 51   
  * <td>in-out</td>
 52   
  * <td>yes</td>
 53   
  * <td>&nbsp;</td>
 54   
  * <td>The property to set. During rendering, this property is read, and sets the default value of
 55   
  * the options in the select. When the form is submitted, list is cleared, then has each selected
 56   
  * option added to it.</td>
 57   
  * </tr>
 58   
  * <tr>
 59   
  * <td>renderer</td>
 60   
  * <td>{@link IMultiplePropertySelectionRenderer}</td>
 61   
  * <td>in</td>
 62   
  * <td>no</td>
 63   
  * <td>shared instance of {@link CheckBoxMultiplePropertySelectionRenderer}</td>
 64   
  * <td>Defines the object used to render this component. The default renders a table of checkboxes.
 65   
  * </td>
 66   
  * </tr>
 67   
  * <tr>
 68   
  * <td>model</td>
 69   
  * <td>{@link IPropertySelectionModel}</td>
 70   
  * <td>in</td>
 71   
  * <td>yes</td>
 72   
  * <td>&nbsp;</td>
 73   
  * <td>The model provides a list of possible labels, and matches those labels against possible
 74   
  * values that can be assigned back to the property.</td>
 75   
  * </tr>
 76   
  * <tr>
 77   
  * <td>disabled</td>
 78   
  * <td>boolean</td>
 79   
  * <td>in</td>
 80   
  * <td>no</td>
 81   
  * <td>false</td>
 82   
  * <td>Controls whether the &lt;select&gt; is active or not. A disabled PropertySelection does not
 83   
  * update its value parameter.
 84   
  * <p>
 85   
  * Corresponds to the <code>disabled</code> HTML attribute.</td>
 86   
  * </tr>
 87   
  * </table>
 88   
  * <p>
 89   
  * Informal parameters are not allowed.
 90   
  * 
 91   
  * @author Sanjay Munjal
 92   
  */
 93   
 
 94   
 public abstract class MultiplePropertySelection extends AbstractFormComponent
 95   
 {
 96   
 
 97   
     /**
 98   
      * A shared instance of {@link CheckBoxMultiplePropertySelectionRenderer}.
 99   
      */
 100   
 
 101   
     public static final IMultiplePropertySelectionRenderer DEFAULT_CHECKBOX_RENDERER = new CheckBoxMultiplePropertySelectionRenderer();
 102   
 
 103  0
     protected void finishLoad()
 104   
     {
 105  0
         setRenderer(DEFAULT_CHECKBOX_RENDERER);
 106   
     }
 107   
 
 108   
     /**
 109   
      * Returns true if the component is disabled (this is relevant to the renderer).
 110   
      */
 111   
 
 112   
     public abstract boolean isDisabled();
 113   
 
 114   
     /**
 115   
      * Renders the component, much of which is the responsiblity of the
 116   
      * {@link IMultiplePropertySelectionRenderer renderer}. The possible options, their labels, and
 117   
      * the values to be encoded in the form are provided by the
 118   
      * {@link IPropertySelectionModel model}.
 119   
      */
 120   
 
 121  0
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 122   
     {
 123  0
         IForm form = getForm(cycle);
 124   
 
 125  0
         if (form.wasPrerendered(writer, this))
 126  0
             return;
 127   
 
 128  0
         boolean rewinding = form.isRewinding();
 129   
 
 130  0
         String name = form.getElementId(this);
 131   
 
 132  0
         List selectedList = (List) getBinding("selectedList").getObject(List.class);
 133   
 
 134  0
         if (selectedList == null)
 135  0
             throw Tapestry.createRequiredParameterException(this, "selectedList");
 136   
 
 137  0
         IPropertySelectionModel model = getModel();
 138   
 
 139  0
         if (model == null)
 140  0
             throw Tapestry.createRequiredParameterException(this, "model");
 141   
 
 142   
         // Handle the form processing first.
 143  0
         if (rewinding)
 144   
         {
 145   
             // If disabled, ignore anything that comes up from the client.
 146   
 
 147  0
             if (isDisabled())
 148  0
                 return;
 149   
 
 150   
             // get all the values
 151  0
             String[] optionValues = cycle.getParameters(name);
 152   
 
 153   
             // Clear the list
 154   
 
 155  0
             selectedList.clear();
 156   
 
 157   
             // Nothing was selected
 158  0
             if (optionValues != null)
 159   
             {
 160   
 
 161   
                 // Go through the array and translate and put back in the list
 162  0
                 for (int i = 0; i < optionValues.length; i++)
 163   
                 {
 164   
                     // Translate the new value
 165  0
                     Object selectedValue = model.translateValue(optionValues[i]);
 166   
 
 167   
                     // Add this element in the list back
 168  0
                     selectedList.add(selectedValue);
 169   
                 }
 170   
             }
 171   
 
 172  0
             return;
 173   
         }
 174   
 
 175  0
         IMultiplePropertySelectionRenderer renderer = getRenderer();
 176   
 
 177   
         // Start rendering
 178  0
         renderer.beginRender(this, writer, cycle);
 179   
 
 180  0
         int count = model.getOptionCount();
 181   
 
 182  0
         for (int i = 0; i < count; i++)
 183   
         {
 184  0
             Object option = model.getOption(i);
 185   
 
 186   
             // Try to find the option in the list and if yes, then it is checked.
 187  0
             boolean optionSelected = selectedList.contains(option);
 188   
 
 189  0
             renderer.renderOption(this, writer, cycle, model, option, i, optionSelected);
 190   
         }
 191   
 
 192   
         // A PropertySelection doesn't allow a body, so no need to worry about
 193   
         // wrapped components.
 194  0
         renderer.endRender(this, writer, cycle);
 195   
     }
 196   
 
 197   
     public abstract IPropertySelectionModel getModel();
 198   
 
 199   
     public abstract IMultiplePropertySelectionRenderer getRenderer();
 200   
 
 201   
     public abstract void setRenderer(IMultiplePropertySelectionRenderer renderer);
 202   
 
 203   
 }