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;
042:
043: import java.io.IOException;
044: import java.util.Map;
045: import javax.faces.component.UIComponent;
046: import javax.faces.context.FacesContext;
047: import javax.faces.context.ResponseWriter;
048: import javax.faces.FacesException;
049: import com.sun.rave.web.ui.component.Body;
050: import com.sun.rave.web.ui.util.RenderingUtilities;
051: import com.sun.rave.web.ui.util.MessageUtil;
052: import javax.servlet.http.HttpServletRequest;
053:
054: /**
055: * <p>Renderer for a {@link Body} component.</p>
056: */
057: public class BodyRenderer extends AbstractRenderer {
058:
059: private static final boolean DEBUG = false;
060: /**
061: * <p>The set of String pass-through attributes to be rendered.</p>
062: */
063: private static final String[] stringAttributes = { "onClick",
064: "onDblClick", "onMouseDown", "onMouseUp", "onMouseOver",
065: "onMouseMove", "onMouseOut", "onKeyPress", "onKeyDown",
066: "onKeyUp", "onFocus", "onBlur" }; //NOI18N
067: /**
068: * <p>The set of integer pass-through attributes to be rendered.</p>
069: */
070: private static final String[] integerAttributes = { "tabIndex" }; //NOI18N
071:
072: /**
073: * <p>Render the appropriate element start, depending on whether the
074: * <code>for</code> property is set or not.</p>
075: *
076: * @param context <code>FacesContext</code> for the current request
077: * @param component component to render.
078: * @param writer <code>ResponseWriter</code> to which the element
079: * start should be rendered
080: *
081: * @exception IOException if an input/output error occurs
082: */
083: protected void renderStart(FacesContext context,
084: UIComponent component, ResponseWriter writer)
085: throws IOException {
086:
087: // Start the appropriate element
088: if (RenderingUtilities.isPortlet(context)) {
089: return;
090: }
091:
092: if (!(component instanceof Body)) {
093: Object[] params = { component.toString(),
094: this .getClass().getName(), Body.class.getName() };
095: String message = MessageUtil.getMessage(
096: "com.sun.rave.web.ui.resources.LogMessages",
097: "Renderer.component", params); //NOI18N
098: throw new FacesException(message);
099: }
100:
101: writer.startElement("body", component); //NOI18N
102: }
103:
104: /**
105: * <p>Render the appropriate element attributes,
106: *
107: * @param context <code>FacesContext</code> for the current request
108: * @param component component to be rendered
109: * submitted value is to be stored
110: * @param writer <code>ResponseWriter</code> to which the element
111: * start should be rendered
112: *
113: * @exception IOException if an input/output error occurs
114: */
115: protected void renderAttributes(FacesContext context,
116: UIComponent component, ResponseWriter writer)
117: throws IOException {
118:
119: if (RenderingUtilities.isPortlet(context)) {
120: return;
121: }
122:
123: Body body = (Body) component;
124:
125: addCoreAttributes(context, component, writer, null);
126: addStringAttributes(context, component, writer,
127: stringAttributes);
128:
129: // onload is a special case;
130: String onload = body.getOnLoad();
131: StringBuffer sb = new StringBuffer(256);
132: if (onload != null) {
133: sb.append(onload);
134: }
135: if (body.getFocusID(context) != null
136: && !RenderingUtilities.isPortlet(context)) {
137: if (onload != null) {
138: sb.append("; ");
139: }
140: sb.append("return ");
141: sb.append(body.getJavaScriptObjectName(context));
142: sb.append(".setInitialFocus();");
143: }
144: if (sb.length() > 0) {
145: writer.writeAttribute("onload", sb.toString(), null); //NO18N
146: }
147: // <RAVE>
148: String imageUrl = body.getImageURL();
149: if (imageUrl != null && imageUrl.length() > 0) {
150: String resourceUrl = context.getApplication()
151: .getViewHandler().getResourceURL(context, imageUrl);
152: writer.writeAttribute("background", resourceUrl, null); //NOI18N
153: }
154: // </RAVE>
155: // unload is a special case;
156: String onUnload = body.getOnUnload();
157: sb.setLength(0);
158: if (onUnload != null) {
159: sb.append(onUnload);
160: }
161: if (body.getFocusID(context) != null
162: && !RenderingUtilities.isPortlet(context)) {
163: if (onUnload != null) {
164: sb.append("; ");
165: }
166: sb.append("return ");
167: sb.append(body.getJavaScriptObjectName(context));
168: sb.append(".setScrollPosition();");
169: }
170: if (sb.length() > 0) {
171: writer.writeAttribute("onunload", sb.toString(), null); //NO18N
172: }
173: addIntegerAttributes(context, component, writer,
174: integerAttributes);
175: writer.write("\n"); //NOI18N
176: }
177:
178: /**
179: * <p>Render the appropriate element end, depending on whether the
180: * <code>for</code> property is set or not.</p>
181: *
182: * @param context <code>FacesContext</code> for the current request
183: * @param component component to be rendered
184: * @param writer <code>ResponseWriter</code> to which the element
185: * start should be rendered
186: *
187: * @exception IOException if an input/output error occurs
188: */
189: protected void renderEnd(FacesContext context,
190: UIComponent component, ResponseWriter writer)
191: throws IOException {
192:
193: if (!RenderingUtilities.isPortlet(context)) {
194: Body body = (Body) component;
195: if (body.getFocusID(context) != null) {
196:
197: StringBuffer jsBuffer = new StringBuffer(128);
198: jsBuffer
199: .append("\n<script type=\"text/javascript\">\nvar ");
200: jsBuffer.append(body.getJavaScriptObjectName(context));
201:
202: jsBuffer.append(" = new Body('");
203: jsBuffer.append(body.getFocusID(context));
204: jsBuffer.append("');");
205:
206: jsBuffer.append("\n</script>\n");
207: writer.write(jsBuffer.toString());
208: }
209: writer.endElement("body"); //NOI18N
210: writer.write("\n"); //NOI18N
211: }
212: }
213: }
|