001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    // http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    package org.apache.tapestry.form;
015    
016    import java.io.Serializable;
017    import java.util.ArrayList;
018    import java.util.Arrays;
019    import java.util.Collection;
020    import java.util.List;
021    
022    import org.apache.commons.beanutils.BeanUtils;
023    
024    /**
025     * This class is a property selection model for an object list. This is used in PropertySelection,
026     * MultiplePropertySelection or Palette tapestry components. For example, to use for a Hospital
027     * class, and have the labels be the hospital names. <code>
028     * List&lt;Hospital&gt; list = ...;
029     * return new BeanPropertySelectionModel(hospitals, "name");
030     * </code>
031     * This will use getName() on the Hospital object, as its display.
032     * 
033     * @author Gabriel Handford
034     */
035    public class BeanPropertySelectionModel implements IPropertySelectionModel, Serializable
036    {
037    
038        /** Comment for <code>serialVersionUID</code>. */
039        private static final long serialVersionUID = 3763091973006766644L;
040        private List _list;
041        private String _labelField;
042    
043        /**
044         * Build an empty property selection model.
045         */
046        public BeanPropertySelectionModel()
047        {
048            this(Arrays.asList(new Object[0]), null);
049        }
050    
051        /**
052         * Build a bean property selection model.
053         * 
054         * @param list
055         *            The list
056         * @param labelField
057         *            The label field
058         */
059        public BeanPropertySelectionModel(List list, String labelField)
060        {
061            _list = list;
062            _labelField = labelField;
063        }
064    
065        /**
066         * Build a bean property selection model.
067         * 
068         * @param c
069         *            Collection
070         * @param labelField
071         *            The label field
072         */
073        public BeanPropertySelectionModel(Collection c, String labelField)
074        {
075            _list = new ArrayList(c);
076            _labelField = labelField;
077        }
078    
079        /**
080         * Get the number of options.
081         * 
082         * @return option count
083         */
084        public int getOptionCount()
085        {
086            return _list.size();
087        }
088    
089        /**
090         * Get the option at index.
091         * 
092         * @param index
093         *            Index
094         * @return object Object at index
095         */
096        public Object getOption(int index)
097        {
098            return _list.get(index);
099        }
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            Object obj = _list.get(index);
111            try {
112    
113                return BeanUtils.getProperty(obj, _labelField);
114            } catch (Exception e) {
115                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            return String.valueOf(index);
129        }
130    
131        public boolean isDisabled(int index)
132        {
133            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            return getOption(Integer.parseInt(value));
146        }
147    }