001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.web.ui.renderer.template;
042:
043: import com.sun.rave.web.ui.component.TemplateComponent;
044: import com.sun.rave.web.ui.component.util.descriptors.LayoutDefinition;
045: import com.sun.rave.web.ui.component.util.descriptors.LayoutElement;
046: import com.sun.rave.web.ui.component.util.descriptors.Resource;
047:
048: import java.io.IOException;
049: import java.util.Iterator;
050:
051: import javax.faces.context.FacesContext;
052: import javax.faces.component.UIComponent;
053: import javax.faces.render.Renderer;
054:
055: /**
056: * <p> This renderer is a generic "template-based" renderer. It uses a
057: * {@link LayoutElement} tree as its template and walks this tree. This
058: * renderer will actually delegate the encode functionality to the
059: * {@link LayoutDefinition} object, which is the top of the LayoutElement
060: * in the tree.</p>
061: *
062: * <p> This renderer also has the feature of registering {@link Resource}
063: * objects to the Request scope prior to rendering its output. This
064: * allows {@link Resource} objects such as ResourceBundles to be added
065: * to the Request scope for easy access.</p>
066: *
067: * @see LayoutDefinition
068: * @see LayoutElement
069: * @see Resource
070: *
071: * @author Ken Paulsen (ken.paulsen@sun.com)
072: */
073: public class TemplateRenderer extends Renderer {
074:
075: /**
076: * <p> This method returns true. This method indicates that this
077: * <code>Renderer</code> will assume resposibilty for rendering its
078: * own children.</p>
079: *
080: * @return true
081: *
082: * @see #encodeChildren(FacesContext, UIComponent)
083: */
084: public boolean getRendersChildren() {
085: return true;
086: }
087:
088: /**
089: * <p> This method initializes the Resources so they will be available for
090: * children. It then calls encodeBegin on the superclass.</p>
091: *
092: * @param context The FacesContext
093: * @param component The UIComponent, should be a
094: * {@link TemplateComponent}
095: */
096: public void encodeBegin(FacesContext context, UIComponent component)
097: throws IOException {
098: // Make sure we have a TemplateComponent
099: if (!(component instanceof TemplateComponent)) {
100: throw new IllegalArgumentException(
101: "TemplateRenderer requires that its UIComponent be an "
102: + "instance of TemplateComponent!");
103: }
104: TemplateComponent tempComp = (TemplateComponent) component;
105: LayoutDefinition def = tempComp.getLayoutDefinition(context);
106:
107: // First ensure that our Resources are available
108: Iterator it = def.getResources().iterator();
109: Resource resource = null;
110: while (it.hasNext()) {
111: resource = (Resource) it.next();
112: // Just calling getResource() puts it in the Request scope
113: resource.getFactory().getResource(context, resource);
114: }
115:
116: // Call the super class
117: super .encodeBegin(context, component);
118: }
119:
120: /**
121: * <p> This method prevents the super class's default functionality of
122: * rendering the child UIComponents. This <code>Renderer</code>
123: * implementation requires that the children be explicitly
124: * rendered. This method does nothing.</p>
125: *
126: * @param context The <code>FacesContext</code>
127: * @param component The <code>UIComponent</code>
128: */
129: public void encodeChildren(FacesContext context,
130: UIComponent component) {
131: // Do nothing...
132: }
133:
134: /**
135: * <p> This method performs the rendering for the TemplateRenderer. It
136: * expects that component be an instanceof {@link TemplateComponent}.
137: * It obtains the {@link LayoutDefinition} from the
138: * {@link TemplateComponent}, initializes the {@link Resource}
139: * objects defined by the {@link LayoutDefinition} (if any), and
140: * finally delegates the encoding to the
141: * {@link LayoutDefinition#encode(FacesContext, TemplateComponent)}
142: * method of the {@link LayoutDefinition}.</p>
143: *
144: * @param context The FacesContext object
145: * @param component The {@link TemplateComponent}
146: */
147: public void encodeEnd(FacesContext context, UIComponent component)
148: throws IOException {
149: // Sanity Check...
150: if (!component.isRendered()) {
151: return;
152: }
153:
154: // Get the LayoutDefinition and begin rendering
155: TemplateComponent tempComp = (TemplateComponent) component;
156: LayoutDefinition def = tempComp.getLayoutDefinition(context);
157:
158: // The following "encode" method does all the rendering
159: def.encode(context, (UIComponent) tempComp);
160: }
161:
162: /**
163: * <p> Decode any new state of the specified UIComponent from the request
164: * contained in the specified FacesContext, and store that state on
165: * the UIComponent.</p>
166: *
167: * <p> During decoding, events may be queued for later processing (by
168: * event listeners that have registered an interest), by calling
169: * <code>queueEvent()</code> on the associated UIComponent.</p>
170: *
171: * <p> This implementation of this method invokes the super class and
172: * then any handlers that have been registered to process decode
173: * functionality. The execution of these handlers is delegated to
174: * the LayoutDefinition.</p>
175: *
176: * @param context FacesContext for the request we are processing
177: * @param component UIComponent to be decoded.
178: *
179: * @exception NullPointerException if <code>context</code>
180: * or <code>component</code> is <code>null</code>
181: */
182: public void decode(FacesContext context, UIComponent component) {
183: // Call the super first
184: super .decode(context, component);
185:
186: // Call any decode handlers
187: TemplateComponent tempComp = (TemplateComponent) component;
188: LayoutDefinition def = tempComp.getLayoutDefinition(context);
189: def.decode(context, component);
190: }
191: }
|