Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BaseComponent |
|
| 2.0;2 |
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; | |
16 | ||
17 | import org.apache.commons.logging.Log; | |
18 | import org.apache.commons.logging.LogFactory; | |
19 | import org.apache.tapestry.engine.IPageLoader; | |
20 | import org.apache.tapestry.spec.IComponentSpecification; | |
21 | ||
22 | /** | |
23 | * Base implementation for most components that use an HTML template. | |
24 | * | |
25 | * @author Howard Lewis Ship | |
26 | */ | |
27 | ||
28 | 0 | public abstract class BaseComponent extends AbstractComponent implements ITemplateComponent |
29 | { | |
30 | 0 | private static final Log LOG = LogFactory.getLog(BaseComponent.class); |
31 | ||
32 | private static final int OUTER_INIT_SIZE = 5; | |
33 | ||
34 | private IRender[] _outer; | |
35 | ||
36 | 0 | private int _outerCount = 0; |
37 | ||
38 | /** | |
39 | * Adds an element as an outer element for the receiver. Outer elements are elements that should | |
40 | * be directly rendered by the receiver's <code>render()</code> method. That is, they are | |
41 | * top-level elements on the HTML template. | |
42 | */ | |
43 | ||
44 | public void addOuter(IRender element) | |
45 | { | |
46 | 0 | if (_outer == null) |
47 | { | |
48 | 0 | _outer = new IRender[OUTER_INIT_SIZE]; |
49 | 0 | _outer[0] = element; |
50 | ||
51 | 0 | _outerCount = 1; |
52 | 0 | return; |
53 | } | |
54 | ||
55 | // No more room? Make the array bigger. | |
56 | ||
57 | 0 | if (_outerCount == _outer.length) |
58 | { | |
59 | IRender[] newOuter; | |
60 | ||
61 | 0 | newOuter = new IRender[_outer.length * 2]; |
62 | ||
63 | 0 | System.arraycopy(_outer, 0, newOuter, 0, _outerCount); |
64 | ||
65 | 0 | _outer = newOuter; |
66 | } | |
67 | ||
68 | 0 | _outer[_outerCount++] = element; |
69 | 0 | } |
70 | ||
71 | public IRender[] getContainedRenderers() | |
72 | { | |
73 | 0 | return _outer; |
74 | } | |
75 | ||
76 | public IRender[] getInnerRenderers() | |
77 | { | |
78 | 0 | return _body; |
79 | } | |
80 | ||
81 | /** | |
82 | * Reads the receiver's template and figures out which elements wrap which other elements. | |
83 | * | |
84 | * @param cycle | |
85 | * The current request. | |
86 | * @param loader | |
87 | * The service responsible for loading / resolving a page spec. | |
88 | */ | |
89 | ||
90 | private void readTemplate(IRequestCycle cycle, IPageLoader loader) | |
91 | { | |
92 | 0 | loader.loadTemplateForComponent(cycle, this); |
93 | 0 | } |
94 | ||
95 | /** | |
96 | * Renders the top level components contained by the receiver. | |
97 | * | |
98 | * @since 2.0.3 | |
99 | */ | |
100 | ||
101 | protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) | |
102 | { | |
103 | 0 | if (LOG.isDebugEnabled()) |
104 | 0 | LOG.debug("Begin render " + getExtendedId()); |
105 | ||
106 | 0 | for (int i = 0; i < _outerCount; i++) |
107 | 0 | cycle.getResponseBuilder().render(writer, _outer[i], cycle); |
108 | ||
109 | 0 | if (LOG.isDebugEnabled()) |
110 | 0 | LOG.debug("End render " + getExtendedId()); |
111 | 0 | } |
112 | ||
113 | /** | |
114 | * Loads the template for the component, then invokes | |
115 | * {@link AbstractComponent#finishLoad(IRequestCycle, IPageLoader, IComponentSpecification)}. | |
116 | * Subclasses must invoke this method first, before adding any additional behavior, though its | |
117 | * usually simpler to override {@link #finishLoad()}instead. | |
118 | */ | |
119 | ||
120 | public void finishLoad(IRequestCycle cycle, IPageLoader loader, IComponentSpecification specification) | |
121 | { | |
122 | 0 | readTemplate(cycle, loader); |
123 | ||
124 | 0 | super.finishLoad(cycle, loader, specification); |
125 | 0 | } |
126 | } |