Coverage Report - org.apache.tapestry.form.LabeledPropertySelectionModel
 
Classes in this File Line Coverage Branch Coverage Complexity
LabeledPropertySelectionModel
67% 
100% 
1.08
 
 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  
 /**
 18  
  * Decorates an underlying {@link IPropertySelectionModel}adding an initial property. The label,
 19  
  * option, and value of the initial property are configurable.
 20  
  * 
 21  
  * @author Paul Ferraro
 22  
  * @since 4.0
 23  
  */
 24  
 public class LabeledPropertySelectionModel implements IPropertySelectionModel
 25  
 {
 26  
     /**
 27  
      * Empty model implementation. Avoids NullPointerExceptions when default constructor is used.
 28  
      */
 29  1
     private static final IPropertySelectionModel EMPTY_MODEL = new IPropertySelectionModel()
 30  1
     {
 31  
         /**
 32  
          * @see org.apache.tapestry.form.IPropertySelectionModel#getOptionCount()
 33  
          */
 34  
         public int getOptionCount()
 35  
         {
 36  2
             return 0;
 37  
         }
 38  
 
 39  
         /**
 40  
          * @see org.apache.tapestry.form.IPropertySelectionModel#getOption(int)
 41  
          */
 42  
         public Object getOption(int index)
 43  
         {
 44  0
             return null;
 45  
         }
 46  
 
 47  
         /**
 48  
          * @see org.apache.tapestry.form.IPropertySelectionModel#getLabel(int)
 49  
          */
 50  
         public String getLabel(int index)
 51  
         {
 52  0
             return null;
 53  
         }
 54  
 
 55  
         /**
 56  
          * @see org.apache.tapestry.form.IPropertySelectionModel#getValue(int)
 57  
          */
 58  
         public String getValue(int index)
 59  
         {
 60  0
             return null;
 61  
         }
 62  
 
 63  
         public boolean isDisabled(int index)
 64  
         {
 65  0
             return false;
 66  
         }
 67  
         
 68  
         /**
 69  
          * @see org.apache.tapestry.form.IPropertySelectionModel#translateValue(java.lang.String)
 70  
          */
 71  
         public Object translateValue(String value)
 72  
         {
 73  0
             return null;
 74  
         }
 75  
     };
 76  
     
 77  
     private IPropertySelectionModel _model;
 78  
 
 79  4
     private String _label = "";
 80  
 
 81  4
     private Object _option = null;
 82  
 
 83  4
     private String _value = "";
 84  
     
 85  
     /**
 86  
      * Constructs a new LabeledPropertySelectionModel using an empty model and default label,
 87  
      * option, and value. Default constructor is made available so that this model may be specified
 88  
      * as a component helper bean.
 89  
      */
 90  
     public LabeledPropertySelectionModel()
 91  
     {
 92  2
         this(EMPTY_MODEL);
 93  2
     }
 94  
 
 95  
     /**
 96  
      * Constructs a new LabeledPropertySelectionModel using the specified model and default label,
 97  
      * option, and value.
 98  
      * 
 99  
      * @param model
 100  
      *            the underlying model to decorate
 101  
      */
 102  
     public LabeledPropertySelectionModel(IPropertySelectionModel model)
 103  4
     {
 104  4
         _model = model;
 105  4
     }
 106  
 
 107  
     /**
 108  
      * Constructs a new LabeledPropertySelectionModel using the specified model and label, and
 109  
      * default option and value.
 110  
      * 
 111  
      * @param model
 112  
      *            the underlying model to decorate
 113  
      * @param label
 114  
      *            the label of the initial property
 115  
      */
 116  
     public LabeledPropertySelectionModel(IPropertySelectionModel model, String label)
 117  
     {
 118  1
         this(model);
 119  
 
 120  1
         _label = label;
 121  1
     }
 122  
 
 123  
     /**
 124  
      * Constructs a new LabeledPropertySelectionModel using the specified model, label, and option;
 125  
      * and default value.
 126  
      * 
 127  
      * @param model
 128  
      *            the underlying model to decorate
 129  
      * @param label
 130  
      *            the label of the initial property
 131  
      * @param option
 132  
      *            the option value of the initial property
 133  
      */
 134  
     public LabeledPropertySelectionModel(IPropertySelectionModel model, String label, Object option)
 135  
     {
 136  1
         this(model, label);
 137  
 
 138  1
         _option = option;
 139  1
     }
 140  
 
 141  
     /**
 142  
      * Constructs a new LabeledPropertySelectionModel using the specified model, label, option, and
 143  
      * value.
 144  
      * 
 145  
      * @param model
 146  
      *            the underlying model to decorate
 147  
      * @param label
 148  
      *            the label of the initial property
 149  
      * @param option
 150  
      *            the option value of the initial property
 151  
      * @param value
 152  
      *            the value of the initial property
 153  
      */
 154  
     public LabeledPropertySelectionModel(IPropertySelectionModel model, String label,
 155  
             Object option, String value)
 156  
     {
 157  1
         this(model, label, option);
 158  
 
 159  1
         _value = value;
 160  1
     }
 161  
 
 162  
     /**
 163  
      * Returns the underlying IPropertySelectionModel.
 164  
      * 
 165  
      * @return the underlying IPropertySelectionModel
 166  
      */
 167  
     public IPropertySelectionModel getModel()
 168  
     {
 169  0
         return _model;
 170  
     }
 171  
 
 172  
     /**
 173  
      * Sets the underlying IPropertySelectionModel.
 174  
      * 
 175  
      * @param model
 176  
      *            the IPropertySelectionModel to set
 177  
      */
 178  
     public void setModel(IPropertySelectionModel model)
 179  
     {
 180  0
         _model = model;
 181  0
     }
 182  
 
 183  
     /**
 184  
      * @see org.apache.tapestry.form.IPropertySelectionModel#getOptionCount()
 185  
      */
 186  
     public int getOptionCount()
 187  
     {
 188  6
         return _model.getOptionCount() + 1;
 189  
     }
 190  
 
 191  
     /**
 192  
      * @see org.apache.tapestry.form.IPropertySelectionModel#getOption(int)
 193  
      */
 194  
     public Object getOption(int index)
 195  
     {
 196  7
         return (index == 0) ? _option : _model.getOption(index - 1);
 197  
     }
 198  
 
 199  
     /**
 200  
      * @see org.apache.tapestry.form.IPropertySelectionModel#getLabel(int)
 201  
      */
 202  
     public String getLabel(int index)
 203  
     {
 204  7
         return (index == 0) ? _label : _model.getLabel(index - 1);
 205  
     }
 206  
 
 207  
     /**
 208  
      * @see org.apache.tapestry.form.IPropertySelectionModel#getValue(int)
 209  
      */
 210  
     public String getValue(int index)
 211  
     {
 212  7
         return (index == 0) ? _value : _model.getValue(index - 1);
 213  
     }
 214  
 
 215  
     public boolean isDisabled(int index)
 216  
     {
 217  0
         return false;
 218  
     }
 219  
     
 220  
     /**
 221  
      * @see org.apache.tapestry.form.IPropertySelectionModel#translateValue(java.lang.String)
 222  
      */
 223  
     public Object translateValue(String value)
 224  
     {
 225  8
         if (value == null)
 226  1
             return null;
 227  
         
 228  7
         return value.equals(_value) ? _option : _model.translateValue(value);
 229  
     }
 230  
 
 231  
     /**
 232  
      * Returns the label of the initial IPropertySelectionModel option.
 233  
      * 
 234  
      * @return a IPropertySelectionModel option label
 235  
      */
 236  
     public String getLabel()
 237  
     {
 238  1
         return _label;
 239  
     }
 240  
 
 241  
     /**
 242  
      * Sets the label of the initial IPropertySelectionModel option.
 243  
      * 
 244  
      * @param label
 245  
      *            a IPropertySelectionModel option label
 246  
      */
 247  
     public void setLabel(String label)
 248  
     {
 249  0
         _label = label;
 250  0
     }
 251  
 
 252  
     /**
 253  
      * Returns the value of the initial IPropertySelectionModel option.
 254  
      * 
 255  
      * @return a IPropertySelectionModel option value
 256  
      */
 257  
     public String getValue()
 258  
     {
 259  1
         return _value;
 260  
     }
 261  
 
 262  
     /**
 263  
      * Sets the value of the initial IPropertySelectionModel option.
 264  
      * 
 265  
      * @param value
 266  
      *            a IPropertySelectionModel option value
 267  
      */
 268  
     public void setValue(String value)
 269  
     {
 270  0
         _value = value;
 271  0
     }
 272  
 
 273  
     /**
 274  
      * Returns the initial option.
 275  
      * 
 276  
      * @return a PropertySelectionModel option
 277  
      */
 278  
     public Object getOption()
 279  
     {
 280  1
         return _option;
 281  
     }
 282  
 
 283  
     /**
 284  
      * Sets the initial IPropertySelectionModel option.
 285  
      * 
 286  
      * @param option
 287  
      *            a IPropertySelectionModel option
 288  
      */
 289  
     public void setOption(Object option)
 290  
     {
 291  0
         _option = option;
 292  0
     }
 293  
 }