KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > jsfext > component > TemplateComponentBase


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.component;
24
25 import com.sun.enterprise.tools.jsfext.layout.LayoutDefinitionManager;
26 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutComponent;
27 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutDefinition;
28 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutElement;
29
30 import java.io.IOException JavaDoc;
31
32 import javax.faces.component.UIComponent;
33 import javax.faces.component.UIComponentBase;
34 import javax.faces.context.FacesContext;
35
36
37 /**
38  * <p> This abstract class provides base functionality for components that
39  * work in conjunction with the
40  * {@link com.sun.enterprise.tools.jsfext.renderer.TemplateRenderer}. It
41  * provides a default implementation of the
42  * {@link com.sun.enterprise.tools.jsfext.component.TemplateComponent}
43  * interface.</p>
44  *
45  * @see com.sun.enterprise.tools.jsfext.renderer.TemplateRenderer
46  * @see com.sun.enterprise.tools.jsfext.component.TemplateComponent
47  *
48  * @author Ken Paulsen (ken.paulsen@sun.com)
49  */

50 public abstract class TemplateComponentBase extends UIComponentBase implements TemplateComponent {
51
52     /**
53      * This method will find the request child UIComponent by id. If it is
54      * not found, it will attempt to create it if it can find a LayoutElement
55      * describing it.
56      *
57      * @param context The FacesContext
58      * @param id The UIComponent id to search for
59      *
60      * @return The requested UIComponent
61      */

62     public UIComponent getChild(FacesContext context, String JavaDoc id) {
63     if ((id == null) || (id.trim().equals(""))) {
64         // No id, no LayoutComponent, nothing we can do.
65
return null;
66     }
67
68     // We have an id, use it to search for an already-created child
69
// FIXME: I am doing this 2x if it falls through to create the child...
70
// FIXME: think about optimizing this
71
UIComponent childComponent = ComponentUtil.findChild(this, id, id);
72     if (childComponent != null) {
73         return childComponent;
74     }
75
76     // If we're still here, then we need to create it... hopefully we have
77
// a LayoutComponent to tell us how to do this!
78
LayoutDefinition ld = getLayoutDefinition(context);
79     if (ld == null) {
80         // No LayoutDefinition to tell us how to create it... return null
81
return null;
82     }
83
84     // Attempt to find a LayoutComponent matching the id
85
LayoutElement elt =
86         LayoutDefinition.getChildLayoutElementById(context, id, ld, this);
87
88     // Create the child from the LayoutComponent
89
return getChild(context, (LayoutComponent) elt);
90     }
91
92
93     /**
94      * This method will find the request child UIComponent by id (the id is
95      * obtained from the given LayoutComponent). If it is not found, it will
96      * attempt to create it from the supplied LayoutElement.
97      *
98      * @param descriptor The LayoutElement describing the UIComponent
99      *
100      * @return The requested UIComponent
101      */

102     public UIComponent getChild(FacesContext context, LayoutComponent descriptor) {
103     UIComponent childComponent = null;
104
105     // Sanity check
106
if (descriptor == null) {
107         throw new IllegalArgumentException JavaDoc("The LayoutComponent is null!");
108     }
109
110     // First pull off the id from the descriptor
111
String JavaDoc id = descriptor.getId(context, this);
112     if ((id != null) && !(id.trim().equals(""))) {
113         // We have an id, use it to search for an already-created child
114
childComponent = ComponentUtil.findChild(this, id, id);
115         if (childComponent != null) {
116         return childComponent;
117         }
118     }
119
120     // No id, or the component hasn't been created. In either case, we
121
// create a new component (moral: always have an id)
122

123     // Invoke "beforeCreate" handlers
124
descriptor.beforeCreate(context, this);
125
126     // Create UIComponent
127
childComponent =
128         ComponentUtil.createChildComponent(context, descriptor, this);
129
130     // Invoke "afterCreate" handlers
131
descriptor.afterCreate(context, childComponent);
132
133     // Return the newly created UIComponent
134
return childComponent;
135     }
136
137     /**
138      * This method returns the LayoutDefinition associated with this component.
139      *
140      * @param context The FacesContext
141      *
142      * @return LayoutDefinition associated with this component.
143      */

144     public LayoutDefinition getLayoutDefinition(FacesContext context) {
145     // Make sure we don't already have it...
146
if (_layoutDefinition != null) {
147         return _layoutDefinition;
148     }
149
150     // Get the LayoutDefinitionManager key
151
String JavaDoc key = getLayoutDefinitionKey();
152     if (key == null) {
153         throw new NullPointerException JavaDoc("LayoutDefinition key is null!");
154     }
155
156     // Get the LayoutDefinitionManager
157
LayoutDefinitionManager ldm =
158         LayoutDefinitionManager.getManager(context);
159
160     // Save the LayoutDefinition for future calls to this method
161
try {
162         _layoutDefinition = ldm.getLayoutDefinition(key);
163     } catch (IOException JavaDoc ex) {
164         throw new IllegalArgumentException JavaDoc(
165             "A LayoutDefinition was not provided for '" + key
166             + "'! This is required.", ex);
167     }
168
169     // Return the LayoutDefinition (if found)
170
return _layoutDefinition;
171     }
172
173     /**
174      * This method saves the state for this component. It relies on the
175      * super class to save its own sate, this method will invoke
176      * super.saveState().
177      *
178      * @param context The FacesContext
179      *
180      * @return The serialized State
181      */

182     public Object JavaDoc saveState(FacesContext context) {
183     Object JavaDoc[] values = new Object JavaDoc[2];
184     values[0] = super.saveState(context);
185     values[1] = _ldmKey;
186     return values;
187     }
188
189     /**
190      * This method restores the state for this component. It will invoke the
191      * super class to restore its state.
192      *
193      * @param context The FacesContext
194      * @param state The serialized State
195      *
196      */

197     public void restoreState(FacesContext context, Object JavaDoc state) {
198     Object JavaDoc[] values = (Object JavaDoc[]) state;
199     super.restoreState(context, values[0]);
200     _ldmKey = (java.lang.String JavaDoc) values[1];
201     }
202
203     /**
204      * This method returns the LayoutDefinitionKey for this component.
205      *
206      * @return key The key to use in the LayoutDefinitionManager
207      */

208     public String JavaDoc getLayoutDefinitionKey() {
209     return _ldmKey;
210     }
211
212
213     /**
214      * This method sets the LayoutDefinition key for this component.
215      *
216      * @param key The key to use in the LayoutDefinitionManager
217      */

218     public void setLayoutDefinitionKey(String JavaDoc key) {
219     _ldmKey = key;
220     }
221
222     /**
223      * This is the LayoutDefinition key for this component. This is
224      * typically set by the Tag. The Component may also provide a default
225      * by setting it in its constructor.
226      */

227     private String JavaDoc _ldmKey = null;
228
229
230     /**
231      * This is a cached reference to the LayoutDefinition used by this
232      * UIComponent.
233      */

234     private transient LayoutDefinition _layoutDefinition = null;
235 }
236
Popular Tags