KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > jsfext > layout > descriptor > LayoutDefinition


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.layout.descriptor;
24
25 import com.sun.enterprise.tools.jsfext.component.TemplateComponent;
26 import com.sun.enterprise.tools.jsfext.event.DecodeEvent;
27
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33
34 import javax.faces.context.FacesContext;
35 import javax.faces.component.UIComponent;
36
37
38 /**
39  * <p> This represents the top-level {@link LayoutElement}, it is the
40  * container for every other {@link LayoutElement}. By itself, it has no
41  * functionality. Its purpose in life is to group all top-level child
42  * {@link LayoutElement}s. LayoutDefintion objects can be registered
43  * with the
44  * {@link com.sun.enterprise.tools.jsfext.layout.LayoutDefinitionManager}.
45  * This class does provide a useful method
46  * {@link #getChildLayoutElementById(FacesContext, String, LayoutElement, UIComponent)}
47  * which will search recursively for the given child id.</p>
48  *
49  * @author Ken Paulsen (ken.paulsen@sun.com)
50  */

51 public class LayoutDefinition extends LayoutElementBase {
52
53     /**
54      * <p> Constructor.</p>
55      */

56     public LayoutDefinition(String JavaDoc id) {
57     // LayoutDefinition objects do not have a parent or an id
58
super(null, id);
59
60     // Set the default StaticText ComponentType
61
addComponentType(new ComponentType(
62         STATIC_TEXT_TYPE, STATIC_TEXT_FACTORY_CLASS_NAME));
63     }
64
65
66     /**
67      * <p> Retrieve a ComponentType by typeID.</p>
68      *
69      * @param typeID The key used to retrieve the ComponentType
70      *
71      * @return The requested ComponentType or null
72      */

73     public ComponentType getComponentType(String JavaDoc typeID) {
74     return (ComponentType) _types.get(typeID);
75     }
76
77
78     /**
79      * <p> This will add the given <code>ComponentType</code> to the map of
80      * registered <code>ComponentType</code>'s. It will use the
81      * <code>ComponentType</code> ID as the key to the <code>Map</code>.
82      * This means that if a <code>ComponentType</code> with the same ID had
83      * previously been registered, it will be replaced with the
84      * <code>ComponentType</code> passed in.</p>
85      *
86      * @param type The <code>ComponentType</code>.
87      */

88     public void addComponentType(ComponentType type) {
89     _types.put(type.getId(), type);
90     }
91
92     /**
93      * <p> This method adds a {@link Resource}. These resources should be
94      * added to the request scope when this component is used. This is
95      * mainly used for <code>ResourceBundle<code>s (at this time).</p>
96      *
97      * @param res The {@link Resource} to associate with the
98      * <code>LayoutDefinition</code>.
99      */

100     public void addResource(Resource res) {
101     _resources.add(res);
102     }
103
104     /**
105      * <p> This method returns a List of {@link Resource} objects.</p>
106      *
107      * @return This method returns a List of {@link Resource} objects.
108      */

109     public List JavaDoc getResources() {
110     return _resources;
111     }
112
113     /**
114      * <p> This method searches for the requested {@link LayoutComponent} by
115      * id.</p>
116      *
117      * @param context <code>FacesContext</code>
118      * @param id id to look for
119      * @param parent Search starts from this {@link LayoutElement}
120      * @param parentComponent Parent <code>UIComponent</code>
121      *
122      * @return The matching {@link LayoutElement} if found, null otherwise.
123      */

124     public static LayoutElement getChildLayoutElementById(FacesContext context, String JavaDoc id, LayoutElement parent, UIComponent parentComponent) {
125     // NOTE: I may want to optimize this by putting all values in a Map so
126
// NOTE: that I don't have to do this search.
127

128     // Make sure this isn't what we're looking for
129
if (parent.getId(context, parentComponent).equals(id)) {
130         return parent;
131     }
132
133     // Not 'this' so lets check the children
134
Iterator JavaDoc it = parent.getChildLayoutElements().iterator();
135     LayoutElement elt = null;
136     while (it.hasNext()) {
137         elt = getChildLayoutElementById(
138             context, id, (LayoutElement) it.next(), parentComponent);
139         if (elt != null) {
140         // Found it!
141
return elt;
142         }
143     }
144
145     // Not found...
146
return null;
147     }
148
149
150     /**
151      * <p> Retrieve an attribute by key.</p>
152      *
153      * @param key The key used to retrieve the attribute
154      *
155      * @return The requested attribute or null.
156      */

157     public Object JavaDoc getAttribute(String JavaDoc key) {
158     return _attributes.get(key);
159     }
160
161
162     /**
163      * <p> Associate the given key with the given Object as an attribute.</p>
164      *
165      * @param key The key associated with the given object (if this key
166      * is already in use, it will replace the previously set attribute
167      * object).
168      *
169      * @param value The Object to store.
170      */

171     public void setAttribute(String JavaDoc key, Object JavaDoc value) {
172     _attributes.put(key, value);
173     }
174
175
176     /**
177      * <p> The <code>LayoutDefinition</code> does not encode anything for
178      * itself, this method simply returns true.</p>
179      *
180      * @param context The <code>FacesContext</code>.
181      * @param component The <code>UIComponent</code>.
182      *
183      * @return true.
184      */

185     protected boolean encodeThis(FacesContext context, UIComponent component) {
186     return true;
187     }
188
189     /**
190      * <p> This method retrieves the Handlers for the requested type. But
191      * also includes any handlers that are associated with the instance
192      * (i.e. the UIComponent).</p>
193      *
194      * @param type The type of <code>Handler</code>s to retrieve.
195      * @param event The associated <code>UIComponent</code> (or null).
196      *
197      * @return A List of Handlers.
198      */

199     public List JavaDoc getHandlers(String JavaDoc type, UIComponent comp) {
200     // 1st get list of handlers for definition of this LayoutElement
201
List JavaDoc handlers = null;
202
203     // Now check to see if there are any on the UIComponent (NOTE: We do
204
// not pull off handlers if the parent is a TemplateComponent b/c it
205
// is the responsibility of the parent class to invoke handlers via
206
// its LayoutComponent. If we do it here, it will happen 2x.)
207
if ((comp != null)
208         && (!(comp.getParent() instanceof TemplateComponent))) {
209         List JavaDoc instHandlers = (List JavaDoc) comp.getAttributes().get(type);
210         if ((instHandlers != null) && (instHandlers.size() > 0)) {
211         // NOTE: Copy b/c this is <i>instance</i> + static
212
// Add the UIComponent instance handlers
213
handlers = new ArrayList JavaDoc(instHandlers);
214
215         List JavaDoc defHandlers = getHandlers(type);
216         if (defHandlers != null) {
217             // Add the LayoutElement "definition" handlers, if any
218
handlers.addAll(getHandlers(type));
219         }
220         }
221     }
222     if (handlers == null) {
223         handlers = getHandlers(type);
224     }
225
226     return handlers;
227     }
228
229     /**
230      * <p> This decode method invokes any registered {@link #DECODE}
231      * handlers.</p>
232      *
233      * @param context The FacesContext.
234      * @param component The <code>UIComponent</code>.
235      */

236     public void decode(FacesContext context, UIComponent component) {
237     // Invoke "decode" handlers
238
dispatchHandlers(context, DECODE, new DecodeEvent(component));
239     }
240
241
242     /**
243      * <p> This is the "type" for handlers to be invoked to handle "decode"
244      * functionality for this element.</p>
245      */

246      public static final String JavaDoc DECODE = "decode";
247
248     /**
249      * <p> This is a hard-coded LayoutComponent type. By default it
250      * corresponds to
251      * {@link com.sun.enterprise.tools.jsfext.component.factory.basic.StaticTextFactory}.</p>
252      */

253     public static final String JavaDoc STATIC_TEXT_TYPE =
254     "staticText";
255
256     /**
257      * <p> This is the full classname of the default StaticTextFactory.</p>
258      */

259     public static final String JavaDoc STATIC_TEXT_FACTORY_CLASS_NAME =
260     "com.sun.enterprise.tools.jsfext.component.factory.basic.StaticTextFactory";
261
262     /**
263      * <p> This is a list of Resource objects. These resources are to be
264      * added to the Request scope when this <code>LayoutDefinition</code>
265      * is used.</p>
266      */

267     private List JavaDoc _resources = new ArrayList JavaDoc();
268
269     /**
270      * <p> Map of types. This information is needed to instantiate
271      * UIComponents.</p>
272      */

273     private Map JavaDoc _types = new HashMap JavaDoc();
274
275     /**
276      * <p> Map of attributes. Attributes can be used to store extra
277      * information about the <code>LayoutDefinition</code>.</p>
278      */

279     private Map JavaDoc _attributes = new HashMap JavaDoc();
280 }
281
Popular Tags