1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry; |
16 |
| |
17 |
| import java.util.Collection; |
18 |
| import java.util.Collections; |
19 |
| import java.util.HashMap; |
20 |
| import java.util.HashSet; |
21 |
| import java.util.Iterator; |
22 |
| import java.util.List; |
23 |
| import java.util.Map; |
24 |
| |
25 |
| import org.apache.hivemind.ApplicationRuntimeException; |
26 |
| import org.apache.hivemind.Messages; |
27 |
| import org.apache.hivemind.impl.BaseLocatable; |
28 |
| import org.apache.hivemind.util.Defense; |
29 |
| import org.apache.hivemind.util.PropertyUtils; |
30 |
| import org.apache.tapestry.bean.BeanProvider; |
31 |
| import org.apache.tapestry.engine.IPageLoader; |
32 |
| import org.apache.tapestry.event.PageEvent; |
33 |
| import org.apache.tapestry.listener.ListenerMap; |
34 |
| import org.apache.tapestry.spec.IComponentSpecification; |
35 |
| import org.apache.tapestry.spec.IContainedComponent; |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| public abstract class AbstractComponent extends BaseLocatable implements IComponent |
44 |
| { |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| private IPage _page; |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| private IComponent _container; |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| private String _id; |
63 |
| |
64 |
| |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| private String _idPath; |
70 |
| |
71 |
| private static final int MAP_SIZE = 5; |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| private Map _bindings; |
79 |
| |
80 |
| private Map _components; |
81 |
| |
82 |
| private static final int BODY_INIT_SIZE = 5; |
83 |
| |
84 |
| private INamespace _namespace; |
85 |
| |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| private static final Map EMPTY_MAP = Collections.unmodifiableMap(new HashMap(1)); |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| private int _bodyCount = 0; |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| private IRender[] _body; |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| private Map _assets; |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
| private ListenerMap _listeners; |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| private IBeanProvider _beans; |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| private boolean _rendering; |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| private boolean _active; |
142 |
| |
143 |
| |
144 |
| |
145 |
| private IContainedComponent _containedComponent; |
146 |
| |
147 |
53
| public void addAsset(String name, IAsset asset)
|
148 |
| { |
149 |
53
| Defense.notNull(name, "name");
|
150 |
53
| Defense.notNull(asset, "asset");
|
151 |
| |
152 |
53
| checkActiveLock();
|
153 |
| |
154 |
53
| if (_assets == null)
|
155 |
49
| _assets = new HashMap(MAP_SIZE);
|
156 |
| |
157 |
53
| _assets.put(name, asset);
|
158 |
| } |
159 |
| |
160 |
886
| public void addComponent(IComponent component)
|
161 |
| { |
162 |
886
| Defense.notNull(component, "component");
|
163 |
| |
164 |
886
| checkActiveLock();
|
165 |
| |
166 |
886
| if (_components == null)
|
167 |
188
| _components = new HashMap(MAP_SIZE);
|
168 |
| |
169 |
886
| _components.put(component.getId(), component);
|
170 |
| } |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
1595
| public void addBody(IRender element)
|
180 |
| { |
181 |
1595
| Defense.notNull(element, "element");
|
182 |
| |
183 |
| |
184 |
| |
185 |
| |
186 |
| |
187 |
| |
188 |
| |
189 |
| |
190 |
| |
191 |
| |
192 |
| |
193 |
1595
| if (_body == null)
|
194 |
| { |
195 |
470
| _body = new IRender[BODY_INIT_SIZE];
|
196 |
470
| _body[0] = element;
|
197 |
| |
198 |
470
| _bodyCount = 1;
|
199 |
470
| return;
|
200 |
| } |
201 |
| |
202 |
| |
203 |
| |
204 |
1125
| if (_bodyCount == _body.length)
|
205 |
| { |
206 |
87
| IRender[] newWrapped;
|
207 |
| |
208 |
87
| newWrapped = new IRender[_body.length * 2];
|
209 |
| |
210 |
87
| System.arraycopy(_body, 0, newWrapped, 0, _bodyCount);
|
211 |
| |
212 |
87
| _body = newWrapped;
|
213 |
| } |
214 |
| |
215 |
1125
| _body[_bodyCount++] = element;
|
216 |
| } |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
997
| public void finishLoad(IRequestCycle cycle, IPageLoader loader,
|
224 |
| IComponentSpecification specification) |
225 |
| { |
226 |
997
| finishLoad();
|
227 |
| } |
228 |
| |
229 |
| |
230 |
| |
231 |
| |
232 |
| |
233 |
| |
234 |
| |
235 |
| |
236 |
| |
237 |
| |
238 |
| |
239 |
| |
240 |
| |
241 |
| |
242 |
| |
243 |
| |
244 |
| |
245 |
| |
246 |
| |
247 |
| |
248 |
| |
249 |
| |
250 |
| |
251 |
| |
252 |
| |
253 |
| |
254 |
1452
| protected void renderInformalParameters(IMarkupWriter writer, IRequestCycle cycle)
|
255 |
| { |
256 |
1452
| String attribute;
|
257 |
| |
258 |
1452
| if (_bindings == null)
|
259 |
34
| return;
|
260 |
| |
261 |
1418
| Iterator i = _bindings.entrySet().iterator();
|
262 |
| |
263 |
1418
| while (i.hasNext())
|
264 |
| { |
265 |
3984
| Map.Entry entry = (Map.Entry) i.next();
|
266 |
3984
| String name = (String) entry.getKey();
|
267 |
| |
268 |
3984
| if (isFormalParameter(name))
|
269 |
3906
| continue;
|
270 |
| |
271 |
78
| IBinding binding = (IBinding) entry.getValue();
|
272 |
| |
273 |
78
| Object value = binding.getObject();
|
274 |
78
| if (value == null)
|
275 |
0
| continue;
|
276 |
| |
277 |
78
| if (value instanceof IAsset)
|
278 |
| { |
279 |
0
| IAsset asset = (IAsset) value;
|
280 |
| |
281 |
| |
282 |
| |
283 |
0
| attribute = asset.buildURL();
|
284 |
| } |
285 |
| else |
286 |
78
| attribute = value.toString();
|
287 |
| |
288 |
78
| writer.attribute(name, attribute);
|
289 |
| } |
290 |
| |
291 |
| } |
292 |
| |
293 |
| |
294 |
3984
| private boolean isFormalParameter(String name)
|
295 |
| { |
296 |
3984
| Defense.notNull(name, "name");
|
297 |
| |
298 |
3984
| return getSpecification().getParameter(name) != null;
|
299 |
| } |
300 |
| |
301 |
| |
302 |
| |
303 |
| |
304 |
| |
305 |
| |
306 |
| |
307 |
| |
308 |
| |
309 |
| |
310 |
| |
311 |
20224
| public IBinding getBinding(String name)
|
312 |
| { |
313 |
20224
| Defense.notNull(name, "name");
|
314 |
| |
315 |
20224
| if (_bindings == null)
|
316 |
868
| return null;
|
317 |
| |
318 |
19356
| return (IBinding) _bindings.get(name);
|
319 |
| } |
320 |
| |
321 |
| |
322 |
| |
323 |
| |
324 |
| |
325 |
| |
326 |
| |
327 |
155
| public boolean isParameterBound(String parameterName)
|
328 |
| { |
329 |
155
| Defense.notNull(parameterName, "parameterName");
|
330 |
| |
331 |
155
| return _bindings != null && _bindings.containsKey(parameterName);
|
332 |
| } |
333 |
| |
334 |
609
| public IComponent getComponent(String id)
|
335 |
| { |
336 |
609
| Defense.notNull(id, "id");
|
337 |
| |
338 |
609
| IComponent result = null;
|
339 |
| |
340 |
609
| if (_components != null)
|
341 |
609
| result = (IComponent) _components.get(id);
|
342 |
| |
343 |
609
| if (result == null)
|
344 |
0
| throw new ApplicationRuntimeException(Tapestry.format("no-such-component", this, id),
|
345 |
| this, null, null); |
346 |
| |
347 |
609
| return result;
|
348 |
| } |
349 |
| |
350 |
31
| public IComponent getContainer()
|
351 |
| { |
352 |
31
| return _container;
|
353 |
| } |
354 |
| |
355 |
897
| public void setContainer(IComponent value)
|
356 |
| { |
357 |
897
| checkActiveLock();
|
358 |
| |
359 |
897
| if (_container != null)
|
360 |
0
| throw new ApplicationRuntimeException(Tapestry
|
361 |
| .getMessage("AbstractComponent.attempt-to-change-container")); |
362 |
| |
363 |
897
| _container = value;
|
364 |
| } |
365 |
| |
366 |
| |
367 |
| |
368 |
| |
369 |
| |
370 |
| |
371 |
| |
372 |
| |
373 |
200
| public String getExtendedId()
|
374 |
| { |
375 |
200
| if (_page == null)
|
376 |
76
| return null;
|
377 |
| |
378 |
124
| return _page.getPageName() + "/" + getIdPath();
|
379 |
| } |
380 |
| |
381 |
1033
| public String getId()
|
382 |
| { |
383 |
1033
| return _id;
|
384 |
| } |
385 |
| |
386 |
896
| public void setId(String value)
|
387 |
| { |
388 |
896
| if (_id != null)
|
389 |
0
| throw new ApplicationRuntimeException(Tapestry
|
390 |
| .getMessage("AbstractComponent.attempt-to-change-component-id")); |
391 |
| |
392 |
896
| _id = value;
|
393 |
| } |
394 |
| |
395 |
208
| public String getIdPath()
|
396 |
| { |
397 |
208
| String containerIdPath;
|
398 |
| |
399 |
208
| if (_container == null)
|
400 |
0
| throw new NullPointerException(Tapestry
|
401 |
| .format("AbstractComponent.null-container", this)); |
402 |
| |
403 |
208
| containerIdPath = _container.getIdPath();
|
404 |
| |
405 |
208
| if (containerIdPath == null)
|
406 |
198
| _idPath = _id;
|
407 |
| else |
408 |
10
| _idPath = containerIdPath + "." + _id;
|
409 |
| |
410 |
208
| return _idPath;
|
411 |
| } |
412 |
| |
413 |
1325
| public IPage getPage()
|
414 |
| { |
415 |
1325
| return _page;
|
416 |
| } |
417 |
| |
418 |
1032
| public void setPage(IPage value)
|
419 |
| { |
420 |
1032
| if (_page != null)
|
421 |
0
| throw new ApplicationRuntimeException(Tapestry
|
422 |
| .getMessage("AbstractComponent.attempt-to-change-page")); |
423 |
| |
424 |
1032
| _page = value;
|
425 |
| } |
426 |
| |
427 |
| |
428 |
| |
429 |
| |
430 |
| |
431 |
1666
| public void renderBody(IMarkupWriter writer, IRequestCycle cycle)
|
432 |
| { |
433 |
1666
| for (int i = 0; i < _bodyCount; i++)
|
434 |
4514
| _body[i].render(writer, cycle);
|
435 |
| } |
436 |
| |
437 |
| |
438 |
| |
439 |
| |
440 |
| |
441 |
| |
442 |
| |
443 |
| |
444 |
1549
| public void setBinding(String name, IBinding binding)
|
445 |
| { |
446 |
1549
| Defense.notNull(name, "name");
|
447 |
1549
| Defense.notNull(binding, "binding");
|
448 |
| |
449 |
1549
| if (_bindings == null)
|
450 |
839
| _bindings = new HashMap(MAP_SIZE);
|
451 |
| |
452 |
1549
| _bindings.put(name, binding);
|
453 |
| } |
454 |
| |
455 |
116
| public String toString()
|
456 |
| { |
457 |
116
| StringBuffer buffer;
|
458 |
| |
459 |
116
| buffer = new StringBuffer(super.toString());
|
460 |
| |
461 |
116
| buffer.append('[');
|
462 |
| |
463 |
116
| buffer.append(getExtendedId());
|
464 |
| |
465 |
116
| buffer.append(']');
|
466 |
| |
467 |
116
| return buffer.toString();
|
468 |
| } |
469 |
| |
470 |
| |
471 |
| |
472 |
| |
473 |
| |
474 |
| |
475 |
2266
| public Map getComponents()
|
476 |
| { |
477 |
2266
| if (_components == null)
|
478 |
1609
| return EMPTY_MAP;
|
479 |
| |
480 |
657
| return Collections.unmodifiableMap(_components);
|
481 |
| |
482 |
| } |
483 |
| |
484 |
27
| public Map getAssets()
|
485 |
| { |
486 |
27
| if (_assets == null)
|
487 |
0
| return EMPTY_MAP;
|
488 |
| |
489 |
27
| return Collections.unmodifiableMap(_assets);
|
490 |
| } |
491 |
| |
492 |
185
| public IAsset getAsset(String name)
|
493 |
| { |
494 |
185
| if (_assets == null)
|
495 |
131
| return null;
|
496 |
| |
497 |
54
| return (IAsset) _assets.get(name);
|
498 |
| } |
499 |
| |
500 |
4
| public Collection getBindingNames()
|
501 |
| { |
502 |
| |
503 |
| |
504 |
4
| if (_container == null)
|
505 |
0
| return null;
|
506 |
| |
507 |
4
| HashSet result = new HashSet();
|
508 |
| |
509 |
| |
510 |
| |
511 |
4
| if (_bindings != null)
|
512 |
2
| result.addAll(_bindings.keySet());
|
513 |
| |
514 |
| |
515 |
| |
516 |
| |
517 |
4
| List names = getSpecification().getParameterNames();
|
518 |
| |
519 |
4
| int count = names.size();
|
520 |
| |
521 |
4
| for (int i = 0; i < count; i++)
|
522 |
| { |
523 |
1
| String name = (String) names.get(i);
|
524 |
| |
525 |
1
| if (result.contains(name))
|
526 |
1
| continue;
|
527 |
| |
528 |
0
| if (getBinding(name) != null)
|
529 |
0
| result.add(name);
|
530 |
| } |
531 |
| |
532 |
4
| return result;
|
533 |
| } |
534 |
| |
535 |
| |
536 |
| |
537 |
| |
538 |
| |
539 |
| |
540 |
| |
541 |
0
| public Map getBindings()
|
542 |
| { |
543 |
0
| if (_bindings == null)
|
544 |
0
| return Collections.EMPTY_MAP;
|
545 |
| |
546 |
0
| return Collections.unmodifiableMap(_bindings);
|
547 |
| } |
548 |
| |
549 |
| |
550 |
| |
551 |
| |
552 |
| |
553 |
| |
554 |
| |
555 |
| |
556 |
| |
557 |
28
| public ListenerMap getListeners()
|
558 |
| { |
559 |
| |
560 |
| |
561 |
| |
562 |
| |
563 |
28
| if (_listeners == null)
|
564 |
24
| _listeners = getPage().getEngine().getInfrastructure().getListenerMapSource()
|
565 |
| .getListenerMapForObject(this); |
566 |
| |
567 |
28
| return _listeners;
|
568 |
| } |
569 |
| |
570 |
| |
571 |
| |
572 |
| |
573 |
| |
574 |
| |
575 |
| |
576 |
| |
577 |
152
| public IBeanProvider getBeans()
|
578 |
| { |
579 |
152
| if (_beans == null)
|
580 |
42
| _beans = new BeanProvider(this);
|
581 |
| |
582 |
152
| return _beans;
|
583 |
| } |
584 |
| |
585 |
| |
586 |
| |
587 |
| |
588 |
| |
589 |
| |
590 |
| |
591 |
| |
592 |
| |
593 |
931
| protected void finishLoad()
|
594 |
| { |
595 |
| } |
596 |
| |
597 |
| |
598 |
| |
599 |
| |
600 |
| |
601 |
| |
602 |
| |
603 |
| |
604 |
| |
605 |
| |
606 |
| |
607 |
| |
608 |
| |
609 |
2540
| public final void render(IMarkupWriter writer, IRequestCycle cycle)
|
610 |
| { |
611 |
2540
| try
|
612 |
| { |
613 |
2540
| _rendering = true;
|
614 |
| |
615 |
2540
| prepareForRender(cycle);
|
616 |
| |
617 |
2538
| renderComponent(writer, cycle);
|
618 |
| } |
619 |
| finally |
620 |
| { |
621 |
2540
| _rendering = false;
|
622 |
| |
623 |
2540
| cleanupAfterRender(cycle);
|
624 |
| } |
625 |
| } |
626 |
| |
627 |
| |
628 |
| |
629 |
| |
630 |
| |
631 |
| |
632 |
| |
633 |
| |
634 |
| |
635 |
2433
| protected void prepareForRender(IRequestCycle cycle)
|
636 |
| { |
637 |
| } |
638 |
| |
639 |
| |
640 |
| |
641 |
| |
642 |
| |
643 |
| |
644 |
| |
645 |
| |
646 |
| protected abstract void renderComponent(IMarkupWriter writer, IRequestCycle cycle); |
647 |
| |
648 |
| |
649 |
| |
650 |
| |
651 |
| |
652 |
| |
653 |
| |
654 |
| |
655 |
2433
| protected void cleanupAfterRender(IRequestCycle cycle)
|
656 |
| { |
657 |
| } |
658 |
| |
659 |
1701
| public INamespace getNamespace()
|
660 |
| { |
661 |
1701
| return _namespace;
|
662 |
| } |
663 |
| |
664 |
1024
| public void setNamespace(INamespace namespace)
|
665 |
| { |
666 |
1024
| _namespace = namespace;
|
667 |
| } |
668 |
| |
669 |
| |
670 |
| |
671 |
| |
672 |
| |
673 |
| |
674 |
| |
675 |
| |
676 |
| |
677 |
| |
678 |
0
| public IRender[] getBody()
|
679 |
| { |
680 |
0
| return _body;
|
681 |
| } |
682 |
| |
683 |
| |
684 |
| |
685 |
| |
686 |
| |
687 |
| |
688 |
| |
689 |
| |
690 |
0
| public int getBodyCount()
|
691 |
| { |
692 |
0
| return _bodyCount;
|
693 |
| } |
694 |
| |
695 |
| |
696 |
| |
697 |
| |
698 |
| |
699 |
| |
700 |
| |
701 |
| |
702 |
| |
703 |
| |
704 |
0
| public void pageEndRender(PageEvent event)
|
705 |
| { |
706 |
| } |
707 |
| |
708 |
| |
709 |
| |
710 |
| |
711 |
| |
712 |
| |
713 |
| |
714 |
| |
715 |
22
| public void setProperty(String propertyName, Object value)
|
716 |
| { |
717 |
22
| PropertyUtils.write(this, propertyName, value);
|
718 |
| } |
719 |
| |
720 |
| |
721 |
| |
722 |
| |
723 |
| |
724 |
| |
725 |
| |
726 |
| |
727 |
4
| public Object getProperty(String propertyName)
|
728 |
| { |
729 |
4
| return PropertyUtils.read(this, propertyName);
|
730 |
| } |
731 |
| |
732 |
| |
733 |
| |
734 |
| |
735 |
| |
736 |
3641
| public final boolean isRendering()
|
737 |
| { |
738 |
3641
| return _rendering;
|
739 |
| } |
740 |
| |
741 |
| |
742 |
| |
743 |
| |
744 |
| |
745 |
| |
746 |
| |
747 |
| |
748 |
1177
| protected final boolean isInActiveState()
|
749 |
| { |
750 |
1177
| return _active;
|
751 |
| } |
752 |
| |
753 |
| |
754 |
995
| public final void enterActiveState()
|
755 |
| { |
756 |
995
| _active = true;
|
757 |
| } |
758 |
| |
759 |
| |
760 |
| |
761 |
1836
| protected final void checkActiveLock()
|
762 |
| { |
763 |
1836
| if (_active)
|
764 |
0
| throw new UnsupportedOperationException(TapestryMessages.componentIsLocked(this));
|
765 |
| } |
766 |
| |
767 |
1
| public Messages getMessages()
|
768 |
| { |
769 |
1
| throw new IllegalStateException(TapestryMessages.providedByEnhancement("getMessages"));
|
770 |
| } |
771 |
| |
772 |
1
| public IComponentSpecification getSpecification()
|
773 |
| { |
774 |
1
| throw new IllegalStateException(TapestryMessages.providedByEnhancement("getSpecification"));
|
775 |
| } |
776 |
| |
777 |
| |
778 |
| |
779 |
| |
780 |
| |
781 |
| |
782 |
| |
783 |
| |
784 |
1
| public String getMessage(String key)
|
785 |
| { |
786 |
1
| return getMessages().getMessage(key);
|
787 |
| } |
788 |
| |
789 |
| |
790 |
| |
791 |
| |
792 |
| |
793 |
| |
794 |
| |
795 |
| |
796 |
| |
797 |
| |
798 |
| |
799 |
| |
800 |
| |
801 |
1
| public String format(String key, Object[] arguments)
|
802 |
| { |
803 |
1
| return getMessages().format(key, arguments);
|
804 |
| } |
805 |
| |
806 |
| |
807 |
| |
808 |
| |
809 |
| |
810 |
| |
811 |
| |
812 |
| |
813 |
1
| public String format(String key, Object argument)
|
814 |
| { |
815 |
1
| return getMessages().format(key, argument);
|
816 |
| } |
817 |
| |
818 |
| |
819 |
| |
820 |
| |
821 |
| |
822 |
| |
823 |
| |
824 |
| |
825 |
1
| public String format(String key, Object argument1, Object argument2)
|
826 |
| { |
827 |
1
| return getMessages().format(key, argument1, argument2);
|
828 |
| } |
829 |
| |
830 |
| |
831 |
| |
832 |
| |
833 |
| |
834 |
| |
835 |
| |
836 |
| |
837 |
1
| public String format(String key, Object argument1, Object argument2, Object argument3)
|
838 |
| { |
839 |
1
| return getMessages().format(key, argument1, argument2, argument3);
|
840 |
| } |
841 |
| |
842 |
| |
843 |
1
| public final IContainedComponent getContainedComponent()
|
844 |
| { |
845 |
1
| return _containedComponent;
|
846 |
| } |
847 |
| |
848 |
| |
849 |
889
| public final void setContainedComponent(IContainedComponent containedComponent)
|
850 |
| { |
851 |
889
| Defense.notNull(containedComponent, "containedComponent");
|
852 |
| |
853 |
889
| if (_containedComponent != null)
|
854 |
1
| throw new ApplicationRuntimeException(TapestryMessages
|
855 |
| .attemptToChangeContainedComponent(this)); |
856 |
| |
857 |
888
| _containedComponent = containedComponent;
|
858 |
| } |
859 |
| |
860 |
| } |