KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > jsfext > renderer > TemplateRenderer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.tools.jsfext.renderer;
24
25 import com.sun.enterprise.tools.jsfext.component.TemplateComponent;
26 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutDefinition;
27 import com.sun.enterprise.tools.jsfext.layout.descriptor.Resource;
28
29 import java.io.IOException JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.faces.context.FacesContext;
33 import javax.faces.component.UIComponent;
34 import javax.faces.render.Renderer;
35
36
37 /**
38  * <p> This renderer is a generic "template-based" renderer. It uses a
39  * <code>LayoutElement</code> tree as its template and walks this tree.
40  * This renderer will actually delegate the encode functionality to the
41  * {@link LayoutDefinition} object, which is the top of the
42  * <code>LayoutElement</code> in the tree.</p>
43  *
44  * <p> This renderer also has the feature of registering {@link Resource}
45  * objects to the Request scope prior to rendering its output. This
46  * allows {@link Resource} objects such as ResourceBundles to be added
47  * to the Request scope for easy access.</p>
48  *
49  * @see LayoutDefinition
50  * @see com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutElement
51  * @see Resource
52  *
53  * @author Ken Paulsen (ken.paulsen@sun.com)
54  */

55 public class TemplateRenderer extends Renderer {
56
57     /**
58      * <p> This method returns true. This method indicates that this
59      * <code>Renderer</code> will assume resposibilty for rendering its
60      * own children.</p>
61      *
62      * @return true
63      *
64      * @see #encodeChildren(FacesContext, UIComponent)
65      */

66     public boolean getRendersChildren() {
67     return true;
68     }
69
70
71     /**
72      * <p> This method initializes the Resources so they will be available for
73      * children. It then calls encodeBegin on the superclass.</p>
74      *
75      * @param context The FacesContext
76      * @param component The UIComponent, should be a
77      * {@link TemplateComponent}
78      */

79     public void encodeBegin(FacesContext context, UIComponent component) throws IOException JavaDoc {
80     // Make sure we have a TemplateComponent
81
if (!(component instanceof TemplateComponent)) {
82         throw new IllegalArgumentException JavaDoc(
83         "TemplateRenderer requires that its UIComponent be an "
84         + "instance of TemplateComponent!");
85     }
86     TemplateComponent tempComp = (TemplateComponent) component;
87     LayoutDefinition def = tempComp.getLayoutDefinition(context);
88
89     // First ensure that our Resources are available
90
Iterator JavaDoc it = def.getResources().iterator();
91     Resource resource = null;
92     while (it.hasNext()) {
93         resource = (Resource) it.next();
94         // Just calling getResource() puts it in the Request scope
95
resource.getFactory().getResource(context, resource);
96     }
97
98     // Call the super class
99
super.encodeBegin(context, component);
100     }
101
102     /**
103      * <p> This method prevents the super class's default functionality of
104      * rendering the child UIComponents. This <code>Renderer</code>
105      * implementation requires that the children be explicitly
106      * rendered. This method does nothing.</p>
107      *
108      * @param context The <code>FacesContext</code>
109      * @param component The <code>UIComponent</code>
110      */

111     public void encodeChildren(FacesContext context, UIComponent component) {
112     // Do nothing...
113
}
114
115     /**
116      * <p> This method performs the rendering for the TemplateRenderer. It
117      * expects that component be an instanceof {@link TemplateComponent}.
118      * It obtains the {@link LayoutDefinition} from the
119      * {@link TemplateComponent}, initializes the {@link Resource}
120      * objects defined by the {@link LayoutDefinition} (if any), and
121      * finally delegates the encoding to the
122      * {@link LayoutDefinition#encode(FacesContext, TemplateComponent)}
123      * method of the {@link LayoutDefinition}.</p>
124      *
125      * @param context The FacesContext object
126      * @param component The {@link TemplateComponent}
127      */

128     public void encodeEnd(FacesContext context, UIComponent component) throws IOException JavaDoc {
129     // Sanity Check...
130
if (!component.isRendered()) {
131         return;
132     }
133
134     // Get the LayoutDefinition and begin rendering
135
TemplateComponent tempComp = (TemplateComponent) component;
136     LayoutDefinition def = tempComp.getLayoutDefinition(context);
137
138     // The following "encode" method does all the rendering
139
def.encode(context, (UIComponent) tempComp);
140     }
141
142     /**
143      * <p> Decode any new state of the specified UIComponent from the request
144      * contained in the specified FacesContext, and store that state on
145      * the UIComponent.</p>
146      *
147      * <p> During decoding, events may be queued for later processing (by
148      * event listeners that have registered an interest), by calling
149      * <code>queueEvent()</code> on the associated UIComponent.</p>
150      *
151      * <p> This implementation of this method invokes the super class and
152      * then any handlers that have been registered to process decode
153      * functionality. The execution of these handlers is delegated to
154      * the LayoutDefinition.</p>
155      *
156      * @param context FacesContext for the request we are processing
157      * @param component UIComponent to be decoded.
158      *
159      * @exception NullPointerException if <code>context</code>
160      * or <code>component</code> is <code>null</code>
161      */

162     public void decode(FacesContext context, UIComponent component) {
163     // Call the super first
164
super.decode(context, component);
165
166     // Call any decode handlers
167
TemplateComponent tempComp = (TemplateComponent) component;
168     LayoutDefinition def = tempComp.getLayoutDefinition(context);
169     def.decode(context, component);
170     }
171 }
172
Popular Tags