Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
DefaultOptionRenderer |
|
| 4.5;4.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 | 1 | 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 | 1 | 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 | 1 | int count = model.getOptionCount(); |
41 | 1 | boolean foundSelected = false; |
42 | ||
43 | 4 | for (int i = 0; i < count; i++) |
44 | { |
|
45 | 3 | Object option = model.getOption(i); |
46 | ||
47 | 3 | writer.begin("option"); |
48 | 3 | writer.attribute("value", model.getValue(i)); |
49 | ||
50 | 3 | if (!foundSelected && isEqual(option, selected)) |
51 | { |
|
52 | 1 | writer.attribute("selected", "selected"); |
53 | ||
54 | 1 | foundSelected = true; |
55 | } |
|
56 | ||
57 | 3 | if (model.isDisabled(i)) |
58 | 1 | writer.attribute("disabled", "true"); |
59 | ||
60 | 3 | writer.print(model.getLabel(i)); |
61 | ||
62 | 3 | writer.end(); |
63 | ||
64 | 3 | writer.println(); |
65 | } |
|
66 | 1 | } |
67 | ||
68 | protected boolean isEqual(Object left, Object right) |
|
69 | { |
|
70 | // Both null, or same object, then are equal |
|
71 | ||
72 | 1 | if (left == right) |
73 | 1 | 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 | } |