Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DefaultOptionRenderer |
|
| 5.5;5.5 |
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.tapestry.IMarkupWriter; | |
17 | import org.apache.tapestry.IRequestCycle; | |
18 | ||
19 | ||
20 | /** | |
21 | * The default implementation of {@link IOptionRenderer} which is used by | |
22 | * the {@link PropertySelection} component if no other renderer is specified | |
23 | * in the component parameters. | |
24 | */ | |
25 | 0 | public class DefaultOptionRenderer implements IOptionRenderer |
26 | { | |
27 | ||
28 | /** | |
29 | * Default instance used by {@link PropertySelection} if no custom renderer is | |
30 | * specified. | |
31 | */ | |
32 | ||
33 | 0 | public static final IOptionRenderer DEFAULT_INSTANCE = new DefaultOptionRenderer(); |
34 | ||
35 | /** | |
36 | * {@inheritDoc} | |
37 | */ | |
38 | public void renderOptions(IMarkupWriter writer, IRequestCycle cycle, IPropertySelectionModel model, Object selected) | |
39 | { | |
40 | 0 | int count = model.getOptionCount(); |
41 | 0 | boolean foundSelected = false; |
42 | ||
43 | 0 | for (int i = 0; i < count; i++) |
44 | { | |
45 | 0 | Object option = model.getOption(i); |
46 | ||
47 | 0 | writer.begin("option"); |
48 | 0 | writer.attribute("value", model.getValue(i)); |
49 | ||
50 | 0 | if (!foundSelected && isEqual(option, selected)) |
51 | { | |
52 | 0 | writer.attribute("selected", "selected"); |
53 | ||
54 | 0 | foundSelected = true; |
55 | } | |
56 | ||
57 | 0 | if (model.isDisabled(i)) |
58 | 0 | writer.attribute("disabled", "disabled"); |
59 | ||
60 | 0 | writer.print(model.getLabel(i)); |
61 | ||
62 | 0 | writer.end(); |
63 | ||
64 | 0 | writer.println(); |
65 | } | |
66 | 0 | } |
67 | ||
68 | protected boolean isEqual(Object left, Object right) | |
69 | { | |
70 | // Both null, or same object, then are equal | |
71 | ||
72 | 0 | if (left == right) |
73 | 0 | return true; |
74 | ||
75 | // If one is null, the other isn't, then not equal. | |
76 | ||
77 | 0 | if (left == null || right == null) |
78 | 0 | return false; |
79 | ||
80 | // Both non-null; use standard comparison. | |
81 | ||
82 | 0 | return left.equals(right); |
83 | } | |
84 | } |