Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
BlockRenderer |
|
| 1.0;1 |
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.components; | |
16 | ||
17 | import org.apache.tapestry.IMarkupWriter; | |
18 | import org.apache.tapestry.IRender; | |
19 | import org.apache.tapestry.IRequestCycle; | |
20 | ||
21 | /** | |
22 | * An implementation of IRender that renders a Block component. | |
23 | * <p> | |
24 | * The BlockRenderer allows the contents of a {@link Block} to be rendered via | |
25 | * {@link IRender}. It can be used in cases when an {@link IRender} object is | |
26 | * required as an argument or a binding to render a part of a Component. To | |
27 | * provide a complicated view, it could be defined in a {@link Block} and then | |
28 | * returned encapsulated in a BlockRenderer. | |
29 | * <p> | |
30 | * It is important to note that a special care has to be taken if the | |
31 | * BlockRenderer is used within an inner class of a component or a page. In such | |
32 | * a case the instance of the component that created the inner class may not be | |
33 | * the currently active instance in the RequestCycle when the BlockRenderer is | |
34 | * required. Thus, calling getComponent("blockName") to get the block component | |
35 | * may return a Block component that is not initialized for this RequestCycle. | |
36 | * <p> | |
37 | * To avoid similar problems, the ComponentAddress class could be used in | |
38 | * conjunction with BlockRenderer. Here is a quick example of how BlockRenderer | |
39 | * could be used with ComponentAddress: | |
40 | * <p> | |
41 | * <code> | |
42 | * <br>// Create a component address for the current component | |
43 | * <br>final ComponentAddress address = new ComponentAddress(this); | |
44 | * <br>return new SomeClass() { | |
45 | * <br> IRender getRenderer(IRequestCycle cycle) { | |
46 | * <br> MyComponent component = (MyComponent) address.findComponent(cycle); | |
47 | * <br> // initialize variables in the component that will be used by the block here | |
48 | * <br> return new BlockRenderer(component.getComponent("block")); | |
49 | * <br> } | |
50 | * <br>} | |
51 | * </code> | |
52 | * | |
53 | * @author mindbridge | |
54 | * @since 2.2 | |
55 | */ | |
56 | public class BlockRenderer implements IRender | |
57 | { | |
58 | ||
59 | private Block m_objBlock; | |
60 | ||
61 | /** | |
62 | * Creates a new BlockRenderer that will render the content of the argument. | |
63 | * | |
64 | * @param objBlock | |
65 | * the Block to be rendered | |
66 | */ | |
67 | public BlockRenderer(Block objBlock) | |
68 | 0 | { |
69 | 0 | m_objBlock = objBlock; |
70 | 0 | } |
71 | ||
72 | /** | |
73 | * @see org.apache.tapestry.IRender#render(IMarkupWriter, IRequestCycle) | |
74 | */ | |
75 | public void render(IMarkupWriter writer, IRequestCycle cycle) | |
76 | { | |
77 | 0 | m_objBlock.renderBody(writer, cycle); |
78 | 0 | } |
79 | ||
80 | } |