1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.contrib.palette; |
16 |
| |
17 |
| import java.util.ArrayList; |
18 |
| import java.util.Collections; |
19 |
| import java.util.HashMap; |
20 |
| import java.util.Iterator; |
21 |
| import java.util.List; |
22 |
| import java.util.Map; |
23 |
| |
24 |
| import org.apache.tapestry.BaseComponent; |
25 |
| import org.apache.tapestry.IAsset; |
26 |
| import org.apache.tapestry.IForm; |
27 |
| import org.apache.tapestry.IMarkupWriter; |
28 |
| import org.apache.tapestry.IRequestCycle; |
29 |
| import org.apache.tapestry.IScript; |
30 |
| import org.apache.tapestry.PageRenderSupport; |
31 |
| import org.apache.tapestry.Tapestry; |
32 |
| import org.apache.tapestry.TapestryUtils; |
33 |
| import org.apache.tapestry.components.Block; |
34 |
| import org.apache.tapestry.form.IPropertySelectionModel; |
35 |
| import org.apache.tapestry.form.ValidatableField; |
36 |
| import org.apache.tapestry.form.ValidatableFieldSupport; |
37 |
| import org.apache.tapestry.valid.IValidationDelegate; |
38 |
| import org.apache.tapestry.valid.ValidationConstants; |
39 |
| import org.apache.tapestry.valid.ValidatorException; |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
| |
84 |
| |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
| |
149 |
| |
150 |
| |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
| |
169 |
| |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
| |
194 |
| |
195 |
| |
196 |
| |
197 |
| |
198 |
| |
199 |
| |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
| |
207 |
| |
208 |
| |
209 |
| |
210 |
| public abstract class Palette extends BaseComponent implements ValidatableField |
211 |
| { |
212 |
| private static final int MAP_SIZE = 7; |
213 |
| |
214 |
| |
215 |
| |
216 |
| |
217 |
| |
218 |
| private Map _symbols; |
219 |
| |
220 |
| |
221 |
| public abstract void setAvailableColumn(PaletteColumn column); |
222 |
| |
223 |
| |
224 |
| public abstract void setSelectedColumn(PaletteColumn column); |
225 |
| |
226 |
| public abstract void setName(String name); |
227 |
| |
228 |
| public abstract void setForm(IForm form); |
229 |
| |
230 |
| |
231 |
| public abstract void setRequiredMessage(String message); |
232 |
| |
233 |
| |
234 |
| |
235 |
| public abstract String getIdParameter(); |
236 |
| |
237 |
| |
238 |
| |
239 |
| public abstract void setClientId(String clientId); |
240 |
| |
241 |
0
| protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
|
242 |
| { |
243 |
| |
244 |
| |
245 |
0
| IForm form = TapestryUtils.getForm(cycle, this);
|
246 |
| |
247 |
0
| setForm(form);
|
248 |
| |
249 |
0
| if (form.wasPrerendered(writer, this))
|
250 |
0
| return;
|
251 |
| |
252 |
0
| IValidationDelegate delegate = form.getDelegate();
|
253 |
| |
254 |
0
| delegate.setFormComponent(this);
|
255 |
| |
256 |
0
| form.getElementId(this);
|
257 |
| |
258 |
0
| if (form.isRewinding())
|
259 |
| { |
260 |
0
| if (!isDisabled())
|
261 |
| { |
262 |
0
| rewindFormComponent(writer, cycle);
|
263 |
| } |
264 |
| } |
265 |
0
| else if (!cycle.isRewinding())
|
266 |
| { |
267 |
0
| if (!isDisabled())
|
268 |
0
| delegate.registerForFocus(this, ValidationConstants.NORMAL_FIELD);
|
269 |
| |
270 |
0
| renderFormComponent(writer, cycle);
|
271 |
| |
272 |
0
| if (delegate.isInError())
|
273 |
0
| delegate.registerForFocus(this, ValidationConstants.ERROR_FIELD);
|
274 |
| } |
275 |
| |
276 |
0
| super.renderComponent(writer, cycle);
|
277 |
| } |
278 |
| |
279 |
0
| protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
280 |
| { |
281 |
0
| String clientId = cycle.getUniqueId(TapestryUtils
|
282 |
| .convertTapestryIdToNMToken(getIdParameter())); |
283 |
| |
284 |
0
| setClientId(clientId);
|
285 |
| |
286 |
0
| _symbols = new HashMap(MAP_SIZE);
|
287 |
| |
288 |
0
| runScript(cycle);
|
289 |
| |
290 |
0
| constructColumns();
|
291 |
| |
292 |
0
| getValidatableFieldSupport().renderContributions(this, writer, cycle);
|
293 |
| } |
294 |
| |
295 |
0
| protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle)
|
296 |
| { |
297 |
0
| String[] values = cycle.getParameters(getName());
|
298 |
| |
299 |
0
| int count = Tapestry.size(values);
|
300 |
| |
301 |
0
| List selected = new ArrayList(count);
|
302 |
0
| IPropertySelectionModel model = getModel();
|
303 |
| |
304 |
0
| for (int i = 0; i < count; i++)
|
305 |
| { |
306 |
0
| String value = values[i];
|
307 |
0
| Object option = model.translateValue(value);
|
308 |
| |
309 |
0
| selected.add(option);
|
310 |
| } |
311 |
| |
312 |
0
| setSelected(selected);
|
313 |
| |
314 |
0
| try
|
315 |
| { |
316 |
0
| getValidatableFieldSupport().validate(this, writer, cycle, selected);
|
317 |
| } |
318 |
| catch (ValidatorException e) |
319 |
| { |
320 |
0
| getForm().getDelegate().record(e);
|
321 |
| } |
322 |
| } |
323 |
| |
324 |
0
| protected void cleanupAfterRender(IRequestCycle cycle)
|
325 |
| { |
326 |
0
| _symbols = null;
|
327 |
| |
328 |
0
| setAvailableColumn(null);
|
329 |
0
| setSelectedColumn(null);
|
330 |
| |
331 |
0
| super.cleanupAfterRender(cycle);
|
332 |
| } |
333 |
| |
334 |
| |
335 |
| |
336 |
| |
337 |
0
| private void runScript(IRequestCycle cycle)
|
338 |
| { |
339 |
0
| PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
|
340 |
| |
341 |
0
| setImage(pageRenderSupport, cycle, "selectImage", getSelectImage());
|
342 |
0
| setImage(pageRenderSupport, cycle, "selectDisabledImage", getSelectDisabledImage());
|
343 |
0
| setImage(pageRenderSupport, cycle, "deselectImage", getDeselectImage());
|
344 |
0
| setImage(pageRenderSupport, cycle, "deselectDisabledImage", getDeselectDisabledImage());
|
345 |
| |
346 |
0
| if (isSortUser())
|
347 |
| { |
348 |
0
| setImage(pageRenderSupport, cycle, "upImage", getUpImage());
|
349 |
0
| setImage(pageRenderSupport, cycle, "upDisabledImage", getUpDisabledImage());
|
350 |
0
| setImage(pageRenderSupport, cycle, "downImage", getDownImage());
|
351 |
0
| setImage(pageRenderSupport, cycle, "downDisabledImage", getDownDisabledImage());
|
352 |
| } |
353 |
| |
354 |
0
| _symbols.put("palette", this);
|
355 |
| |
356 |
0
| getScript().execute(cycle, pageRenderSupport, _symbols);
|
357 |
| } |
358 |
| |
359 |
| |
360 |
| |
361 |
| |
362 |
| |
363 |
0
| private void setImage(PageRenderSupport pageRenderSupport, IRequestCycle cycle,
|
364 |
| String symbolName, IAsset asset) |
365 |
| { |
366 |
0
| String URL = asset.buildURL(cycle);
|
367 |
0
| String reference = pageRenderSupport.getPreloadedImageReference(URL);
|
368 |
| |
369 |
0
| _symbols.put(symbolName, reference);
|
370 |
| } |
371 |
| |
372 |
0
| public Map getSymbols()
|
373 |
| { |
374 |
0
| return _symbols;
|
375 |
| } |
376 |
| |
377 |
| |
378 |
| |
379 |
| |
380 |
0
| private void constructColumns()
|
381 |
| { |
382 |
| |
383 |
| |
384 |
0
| List selected = getSelected();
|
385 |
| |
386 |
0
| if (selected == null)
|
387 |
0
| selected = Collections.EMPTY_LIST;
|
388 |
| |
389 |
0
| String sortMode = getSort();
|
390 |
| |
391 |
0
| boolean sortUser = sortMode.equals(SortMode.USER);
|
392 |
| |
393 |
0
| List selectedOptions = null;
|
394 |
| |
395 |
0
| if (sortUser)
|
396 |
| { |
397 |
0
| int count = selected.size();
|
398 |
0
| selectedOptions = new ArrayList(count);
|
399 |
| |
400 |
0
| for (int i = 0; i < count; i++)
|
401 |
0
| selectedOptions.add(null);
|
402 |
| } |
403 |
| |
404 |
0
| PaletteColumn availableColumn = new PaletteColumn((String) _symbols.get("availableName"),
|
405 |
| null, getRows()); |
406 |
0
| PaletteColumn selectedColumn = new PaletteColumn(getName(), getClientId(), getRows());
|
407 |
| |
408 |
| |
409 |
| |
410 |
| |
411 |
0
| IPropertySelectionModel model = getModel();
|
412 |
| |
413 |
0
| int count = model.getOptionCount();
|
414 |
| |
415 |
0
| for (int i = 0; i < count; i++)
|
416 |
| { |
417 |
0
| Object optionValue = model.getOption(i);
|
418 |
| |
419 |
0
| PaletteOption o = new PaletteOption(model.getValue(i), model.getLabel(i));
|
420 |
| |
421 |
0
| int index = selected.indexOf(optionValue);
|
422 |
0
| boolean isSelected = index >= 0;
|
423 |
| |
424 |
0
| if (sortUser && isSelected)
|
425 |
| { |
426 |
0
| selectedOptions.set(index, o);
|
427 |
0
| continue;
|
428 |
| } |
429 |
| |
430 |
0
| PaletteColumn c = isSelected ? selectedColumn : availableColumn;
|
431 |
| |
432 |
0
| c.addOption(o);
|
433 |
| } |
434 |
| |
435 |
0
| if (sortUser)
|
436 |
| { |
437 |
0
| Iterator i = selectedOptions.iterator();
|
438 |
0
| while (i.hasNext())
|
439 |
| { |
440 |
0
| PaletteOption o = (PaletteOption) i.next();
|
441 |
0
| selectedColumn.addOption(o);
|
442 |
| } |
443 |
| } |
444 |
| |
445 |
0
| if (sortMode.equals(SortMode.VALUE))
|
446 |
| { |
447 |
0
| availableColumn.sortByValue();
|
448 |
0
| selectedColumn.sortByValue();
|
449 |
| } |
450 |
0
| else if (sortMode.equals(SortMode.LABEL))
|
451 |
| { |
452 |
0
| availableColumn.sortByLabel();
|
453 |
0
| selectedColumn.sortByLabel();
|
454 |
| } |
455 |
| |
456 |
0
| setAvailableColumn(availableColumn);
|
457 |
0
| setSelectedColumn(selectedColumn);
|
458 |
| } |
459 |
| |
460 |
0
| public boolean isSortUser()
|
461 |
| { |
462 |
0
| return getSort().equals(SortMode.USER);
|
463 |
| } |
464 |
| |
465 |
| public abstract Block getAvailableTitleBlock(); |
466 |
| |
467 |
| public abstract IAsset getDeselectDisabledImage(); |
468 |
| |
469 |
| public abstract IAsset getDeselectImage(); |
470 |
| |
471 |
| public abstract IAsset getDownDisabledImage(); |
472 |
| |
473 |
| public abstract IAsset getDownImage(); |
474 |
| |
475 |
| public abstract IAsset getSelectDisabledImage(); |
476 |
| |
477 |
| public abstract IPropertySelectionModel getModel(); |
478 |
| |
479 |
| public abstract int getRows(); |
480 |
| |
481 |
| public abstract Block getSelectedTitleBlock(); |
482 |
| |
483 |
| public abstract IAsset getSelectImage(); |
484 |
| |
485 |
| public abstract String getSort(); |
486 |
| |
487 |
| public abstract IAsset getUpDisabledImage(); |
488 |
| |
489 |
| public abstract IAsset getUpImage(); |
490 |
| |
491 |
| |
492 |
| |
493 |
| |
494 |
| |
495 |
| |
496 |
0
| public boolean isDisabled()
|
497 |
| { |
498 |
0
| return false;
|
499 |
| } |
500 |
| |
501 |
| |
502 |
| |
503 |
| public abstract List getSelected(); |
504 |
| |
505 |
| |
506 |
| |
507 |
| public abstract void setSelected(List selected); |
508 |
| |
509 |
| |
510 |
| |
511 |
| |
512 |
| |
513 |
| |
514 |
| public abstract IScript getScript(); |
515 |
| |
516 |
| |
517 |
| |
518 |
| |
519 |
| |
520 |
| |
521 |
| public abstract ValidatableFieldSupport getValidatableFieldSupport(); |
522 |
| |
523 |
| |
524 |
| |
525 |
| |
526 |
0
| public boolean isRequired()
|
527 |
| { |
528 |
0
| return getValidatableFieldSupport().isRequired(this);
|
529 |
| } |
530 |
| } |