Coverage Report - org.apache.tapestry.form.BeanPropertySelectionModel
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanPropertySelectionModel
58% 
N/A 
1.333
 
 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  
 package org.apache.tapestry.form;
 15  
 
 16  
 import java.io.Serializable;
 17  
 import java.util.ArrayList;
 18  
 import java.util.Arrays;
 19  
 import java.util.Collection;
 20  
 import java.util.List;
 21  
 
 22  
 import org.apache.commons.beanutils.BeanUtils;
 23  
 
 24  
 /**
 25  
  * This class is a property selection model for an object list. This is used in PropertySelection,
 26  
  * MultiplePropertySelection or Palette tapestry components. For example, to use for a Hospital
 27  
  * class, and have the labels be the hospital names. <code>
 28  
  * List&lt;Hospital&gt; list = ...;
 29  
  * return new BeanPropertySelectionModel(hospitals, "name");
 30  
  * </code>
 31  
  * This will use getName() on the Hospital object, as its display.
 32  
  * 
 33  
  * @author Gabriel Handford
 34  
  */
 35  
 public class BeanPropertySelectionModel implements IPropertySelectionModel, Serializable
 36  
 {
 37  
 
 38  
     /** Comment for <code>serialVersionUID</code>. */
 39  
     private static final long serialVersionUID = 3763091973006766644L;
 40  
     private List _list;
 41  
     private String _labelField;
 42  
 
 43  
     /**
 44  
      * Build an empty property selection model.
 45  
      */
 46  
     public BeanPropertySelectionModel()
 47  
     {
 48  1
         this(Arrays.asList(new Object[0]), null);
 49  1
     }
 50  
 
 51  
     /**
 52  
      * Build a bean property selection model.
 53  
      * 
 54  
      * @param list
 55  
      *            The list
 56  
      * @param labelField
 57  
      *            The label field
 58  
      */
 59  
     public BeanPropertySelectionModel(List list, String labelField)
 60  2
     {
 61  2
         _list = list;
 62  2
         _labelField = labelField;
 63  2
     }
 64  
 
 65  
     /**
 66  
      * Build a bean property selection model.
 67  
      * 
 68  
      * @param c
 69  
      *            Collection
 70  
      * @param labelField
 71  
      *            The label field
 72  
      */
 73  
     public BeanPropertySelectionModel(Collection c, String labelField)
 74  0
     {
 75  0
         _list = new ArrayList(c);
 76  0
         _labelField = labelField;
 77  0
     }
 78  
 
 79  
     /**
 80  
      * Get the number of options.
 81  
      * 
 82  
      * @return option count
 83  
      */
 84  
     public int getOptionCount()
 85  
     {
 86  2
         return _list.size();
 87  
     }
 88  
 
 89  
     /**
 90  
      * Get the option at index.
 91  
      * 
 92  
      * @param index
 93  
      *            Index
 94  
      * @return object Object at index
 95  
      */
 96  
     public Object getOption(int index)
 97  
     {
 98  2
         return _list.get(index);
 99  
     }
 100  
 
 101  
     /**
 102  
      * Get the label at index.
 103  
      * 
 104  
      * @param index
 105  
      *            Index
 106  
      * @return label Label at index
 107  
      */
 108  
     public String getLabel(int index)
 109  
     {
 110  1
         Object obj = _list.get(index);
 111  
         try {
 112  
 
 113  1
             return BeanUtils.getProperty(obj, _labelField);
 114  0
         } catch (Exception e) {
 115  0
             throw new RuntimeException("Error getting property", e);
 116  
         }
 117  
     }
 118  
 
 119  
     /**
 120  
      * Get the value at index.
 121  
      * 
 122  
      * @param index
 123  
      *            Index
 124  
      * @return value Value at index
 125  
      */
 126  
     public String getValue(int index)
 127  
     {
 128  0
         return String.valueOf(index);
 129  
     }
 130  
 
 131  
     public boolean isDisabled(int index)
 132  
     {
 133  0
         return false;
 134  
     }
 135  
     
 136  
     /**
 137  
      * Translate value to object.
 138  
      * 
 139  
      * @param value
 140  
      *            Value
 141  
      * @return object Object from value
 142  
      */
 143  
     public Object translateValue(String value)
 144  
     {
 145  1
         return getOption(Integer.parseInt(value));
 146  
     }
 147  
 }