1 | |
|
2 | |
|
3 | |
|
4 | |
package net.sf.jolene.dom; |
5 | |
|
6 | |
import net.sf.jolene.constants.Prefs; |
7 | |
import net.sf.jolene.constants.Tags; |
8 | |
|
9 | |
import java.util.*; |
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | 3 | public final class Select extends HTMLElement { |
18 | |
|
19 | |
private boolean multiple; |
20 | |
|
21 | |
|
22 | |
|
23 | 36 | List<Option> options = new ArrayList<Option>(3); |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 36 | public Select() { |
29 | 36 | tag = Tags.select; |
30 | 36 | multiple = false; |
31 | 36 | } |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public Select(String name) { |
40 | 2 | this(); |
41 | 2 | setName(name); |
42 | 2 | } |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
public int addOption(Option option) { |
51 | 1277 | int index = -1; |
52 | |
|
53 | |
|
54 | 1277 | StringTokenizer st = new StringTokenizer(getValue(), Prefs.SelectValueSeperator.getValue()); |
55 | |
String itemValue; |
56 | 1303 | while (st.hasMoreTokens()) { |
57 | 26 | itemValue = st.nextToken(); |
58 | 26 | if (itemValue.equals(option.getValue())) { |
59 | 4 | option.setSelected(true); |
60 | |
} |
61 | |
} |
62 | |
|
63 | 1277 | options.add(option); |
64 | 1277 | index = options.size(); |
65 | 1277 | return index; |
66 | |
} |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public void clearOptions() { |
72 | 2 | options.clear(); |
73 | 2 | } |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
@Override |
82 | |
public Select clone() { |
83 | 3 | Select select = (Select) super.clone(); |
84 | |
|
85 | 10 | for (int j = 0; j < options.size(); j++) { |
86 | 7 | select.addOption(new Option(options.get(j).getValue(), options.get(j).getText())); |
87 | |
} |
88 | 3 | return select; |
89 | |
} |
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public Option options(int index) { |
98 | 7 | return options.get(index); |
99 | |
} |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
@Override |
107 | |
public String getValue() { |
108 | 1329 | String selectValue = super.getValue(); |
109 | |
|
110 | |
|
111 | 1329 | setValue(selectValue); |
112 | |
|
113 | 1329 | String value = ""; |
114 | |
|
115 | |
|
116 | |
|
117 | 1329 | String sep = Prefs.SelectValueSeperator.getValue(); |
118 | 127521 | for (int j = 0; j < options.size(); j++) { |
119 | 126192 | Option option = options.get(j); |
120 | |
|
121 | 126192 | if (option.isSelected()) { |
122 | 27 | value += option.getValue() + sep; |
123 | |
} |
124 | |
} |
125 | |
|
126 | |
|
127 | 1329 | int n = value.lastIndexOf(","); |
128 | 1329 | if (n > -1) { |
129 | 16 | value = value.substring(0, n); |
130 | |
} |
131 | |
|
132 | 1329 | if (value.trim().length() == 0 && selectValue.trim().length() > 0) { |
133 | |
|
134 | |
|
135 | |
|
136 | 13 | value = selectValue; |
137 | 13 | log.warn("Returning Select object " + getName() + " assigned value: " + value + " which does not exist in the options list."); |
138 | |
} |
139 | 1329 | return value; |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public boolean isMultiple() { |
148 | 0 | return multiple; |
149 | |
} |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
@Override |
157 | |
public void setName(String name) { |
158 | 33 | super.setName(name); |
159 | 33 | setAttribute("name", name); |
160 | 33 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
public void setMultiple(boolean multiple) { |
168 | |
|
169 | 5 | this.multiple = multiple; |
170 | 5 | if (multiple) { |
171 | 5 | setAttribute("multiple", "multiple"); |
172 | |
} else { |
173 | 0 | removeAttribute("multiple"); |
174 | |
} |
175 | 5 | } |
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
@Override |
184 | |
public void setValue(String value) { |
185 | 1341 | super.setValue(value); |
186 | |
|
187 | |
|
188 | |
|
189 | 127546 | for (int j = 0; j < options.size(); j++) { |
190 | |
|
191 | 126205 | Option option = options.get(j); |
192 | |
|
193 | 126205 | option.setSelected(false); |
194 | |
|
195 | 126205 | StringTokenizer st = new StringTokenizer(value, Prefs.SelectValueSeperator.getValue()); |
196 | 126258 | while (st.hasMoreTokens()) { |
197 | 82 | String itemValue = st.nextToken(); |
198 | 82 | String optionValue = option.getValue(); |
199 | |
|
200 | 82 | if (optionValue.equals(itemValue)) { |
201 | 29 | option.setSelected(true); |
202 | 29 | break; |
203 | |
} |
204 | 53 | } |
205 | |
} |
206 | |
|
207 | 1341 | } |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
public void sortOptions() { |
213 | 0 | Collections.sort(options, new Comparator<Option>() { |
214 | |
|
215 | 0 | public int compare(Option o1, Option o2) { |
216 | 0 | String s1 = o1.getText(); |
217 | 0 | String s2 = o2.getText(); |
218 | 0 | return s1.compareTo(s2); |
219 | |
} |
220 | |
}); |
221 | 0 | } |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
@Override |
230 | |
public String toString() { |
231 | |
|
232 | 49 | if (swapWith != null) { |
233 | 0 | return swapWith.toString(); |
234 | |
} |
235 | |
|
236 | 49 | if (!isRenderable() && !log.isDebugEnabled()) { |
237 | 0 | return ""; |
238 | |
} |
239 | |
|
240 | |
|
241 | 49 | getValue(); |
242 | |
|
243 | 49 | StringBuilder sb = new StringBuilder(super.toString()); |
244 | 1373 | for (int j = 0; j < options.size(); j++) { |
245 | 1324 | sb.append(options.get(j).toString()); |
246 | |
} |
247 | 49 | String ls = System.getProperty("line.separator"); |
248 | 49 | sb.append(ls); |
249 | 49 | sb.append("</").append(tag).append(">"); |
250 | 49 | sb.append(ls); |
251 | 49 | return sb.toString(); |
252 | |
} |
253 | |
} |