Clover coverage report - Code Coverage for tapestry release 3.1-alpha-1
Coverage timestamp: Mon Feb 21 2005 09:16:14 EST
file stats: LOC: 247   Methods: 3
NCLOC: 105   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
PropertySelection.java 59.4% 65.6% 66.7% 63.6%
coverage 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.form;
 16   
 
 17   
 import org.apache.tapestry.IForm;
 18   
 import org.apache.tapestry.IMarkupWriter;
 19   
 import org.apache.tapestry.IRequestCycle;
 20   
 import org.apache.tapestry.Tapestry;
 21   
 
 22   
 /**
 23   
  *  A component used to render a drop-down list of options that
 24   
  *  the user may select.
 25   
  * 
 26   
  *  [<a href="../../../../../ComponentReference/PropertySelection.html">Component Reference</a>]
 27   
  *
 28   
  *  <p>Earlier versions of PropertySelection (through release 2.2)
 29   
  *  were more flexible, they included a <b>renderer</b> property
 30   
  *  that controlled how the selection was rendered.  Ultimately,
 31   
  *  this proved of little value and this portion of
 32   
  *  functionality was deprecated in 2.3 and will be removed in 2.3.
 33   
  * 
 34   
  *  <p>Typically, the values available to be selected
 35   
  *  are defined using an {@link org.apache.commons.lang.enum.Enum}.
 36   
  *  A PropertySelection is dependent on
 37   
  *  an {@link IPropertySelectionModel} to provide the list of possible values.
 38   
  *
 39   
  *  <p>Often, this is used to select a particular 
 40   
  *  {@link org.apache.commons.lang.enum.Enum} to assign to a property; the
 41   
  *  {@link EnumPropertySelectionModel} class simplifies this.
 42   
  *
 43   
  *
 44   
  *  @author Howard Lewis Ship
 45   
  *
 46   
  **/
 47   
 
 48   
 public abstract class PropertySelection extends AbstractFormComponent
 49   
 {
 50   
     /**
 51   
      *  A shared instance of {@link SelectPropertySelectionRenderer}.
 52   
      *
 53   
      * 
 54   
      **/
 55   
 
 56   
     public static final IPropertySelectionRenderer DEFAULT_SELECT_RENDERER =
 57   
         new SelectPropertySelectionRenderer();
 58   
 
 59   
     /**
 60   
      *  A shared instance of {@link RadioPropertySelectionRenderer}.
 61   
      *
 62   
      * 
 63   
      **/
 64   
 
 65   
     public static final IPropertySelectionRenderer DEFAULT_RADIO_RENDERER =
 66   
         new RadioPropertySelectionRenderer();
 67   
 
 68   
     /**
 69   
      *  Renders the component, much of which is the responsiblity
 70   
      *  of the {@link IPropertySelectionRenderer renderer}.  The possible options,
 71   
      *  thier labels, and the values to be encoded in the form are provided
 72   
      *  by the {@link IPropertySelectionModel model}.
 73   
      *
 74   
      **/
 75   
 
 76  22
     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
 77   
     {
 78  22
         IForm form = getForm(cycle);
 79   
 
 80  22
         boolean rewinding = form.isRewinding();
 81   
 
 82  22
         String name = form.getElementId(this);
 83   
 
 84  21
         if (rewinding)
 85   
         {
 86   
             // If disabled, ignore anything that comes up from the client.
 87   
 
 88  11
             if (isDisabled())
 89  0
                 return;
 90   
 
 91  11
             String optionValue = cycle.getRequestContext().getParameter(name);
 92   
 
 93  11
             Object value = (optionValue == null) ? null : getModel().translateValue(optionValue);
 94   
 
 95  11
             setValue(value);
 96   
 
 97  11
             return;
 98   
         }
 99   
 
 100  10
         IPropertySelectionRenderer renderer = getRenderer();
 101   
 
 102  10
         if (renderer != null)
 103   
         {
 104  0
             renderWithRenderer(writer, cycle, renderer);
 105  0
             return;
 106   
         }
 107   
 
 108  10
         writer.begin("select");
 109  10
         writer.attribute("name", name);
 110   
 
 111  10
         if (isDisabled())
 112  0
             writer.attribute("disabled", "disabled");
 113   
 
 114  10
         if (getSubmitOnChange())
 115  0
             writer.attribute("onchange", "javascript:this.form.submit();");
 116   
 
 117   
         // Apply informal attributes.
 118   
 
 119  10
         renderInformalParameters(writer, cycle);
 120   
 
 121  10
         writer.println();
 122   
 
 123  10
         IPropertySelectionModel model = getModel();
 124   
 
 125  10
         if (model == null)
 126  0
             throw Tapestry.createRequiredParameterException(this, "model");
 127   
 
 128  10
         int count = model.getOptionCount();
 129  10
         boolean foundSelected = false;
 130  10
         boolean selected = false;
 131  10
         Object value = getValue();
 132   
 
 133  10
         for (int i = 0; i < count; i++)
 134   
         {
 135  44
             Object option = model.getOption(i);
 136   
 
 137  44
             if (!foundSelected)
 138   
             {
 139  24
                 selected = isEqual(option, value);
 140  24
                 if (selected)
 141  10
                     foundSelected = true;
 142   
             }
 143   
 
 144  44
             writer.begin("option");
 145  44
             writer.attribute("value", model.getValue(i));
 146   
 
 147  44
             if (selected)
 148  10
                 writer.attribute("selected", "selected");
 149   
 
 150  44
             writer.print(model.getLabel(i));
 151   
 
 152  44
             writer.end();
 153   
 
 154  44
             writer.println();
 155   
 
 156  44
             selected = false;
 157   
         }
 158   
 
 159  10
         writer.end(); // <select>
 160   
 
 161   
     }
 162   
 
 163   
     /**
 164   
      *  Renders the property selection using a {@link IPropertySelectionRenderer}.
 165   
      *  Support for this will be removed in 2.3.
 166   
      * 
 167   
      **/
 168   
 
 169  0
     private void renderWithRenderer(
 170   
         IMarkupWriter writer,
 171   
         IRequestCycle cycle,
 172   
         IPropertySelectionRenderer renderer)
 173   
     {
 174  0
         renderer.beginRender(this, writer, cycle);
 175   
 
 176  0
         IPropertySelectionModel model = getModel();
 177   
 
 178  0
         int count = model.getOptionCount();
 179   
 
 180  0
         boolean foundSelected = false;
 181  0
         boolean selected = false;
 182   
 
 183  0
         Object value = getValue();
 184   
 
 185  0
         for (int i = 0; i < count; i++)
 186   
         {
 187  0
             Object option = model.getOption(i);
 188   
 
 189  0
             if (!foundSelected)
 190   
             {
 191  0
                 selected = isEqual(option, value);
 192  0
                 if (selected)
 193  0
                     foundSelected = true;
 194   
             }
 195   
 
 196  0
             renderer.renderOption(this, writer, cycle, model, option, i, selected);
 197   
 
 198  0
             selected = false;
 199   
         }
 200   
 
 201   
         // A PropertySelection doesn't allow a body, so no need to worry about
 202   
         // wrapped components.
 203   
 
 204  0
         renderer.endRender(this, writer, cycle);
 205   
     }
 206   
 
 207  24
     private boolean isEqual(Object left, Object right)
 208   
     {
 209   
         // Both null, or same object, then are equal
 210   
 
 211  24
         if (left == right)
 212  10
             return true;
 213   
 
 214   
         // If one is null, the other isn't, then not equal.
 215   
 
 216  14
         if (left == null || right == null)
 217  0
             return false;
 218   
 
 219   
         // Both non-null; use standard comparison.
 220   
 
 221  14
         return left.equals(right);
 222   
     }
 223   
 
 224   
     public abstract IPropertySelectionModel getModel();
 225   
 
 226   
     public abstract IPropertySelectionRenderer getRenderer();
 227   
 
 228   
     /** @since 2.2 **/
 229   
 
 230   
     public abstract boolean getSubmitOnChange();
 231   
 
 232   
     /** @since 2.2 **/
 233   
 
 234   
     public abstract Object getValue();
 235   
 
 236   
     /** @since 2.2 **/
 237   
 
 238   
     public abstract void setValue(Object value);
 239   
 
 240   
     /**
 241   
      *  Returns true if this PropertySelection's disabled parameter yields true.
 242   
      *  The corresponding HTML control(s) should be disabled.
 243   
      **/
 244   
 
 245   
     public abstract boolean isDisabled();
 246   
 
 247   
 }