KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > components > Block


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.hivemind.util.Defense;
18 import org.apache.tapestry.AbstractComponent;
19 import org.apache.tapestry.IComponent;
20 import org.apache.tapestry.IMarkupWriter;
21 import org.apache.tapestry.IRequestCycle;
22
23 /**
24  * Prevents its contents from being rendered until triggered by an {@link RenderBlock} component. [<a
25  * HREF="../../../../../ComponentReference/Block.html">Component Reference</a>]
26  * <p>
27  * Block and {@link RenderBlock} are used to build a certain class of complicated component that
28  * can't be assembled using the normal wrapping containment. Such a super component would have two
29  * or more sections that need to be supplied by the containing page (or component).
30  * <p>
31  * Using Blocks, the blocks can be provided as parameters to the super component.
32  * <p>
33  * The invoker property gives the components inside the block access to the component (typically an
34  * {@link RenderBlock}) that rendered the block. More often, the {@link #getParameter(String)}
35  * method is used to get parameters <em>of the invoking component</em>.
36  *
37  * @author Howard Lewis Ship
38  * @since 0.2.9
39  */

40
41 public abstract class Block extends AbstractComponent
42 {
43     private IComponent _invoker;
44
45     /**
46      * Provides access to the invoking component's parameters.
47      *
48      * @since 4.0
49      */

50
51     public Object JavaDoc getParameter(String JavaDoc name)
52     {
53         return _invoker.getBinding(name).getObject();
54     }
55
56     public void renderForComponent(IMarkupWriter writer, IRequestCycle cycle, IComponent invoker)
57     {
58         Defense.notNull(invoker, "invoker");
59
60         IComponent oldInvoker = _invoker;
61
62         try
63         {
64             _invoker = invoker;
65             renderBody(writer, cycle);
66         }
67         finally
68         {
69             _invoker = oldInvoker;
70
71         }
72     }
73
74     /**
75      * Does nothing; the idea of a Block is to defer the rendering of the body of the block until an
76      * {@link RenderBlock} forces it out.
77      */

78
79     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
80     {
81         // Nothing!
82
}
83
84     /**
85      * @deprecated Use {@link #getInvoker()} instead.
86      */

87     public IComponent getInserter()
88     {
89         return _invoker;
90     }
91
92     /**
93      * Returns the object which invoked this Block's
94      * {@link #renderForComponent(IMarkupWriter, IRequestCycle, IComponent)} method. This is often
95      * used to access the informal parameters of a {@link RenderBlock} component.
96      *
97      * @since 4.0
98      */

99     public IComponent getInvoker()
100     {
101         return _invoker;
102     }
103
104     /**
105      * Used for testing only.
106      *
107      * @since 4.0
108      */

109
110     void setInvoker(IComponent invoker)
111     {
112         _invoker = invoker;
113     }
114 }
Popular Tags