Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
LabeledPropertySelectionModel |
|
| 1.2857142857142858;1.286 | ||||
LabeledPropertySelectionModel$1 |
|
| 1.2857142857142858;1.286 |
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.<p/> | |
20 | * | |
21 | * By default, the label will be rendered as disabled if its option is null. This behavior can be | |
22 | * changed by {@link #setLabelAlwaysEnabled(boolean)}. | |
23 | * | |
24 | * @author Paul Ferraro | |
25 | * @since 4.0 | |
26 | */ | |
27 | public class LabeledPropertySelectionModel implements IPropertySelectionModel | |
28 | { | |
29 | /** | |
30 | * Empty model implementation. Avoids NullPointerExceptions when default constructor is used. | |
31 | */ | |
32 | 0 | private static final IPropertySelectionModel EMPTY_MODEL = new IPropertySelectionModel() |
33 | 0 | { |
34 | /** | |
35 | * @see org.apache.tapestry.form.IPropertySelectionModel#getOptionCount() | |
36 | */ | |
37 | public int getOptionCount() | |
38 | { | |
39 | 0 | return 0; |
40 | } | |
41 | ||
42 | /** | |
43 | * @see org.apache.tapestry.form.IPropertySelectionModel#getOption(int) | |
44 | */ | |
45 | public Object getOption(int index) | |
46 | { | |
47 | 0 | return null; |
48 | } | |
49 | ||
50 | /** | |
51 | * @see org.apache.tapestry.form.IPropertySelectionModel#getLabel(int) | |
52 | */ | |
53 | public String getLabel(int index) | |
54 | { | |
55 | 0 | return null; |
56 | } | |
57 | ||
58 | /** | |
59 | * @see org.apache.tapestry.form.IPropertySelectionModel#getValue(int) | |
60 | */ | |
61 | public String getValue(int index) | |
62 | { | |
63 | 0 | return null; |
64 | } | |
65 | ||
66 | public boolean isDisabled(int index) | |
67 | { | |
68 | 0 | return false; |
69 | } | |
70 | ||
71 | /** | |
72 | * @see org.apache.tapestry.form.IPropertySelectionModel#translateValue(java.lang.String) | |
73 | */ | |
74 | public Object translateValue(String value) | |
75 | { | |
76 | 0 | return null; |
77 | } | |
78 | }; | |
79 | ||
80 | private IPropertySelectionModel _model; | |
81 | ||
82 | 0 | private String _label = ""; |
83 | ||
84 | 0 | private Object _option = null; |
85 | ||
86 | 0 | private String _value = ""; |
87 | ||
88 | private boolean _labelAlwaysEnabled; | |
89 | ||
90 | /** | |
91 | * Constructs a new LabeledPropertySelectionModel using an empty model and default label, | |
92 | * option, and value. Default constructor is made available so that this model may be specified | |
93 | * as a component helper bean. | |
94 | */ | |
95 | public LabeledPropertySelectionModel() | |
96 | { | |
97 | 0 | this(EMPTY_MODEL); |
98 | 0 | } |
99 | ||
100 | /** | |
101 | * Constructs a new LabeledPropertySelectionModel using the specified model and default label, | |
102 | * option, and value. | |
103 | * | |
104 | * @param model | |
105 | * the underlying model to decorate | |
106 | */ | |
107 | public LabeledPropertySelectionModel(IPropertySelectionModel model) | |
108 | 0 | { |
109 | 0 | _model = model; |
110 | 0 | } |
111 | ||
112 | /** | |
113 | * Constructs a new LabeledPropertySelectionModel using the specified model and label, and | |
114 | * default option and value. | |
115 | * | |
116 | * @param model | |
117 | * the underlying model to decorate | |
118 | * @param label | |
119 | * the label of the initial property | |
120 | */ | |
121 | public LabeledPropertySelectionModel(IPropertySelectionModel model, String label) | |
122 | { | |
123 | 0 | this(model); |
124 | ||
125 | 0 | _label = label; |
126 | 0 | } |
127 | ||
128 | /** | |
129 | * Constructs a new LabeledPropertySelectionModel using the specified model, label, and option; | |
130 | * and default value. | |
131 | * | |
132 | * @param model | |
133 | * the underlying model to decorate | |
134 | * @param label | |
135 | * the label of the initial property | |
136 | * @param option | |
137 | * the option value of the initial property | |
138 | */ | |
139 | public LabeledPropertySelectionModel(IPropertySelectionModel model, String label, Object option) | |
140 | { | |
141 | 0 | this(model, label); |
142 | ||
143 | 0 | _option = option; |
144 | 0 | } |
145 | ||
146 | /** | |
147 | * Constructs a new LabeledPropertySelectionModel using the specified model, label, option, and | |
148 | * value. | |
149 | * | |
150 | * @param model | |
151 | * the underlying model to decorate | |
152 | * @param label | |
153 | * the label of the initial property | |
154 | * @param option | |
155 | * the option value of the initial property | |
156 | * @param value | |
157 | * the value of the initial property | |
158 | */ | |
159 | public LabeledPropertySelectionModel(IPropertySelectionModel model, String label, Object option, String value) | |
160 | { | |
161 | 0 | this(model, label, option); |
162 | ||
163 | 0 | _value = value; |
164 | 0 | } |
165 | ||
166 | /** | |
167 | * Constructs a new LabeledPropertySelectionModel using the specified model, label, option, and | |
168 | * value. | |
169 | * | |
170 | * @param model | |
171 | * the underlying model to decorate | |
172 | * @param label | |
173 | * the label of the initial property | |
174 | * @param option | |
175 | * the option value of the initial property | |
176 | * @param value | |
177 | * the value of the initial property | |
178 | * @param labelAlwaysEnabled | |
179 | * if the label should always be enabled | |
180 | */ | |
181 | public LabeledPropertySelectionModel(IPropertySelectionModel model, String label, Object option, String value, | |
182 | boolean labelAlwaysEnabled) | |
183 | { | |
184 | 0 | this(model, label, option, value); |
185 | ||
186 | 0 | _labelAlwaysEnabled = labelAlwaysEnabled; |
187 | 0 | } |
188 | ||
189 | /** | |
190 | * Returns the underlying IPropertySelectionModel. | |
191 | * | |
192 | * @return the underlying IPropertySelectionModel | |
193 | */ | |
194 | public IPropertySelectionModel getModel() | |
195 | { | |
196 | 0 | return _model; |
197 | } | |
198 | ||
199 | /** | |
200 | * Sets the underlying IPropertySelectionModel. | |
201 | * | |
202 | * @param model | |
203 | * the IPropertySelectionModel to set | |
204 | */ | |
205 | public void setModel(IPropertySelectionModel model) | |
206 | { | |
207 | 0 | _model = model; |
208 | 0 | } |
209 | ||
210 | /** | |
211 | * @see org.apache.tapestry.form.IPropertySelectionModel#getOptionCount() | |
212 | */ | |
213 | public int getOptionCount() | |
214 | { | |
215 | 0 | return _model.getOptionCount() + 1; |
216 | } | |
217 | ||
218 | /** | |
219 | * @see org.apache.tapestry.form.IPropertySelectionModel#getOption(int) | |
220 | */ | |
221 | public Object getOption(int index) | |
222 | { | |
223 | 0 | return (index == 0) ? _option : _model.getOption(index - 1); |
224 | } | |
225 | ||
226 | /** | |
227 | * @see org.apache.tapestry.form.IPropertySelectionModel#getLabel(int) | |
228 | */ | |
229 | public String getLabel(int index) | |
230 | { | |
231 | 0 | return (index == 0) ? _label : _model.getLabel(index - 1); |
232 | } | |
233 | ||
234 | /** | |
235 | * @see org.apache.tapestry.form.IPropertySelectionModel#getValue(int) | |
236 | */ | |
237 | public String getValue(int index) | |
238 | { | |
239 | 0 | return (index == 0) ? _value : _model.getValue(index - 1); |
240 | } | |
241 | ||
242 | public boolean isDisabled(int index) | |
243 | { | |
244 | 0 | return index == 0 ? (!_labelAlwaysEnabled && _option == null) : _model.isDisabled(index - 1); |
245 | } | |
246 | ||
247 | /** | |
248 | * @see org.apache.tapestry.form.IPropertySelectionModel#translateValue(java.lang.String) | |
249 | */ | |
250 | public Object translateValue(String value) | |
251 | { | |
252 | 0 | if (value == null) |
253 | 0 | return null; |
254 | ||
255 | 0 | return value.equals(_value) ? _option : _model.translateValue(value); |
256 | } | |
257 | ||
258 | /** | |
259 | * Returns the label of the initial IPropertySelectionModel option. | |
260 | * | |
261 | * @return a IPropertySelectionModel option label | |
262 | */ | |
263 | public String getLabel() | |
264 | { | |
265 | 0 | return _label; |
266 | } | |
267 | ||
268 | /** | |
269 | * Sets the label of the initial IPropertySelectionModel option. | |
270 | * | |
271 | * @param label | |
272 | * a IPropertySelectionModel option label | |
273 | */ | |
274 | public void setLabel(String label) | |
275 | { | |
276 | 0 | _label = label; |
277 | 0 | } |
278 | ||
279 | /** | |
280 | * Returns the value of the initial IPropertySelectionModel option. | |
281 | * | |
282 | * @return a IPropertySelectionModel option value | |
283 | */ | |
284 | public String getValue() | |
285 | { | |
286 | 0 | return _value; |
287 | } | |
288 | ||
289 | /** | |
290 | * Sets the value of the initial IPropertySelectionModel option. | |
291 | * | |
292 | * @param value | |
293 | * a IPropertySelectionModel option value | |
294 | */ | |
295 | public void setValue(String value) | |
296 | { | |
297 | 0 | _value = value; |
298 | 0 | } |
299 | ||
300 | /** | |
301 | * Returns the initial option. | |
302 | * | |
303 | * @return a PropertySelectionModel option | |
304 | */ | |
305 | public Object getOption() | |
306 | { | |
307 | 0 | return _option; |
308 | } | |
309 | ||
310 | /** | |
311 | * Sets the initial IPropertySelectionModel option. | |
312 | * | |
313 | * @param option | |
314 | * a IPropertySelectionModel option | |
315 | */ | |
316 | public void setOption(Object option) | |
317 | { | |
318 | 0 | _option = option; |
319 | 0 | } |
320 | ||
321 | /** | |
322 | * Returns if label should always be enabled. | |
323 | */ | |
324 | public boolean isLabelAlwaysEnabled() | |
325 | { | |
326 | 0 | return _labelAlwaysEnabled; |
327 | } | |
328 | ||
329 | /** | |
330 | * Sets the state of the label. | |
331 | */ | |
332 | public void setLabelAlwaysEnabled(boolean labelAlwaysEnabled) | |
333 | { | |
334 | 0 | _labelAlwaysEnabled = labelAlwaysEnabled; |
335 | 0 | } |
336 | } |