Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
LabeledPropertySelectionModel |
|
| 1.08;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 | 7 | private String _label = ""; |
80 | ||
81 | 7 | private Object _option = null; |
82 | ||
83 | 7 | 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 | 7 | { |
104 | 7 | _model = model; |
105 | 7 | } |
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 | 4 | this(model); |
119 | ||
120 | 4 | _label = label; |
121 | 4 | } |
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 | 3 | this(model, label); |
137 | ||
138 | 3 | _option = option; |
139 | 3 | } |
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, Object option, String value) |
|
155 | { |
|
156 | 2 | this(model, label, option); |
157 | ||
158 | 2 | _value = value; |
159 | 2 | } |
160 | ||
161 | /** |
|
162 | * Returns the underlying IPropertySelectionModel. |
|
163 | * |
|
164 | * @return the underlying IPropertySelectionModel |
|
165 | */ |
|
166 | public IPropertySelectionModel getModel() |
|
167 | { |
|
168 | 0 | return _model; |
169 | } |
|
170 | ||
171 | /** |
|
172 | * Sets the underlying IPropertySelectionModel. |
|
173 | * |
|
174 | * @param model |
|
175 | * the IPropertySelectionModel to set |
|
176 | */ |
|
177 | public void setModel(IPropertySelectionModel model) |
|
178 | { |
|
179 | 0 | _model = model; |
180 | 0 | } |
181 | ||
182 | /** |
|
183 | * @see org.apache.tapestry.form.IPropertySelectionModel#getOptionCount() |
|
184 | */ |
|
185 | public int getOptionCount() |
|
186 | { |
|
187 | 7 | return _model.getOptionCount() + 1; |
188 | } |
|
189 | ||
190 | /** |
|
191 | * @see org.apache.tapestry.form.IPropertySelectionModel#getOption(int) |
|
192 | */ |
|
193 | public Object getOption(int index) |
|
194 | { |
|
195 | 8 | return (index == 0) ? _option : _model.getOption(index - 1); |
196 | } |
|
197 | ||
198 | /** |
|
199 | * @see org.apache.tapestry.form.IPropertySelectionModel#getLabel(int) |
|
200 | */ |
|
201 | public String getLabel(int index) |
|
202 | { |
|
203 | 11 | return (index == 0) ? _label : _model.getLabel(index - 1); |
204 | } |
|
205 | ||
206 | /** |
|
207 | * @see org.apache.tapestry.form.IPropertySelectionModel#getValue(int) |
|
208 | */ |
|
209 | public String getValue(int index) |
|
210 | { |
|
211 | 8 | return (index == 0) ? _value : _model.getValue(index - 1); |
212 | } |
|
213 | ||
214 | public boolean isDisabled(int index) |
|
215 | { |
|
216 | 6 | return index == 0 && _option == null; |
217 | } |
|
218 | ||
219 | /** |
|
220 | * @see org.apache.tapestry.form.IPropertySelectionModel#translateValue(java.lang.String) |
|
221 | */ |
|
222 | public Object translateValue(String value) |
|
223 | { |
|
224 | 8 | if (value == null) |
225 | 1 | return null; |
226 | ||
227 | 7 | return value.equals(_value) ? _option : _model.translateValue(value); |
228 | } |
|
229 | ||
230 | /** |
|
231 | * Returns the label of the initial IPropertySelectionModel option. |
|
232 | * |
|
233 | * @return a IPropertySelectionModel option label |
|
234 | */ |
|
235 | public String getLabel() |
|
236 | { |
|
237 | 1 | return _label; |
238 | } |
|
239 | ||
240 | /** |
|
241 | * Sets the label of the initial IPropertySelectionModel option. |
|
242 | * |
|
243 | * @param label |
|
244 | * a IPropertySelectionModel option label |
|
245 | */ |
|
246 | public void setLabel(String label) |
|
247 | { |
|
248 | 0 | _label = label; |
249 | 0 | } |
250 | ||
251 | /** |
|
252 | * Returns the value of the initial IPropertySelectionModel option. |
|
253 | * |
|
254 | * @return a IPropertySelectionModel option value |
|
255 | */ |
|
256 | public String getValue() |
|
257 | { |
|
258 | 1 | return _value; |
259 | } |
|
260 | ||
261 | /** |
|
262 | * Sets the value of the initial IPropertySelectionModel option. |
|
263 | * |
|
264 | * @param value |
|
265 | * a IPropertySelectionModel option value |
|
266 | */ |
|
267 | public void setValue(String value) |
|
268 | { |
|
269 | 0 | _value = value; |
270 | 0 | } |
271 | ||
272 | /** |
|
273 | * Returns the initial option. |
|
274 | * |
|
275 | * @return a PropertySelectionModel option |
|
276 | */ |
|
277 | public Object getOption() |
|
278 | { |
|
279 | 1 | return _option; |
280 | } |
|
281 | ||
282 | /** |
|
283 | * Sets the initial IPropertySelectionModel option. |
|
284 | * |
|
285 | * @param option |
|
286 | * a IPropertySelectionModel option |
|
287 | */ |
|
288 | public void setOption(Object option) |
|
289 | { |
|
290 | 0 | _option = option; |
291 | 0 | } |
292 | } |