KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > jsfext > component > factory > ComponentFactoryBase


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.factory;
24
25 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutComponent;
26 import com.sun.enterprise.tools.jsfext.util.LogUtil;
27
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.faces.component.UIComponent;
32 import javax.faces.context.FacesContext;
33 import javax.faces.el.ValueBinding;
34 import javax.faces.webapp.UIComponentTag;
35
36
37 /**
38  * <p> This abstract class provides common functionality for UIComponent
39  * factories.</p>
40  *
41  * @author Ken Paulsen (ken.paulsen@sun.com)
42  */

43 public abstract class ComponentFactoryBase implements ComponentFactory {
44
45     /**
46      * <p> This method iterates through the Map of options. It looks at each
47      * one, if it contians an EL expression, it sets a value binding.
48      * Otherwise, it calls setAttribute() on the component (which in turn
49      * will invoke the bean setter if there is one).</p>
50      *
51      * <p> This method also interates through the child
52      * <code>LayoutElement</code>s of the given {@link LayoutComponent}
53      * descriptor and adds Facets or children as appropriate.</p>
54      *
55      * @param context The <code>FacesContext</code>
56      * @param desc The {@link LayoutComponent} descriptor associated with
57      * the requested <code>UIComponent</code>.
58      * @param comp The <code>UIComponent</code>
59      */

60     protected void setOptions(FacesContext context, LayoutComponent desc, UIComponent comp) {
61     // First set the id if supplied, treated special b/c the component
62
// used for ${} expressions is the parent and this must be set first
63
// so other ${} expressions can use $this{id} and $this{clientId}.
64
String JavaDoc compId = (String JavaDoc) desc.getId(context, comp.getParent());
65     if ((compId != null) && (!compId.equals(""))) {
66         comp.setId(compId);
67     }
68
69     // Loop through all the options and set the values
70
Map JavaDoc attributes = comp.getAttributes();
71 // FIXME: Figure a way to skip options that should not be set on the Component
72
Iterator JavaDoc it = desc.getOptions().keySet().iterator();
73     Object JavaDoc value = null;
74     String JavaDoc strVal = null;
75     String JavaDoc key = null;
76     while (it.hasNext()) {
77         // Get next property
78
key = (String JavaDoc) it.next();
79         value = desc.getEvaluatedOption(context, key, comp);
80
81         // Next check to see if the value contains a JSF ValueBinding
82
strVal = "" + value;
83         if (UIComponentTag.isValueReference(strVal)) {
84         ValueBinding vb =
85             context.getApplication().createValueBinding(strVal);
86         comp.setValueBinding((String JavaDoc) key, vb);
87         } else {
88         // In JSF, you must directly modify the attribute Map
89
try {
90             attributes.put(key, value);
91         } catch (NullPointerException JavaDoc ex) {
92             // Setting null, assume they want to remove the value
93
attributes.remove(key);
94         }
95         }
96     }
97
98     // Set the events on the new component
99
storeInstanceHandlers(desc, comp);
100     }
101
102     /**
103      * <p> This method is responsible for interating over the "instance"
104      * handlers and applying them to the UIComponent. An "instance"
105      * handler is one that is defined <b>outside a renderer</b>, or <b>a
106      * nested component within a renderer</b>. In other words, a handler
107      * that would not get fired by the TemplateRenderer. By passing this
108      * in via the UIComponent, code that is aware of events (see
109      * {@link com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutElementBase})
110      * may find these events and fire them. These may vary per "instance"
111      * of a particular component (i.e. <code>TreeNode</code>) unlike the
112      * handlers defined in a TemplateRender's XML (which are shared and
113      * therefor should not change dynamically).</p>
114      *
115      * <p> This method is invoked from setOptions(), however, if setOptions
116      * is not used in by a factory, this method may be invoked directly.
117      * Calling this method multiple times will not cause any harm,
118      * besides making an extra unnecessary call.</p>
119      *
120      * @param desc The descriptor potentially containing handlers to copy.
121      * @param comp The UIComponent instance to store the handlers.
122      */

123     protected void storeInstanceHandlers(LayoutComponent desc, UIComponent comp) {
124     if (!desc.isNested()) {
125         // This is not a nested LayoutComponent, it does should not store
126
// instance handlers
127
return;
128     }
129
130     // Iterate over the instance handlers
131
Iterator JavaDoc it = desc.getHandlersByTypeMap().keySet().iterator();
132     if (it.hasNext()) {
133         String JavaDoc eventType = null;
134         Map JavaDoc compAttrs = comp.getAttributes();
135         while (it.hasNext()) {
136         // Assign instance handlers to attribute for retrieval later
137
// (NOTE: retrieval must be explicit, see LayoutElementBase)
138
eventType = (String JavaDoc) it.next();
139         if (eventType.equals(LayoutComponent.BEFORE_CREATE)) {
140             // This is handled directly, no need for instance handler
141
continue;
142         } else if (eventType.equals(LayoutComponent.AFTER_CREATE)) {
143             // This is handled directly, no need for instance handler
144
continue;
145         }
146         compAttrs.put(eventType, desc.getHandlers(eventType));
147         }
148     }
149     }
150
151     /**
152      * <p> This method associates the given child with the given parent. By
153      * using this method we centralize the code so that if we decide
154      * later to add it as a real child it can be done in one place.</p>
155      *
156      * @param context The <code>FacesContext</code>
157      * @param descriptor The {@link LayoutComponent} descriptor associated
158      * with the requested <code>UIComponent</code>.
159      * @param parent The parent <code>UIComponent</code>
160      * @param child The child <code>UIComponent</code>
161      */

162     protected void addChild(FacesContext context, LayoutComponent descriptor, UIComponent parent, UIComponent child) {
163     if (descriptor.isFacetChild()) {
164         String JavaDoc name = (String JavaDoc) descriptor.getEvaluatedOption(
165             context, LayoutComponent.FACET_NAME, child);
166         if (name != null) {
167         parent.getFacets().put(name, child);
168         } else {
169         // Warn the developer that they may have a problem
170
if (LogUtil.configEnabled()) {
171             LogUtil.config("Warning: no facet name was supplied for '"
172                 + descriptor.getId(context, child) + "'!");
173         }
174
175         // Set the parent
176
child.setParent(parent);
177         }
178     } else {
179         // Add this as an actual child
180
parent.getChildren().add(child);
181     }
182     }
183 }
184
Popular Tags