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 org.apache.commons.beanutils.BeanUtils;
017    
018    import java.io.Serializable;
019    import java.util.ArrayList;
020    import java.util.Arrays;
021    import java.util.Collection;
022    import java.util.List;
023    
024    /**
025     * This class is a property selection model for an object list. This is used in {@link PropertySelection},
026     * MultiplePropertySelection or Palette tapestry components. For example, to use for a Hospital
027     * class, and have the labels be the hospital names.
028     *
029     * <p>
030     * <code>
031     * List&lt;Hospital&gt; list = ...;
032     * return new BeanPropertySelectionModel(hospitals, "name");
033     * </code>
034     * </p>
035     * <p>This will use getName() on the Hospital object, as its display.</p>
036     *
037     * @author Gabriel Handford
038     */
039    public class BeanPropertySelectionModel implements IPropertySelectionModel, Serializable
040    {
041    
042        /** Comment for <code>serialVersionUID</code>. */
043        private static final long serialVersionUID = 3763091973006766644L;
044        protected List _list;
045        protected String _labelField;
046    
047        /**
048         * Build an empty property selection model.
049         */
050        public BeanPropertySelectionModel()
051        {
052            this(Arrays.asList(new Object[0]), null);
053        }
054    
055        /**
056         * Build a bean property selection model.
057         *
058         * @param list
059         *            The list
060         * @param labelField
061         *            The label field
062         */
063        public BeanPropertySelectionModel(List list, String labelField)
064        {
065            _list = list;
066            _labelField = labelField;
067        }
068    
069        /**
070         * Build a bean property selection model.
071         *
072         * @param c
073         *          Collection
074         * @param labelField
075         *          The label field
076         */
077        public BeanPropertySelectionModel(Collection c, String labelField)
078        {
079            _list = new ArrayList(c);
080            _labelField = labelField;
081        }
082    
083        /**
084         * Get the number of options.
085         *
086         * @return option count
087         */
088        public int getOptionCount()
089        {
090            return _list.size();
091        }
092    
093        /**
094         * Get the option at index.
095         *
096         * @param index
097         *            Index
098         * @return object Object at index
099         */
100        public Object getOption(int index)
101        {
102            if (index > (_list.size() - 1))
103            {
104                return null;
105            }
106    
107            return _list.get(index);
108        }
109    
110        /**
111         * Get the label at index.
112         *
113         * @param index
114         *            Index
115         * @return label Label at index
116         */
117        public String getLabel(int index)
118        {
119            Object obj = _list.get(index);
120            
121            try
122            {
123                return BeanUtils.getProperty(obj, _labelField);
124            } catch (Exception e)
125            {
126                throw new RuntimeException("Error getting property", e);
127            }
128        }
129    
130        /**
131         * Get the value at index.
132         *
133         * @param index
134         *            Index
135         * @return value Value at index
136         */
137        public String getValue(int index)
138        {
139            return String.valueOf(index);
140        }
141    
142        public boolean isDisabled(int index)
143        {
144            return false;
145        }
146    
147        /**
148         * Translate value to object.
149         *
150         * @param value
151         *            Value
152         * @return object Object from value
153         */
154        public Object translateValue(String value)
155        {
156            if (value == null)
157            {
158                return null;
159            }
160    
161            return getOption( Integer.parseInt(value));
162        }
163    
164        public String toString()
165        {
166            return "BeanPropertySelectionModel[" +
167                   "_list=" + _list +
168                   '\n' +
169                   ", _labelField='" + _labelField + '\'' +
170                   '\n' +
171                   ']';
172        }
173    
174        public boolean equals(Object o)
175        {
176            if (this == o) return true;
177            if (!(o instanceof BeanPropertySelectionModel)) return false;
178    
179            BeanPropertySelectionModel that = (BeanPropertySelectionModel) o;
180    
181            if (_labelField != null ? !_labelField.equals(that._labelField) : that._labelField != null) return false;
182            if (_list != null ? !_list.equals(that._list) : that._list != null) return false;
183    
184            return true;
185        }
186    
187        public int hashCode()
188        {
189            int result;
190            result = (_list != null ? _list.hashCode() : 0);
191            result = 31 * result + (_labelField != null ? _labelField.hashCode() : 0);
192            return result;
193        }
194    }