Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
PaletteColumn |
|
| 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.contrib.palette; |
|
16 | ||
17 | import java.util.ArrayList; |
|
18 | import java.util.Collections; |
|
19 | import java.util.Comparator; |
|
20 | import java.util.List; |
|
21 | ||
22 | import org.apache.tapestry.IMarkupWriter; |
|
23 | import org.apache.tapestry.IRender; |
|
24 | import org.apache.tapestry.IRequestCycle; |
|
25 | ||
26 | /** |
|
27 | * One of the two columns in a Palette component: the left column lists available options, the right |
|
28 | * column lists the selected columns. |
|
29 | * |
|
30 | * @author Howard Lewis Ship |
|
31 | */ |
|
32 | public class PaletteColumn implements IRender |
|
33 | { |
|
34 | private String _name; |
|
35 | ||
36 | private String _clientId; |
|
37 | ||
38 | private int _rows; |
|
39 | ||
40 | 0 | private List _options = new ArrayList(); |
41 | ||
42 | /** |
|
43 | * |
|
44 | * @author hls |
|
45 | */ |
|
46 | 0 | private static class ValueComparator implements Comparator |
47 | { |
|
48 | public int compare(Object o1, Object o2) |
|
49 | { |
|
50 | 0 | PaletteOption option1 = (PaletteOption) o1; |
51 | 0 | PaletteOption option2 = (PaletteOption) o2; |
52 | ||
53 | 0 | return option1.getValue().compareTo(option2.getValue()); |
54 | } |
|
55 | } |
|
56 | ||
57 | /** |
|
58 | * |
|
59 | * @author hls |
|
60 | */ |
|
61 | 0 | private static class LabelComparator implements Comparator |
62 | { |
|
63 | public int compare(Object o1, Object o2) |
|
64 | { |
|
65 | 0 | PaletteOption option1 = (PaletteOption) o1; |
66 | 0 | PaletteOption option2 = (PaletteOption) o2; |
67 | ||
68 | 0 | return option1.getLabel().compareTo(option2.getLabel()); |
69 | } |
|
70 | } |
|
71 | ||
72 | /** |
|
73 | * @param name |
|
74 | * the name of the column (the name attribute of the <select>) |
|
75 | * @param rows |
|
76 | * the number of visible rows (the size attribute of the <select>) |
|
77 | */ |
|
78 | public PaletteColumn(String name, String clientId, int rows) |
|
79 | 0 | { |
80 | 0 | _name = name; |
81 | 0 | _clientId = clientId; |
82 | 0 | _rows = rows; |
83 | 0 | } |
84 | ||
85 | public void addOption(PaletteOption option) |
|
86 | { |
|
87 | 0 | _options.add(option); |
88 | 0 | } |
89 | ||
90 | /** |
|
91 | * Sorts the options by value (the hidden value for the option that represents the object |
|
92 | * value). This should be invoked before rendering this PaletteColumn. |
|
93 | */ |
|
94 | public void sortByValue() |
|
95 | { |
|
96 | 0 | Collections.sort(_options, new ValueComparator()); |
97 | 0 | } |
98 | ||
99 | /** |
|
100 | * Sorts the options by the label visible to the user. This should be invoked before rendering |
|
101 | * this PaletteColumn. |
|
102 | */ |
|
103 | public void sortByLabel() |
|
104 | { |
|
105 | 0 | Collections.sort(_options, new LabelComparator()); |
106 | 0 | } |
107 | ||
108 | /** |
|
109 | * Renders the <select> and <option> tags for this column. |
|
110 | */ |
|
111 | public void render(IMarkupWriter writer, IRequestCycle cycle) |
|
112 | { |
|
113 | 0 | writer.begin("select"); |
114 | 0 | writer.attribute("multiple", "multiple"); |
115 | 0 | writer.attribute("name", _name); |
116 | ||
117 | 0 | if (_clientId != null) |
118 | 0 | writer.attribute("id", _clientId); |
119 | ||
120 | 0 | writer.attribute("size", _rows); |
121 | 0 | writer.println(); |
122 | ||
123 | 0 | int count = _options.size(); |
124 | 0 | for (int i = 0; i < count; i++) |
125 | { |
|
126 | 0 | PaletteOption o = (PaletteOption) _options.get(i); |
127 | ||
128 | 0 | o.render(writer, cycle); |
129 | } |
|
130 | ||
131 | 0 | writer.end(); |
132 | 0 | } |
133 | ||
134 | } |