Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BeanPropertySelectionModel |
|
| 2.5833333333333335;2.583 |
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 org.apache.commons.beanutils.BeanUtils; | |
17 | ||
18 | import java.io.Serializable; | |
19 | import java.util.ArrayList; | |
20 | import java.util.Arrays; | |
21 | import java.util.Collection; | |
22 | import java.util.List; | |
23 | ||
24 | /** | |
25 | * This class is a property selection model for an object list. This is used in {@link PropertySelection}, | |
26 | * MultiplePropertySelection or Palette tapestry components. For example, to use for a Hospital | |
27 | * class, and have the labels be the hospital names. | |
28 | * | |
29 | * <p> | |
30 | * <code> | |
31 | * List<Hospital> list = ...; | |
32 | * return new BeanPropertySelectionModel(hospitals, "name"); | |
33 | * </code> | |
34 | * </p> | |
35 | * <p>This will use getName() on the Hospital object, as its display.</p> | |
36 | * | |
37 | * @author Gabriel Handford | |
38 | */ | |
39 | public class BeanPropertySelectionModel implements IPropertySelectionModel, Serializable | |
40 | { | |
41 | ||
42 | /** Comment for <code>serialVersionUID</code>. */ | |
43 | private static final long serialVersionUID = 3763091973006766644L; | |
44 | protected List _list; | |
45 | protected String _labelField; | |
46 | ||
47 | /** | |
48 | * Build an empty property selection model. | |
49 | */ | |
50 | public BeanPropertySelectionModel() | |
51 | { | |
52 | 0 | this(Arrays.asList(new Object[0]), null); |
53 | 0 | } |
54 | ||
55 | /** | |
56 | * Build a bean property selection model. | |
57 | * | |
58 | * @param list | |
59 | * The list | |
60 | * @param labelField | |
61 | * The label field | |
62 | */ | |
63 | public BeanPropertySelectionModel(List list, String labelField) | |
64 | 0 | { |
65 | 0 | _list = list; |
66 | 0 | _labelField = labelField; |
67 | 0 | } |
68 | ||
69 | /** | |
70 | * Build a bean property selection model. | |
71 | * | |
72 | * @param c | |
73 | * Collection | |
74 | * @param labelField | |
75 | * The label field | |
76 | */ | |
77 | public BeanPropertySelectionModel(Collection c, String labelField) | |
78 | 0 | { |
79 | 0 | _list = new ArrayList(c); |
80 | 0 | _labelField = labelField; |
81 | 0 | } |
82 | ||
83 | /** | |
84 | * Get the number of options. | |
85 | * | |
86 | * @return option count | |
87 | */ | |
88 | public int getOptionCount() | |
89 | { | |
90 | 0 | return _list.size(); |
91 | } | |
92 | ||
93 | /** | |
94 | * Get the option at index. | |
95 | * | |
96 | * @param index | |
97 | * Index | |
98 | * @return object Object at index | |
99 | */ | |
100 | public Object getOption(int index) | |
101 | { | |
102 | 0 | if (index > (_list.size() - 1)) |
103 | { | |
104 | 0 | return null; |
105 | } | |
106 | ||
107 | 0 | 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 | 0 | Object obj = _list.get(index); |
120 | ||
121 | try | |
122 | { | |
123 | 0 | return BeanUtils.getProperty(obj, _labelField); |
124 | 0 | } catch (Exception e) |
125 | { | |
126 | 0 | 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 | 0 | return String.valueOf(index); |
140 | } | |
141 | ||
142 | public boolean isDisabled(int index) | |
143 | { | |
144 | 0 | 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 | 0 | if (value == null) |
157 | { | |
158 | 0 | return null; |
159 | } | |
160 | ||
161 | 0 | return getOption( Integer.parseInt(value)); |
162 | } | |
163 | ||
164 | public String toString() | |
165 | { | |
166 | 0 | return "BeanPropertySelectionModel[" + |
167 | "_list=" + _list + | |
168 | '\n' + | |
169 | ", _labelField='" + _labelField + '\'' + | |
170 | '\n' + | |
171 | ']'; | |
172 | } | |
173 | ||
174 | public boolean equals(Object o) | |
175 | { | |
176 | 0 | if (this == o) return true; |
177 | 0 | if (!(o instanceof BeanPropertySelectionModel)) return false; |
178 | ||
179 | 0 | BeanPropertySelectionModel that = (BeanPropertySelectionModel) o; |
180 | ||
181 | 0 | if (_labelField != null ? !_labelField.equals(that._labelField) : that._labelField != null) return false; |
182 | 0 | if (_list != null ? !_list.equals(that._list) : that._list != null) return false; |
183 | ||
184 | 0 | return true; |
185 | } | |
186 | ||
187 | public int hashCode() | |
188 | { | |
189 | int result; | |
190 | 0 | result = (_list != null ? _list.hashCode() : 0); |
191 | 0 | result = 31 * result + (_labelField != null ? _labelField.hashCode() : 0); |
192 | 0 | return result; |
193 | } | |
194 | } |