KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > javaee > blueprints > components > ui > example > CompARenderer


1 /*
2 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at:
3 * http://developer.sun.com/berkeley_license.html
4 * $Id: CompARenderer.java,v 1.3 2006/06/26 16:29:05 basler Exp $
5 */

6 /*
7  * CompARenderer.java
8  *
9  * Created on April 28, 2006, 7:22 AM
10  *
11  * To change this template, choose Tools | Template Manager
12  * and open the template in the editor.
13  */

14
15 package com.sun.javaee.blueprints.components.ui.example;
16
17 import java.io.IOException JavaDoc;
18 import java.net.URL JavaDoc;
19 import java.util.Map JavaDoc;
20 import javax.faces.component.UIComponent;
21 import javax.faces.component.UIOutput;
22 import javax.faces.context.FacesContext;
23 import javax.faces.render.Renderer;
24 import javax.faces.context.ResponseWriter;
25 import javax.el.ValueExpression;
26
27 /**
28  *
29  * @author basler
30  */

31 public class CompARenderer extends Renderer {
32     private static final boolean bDebug=false;
33     private static final String JavaDoc RENDERED_SCRIPT_KEY = "JSF_EXAMPLE_COMPA";
34     private static final String JavaDoc COMPA_SCRIPT_RESOURCE="/compA.js";
35     private static final String JavaDoc COMPA_CSS_RESOURCE="/compA.css";
36     
37     
38     /**
39      * <p>Encode the end of the RichTextEditor tag with the instance specific javascript and html/p>
40      *
41      * @param context <code>FacesContext</code>for the current request
42      * @param component <code>UIComponent</code> to be decoded
43      */

44     public void encodeEnd(FacesContext context, UIComponent component) throws IOException JavaDoc {
45         
46         if(bDebug) System.out.println("\n *** START encodeEnd");
47         if ((context == null) || (component == null)) {
48             throw new NullPointerException JavaDoc();
49         }
50         
51         UIOutput outComp = (UIOutput)component;
52         ResponseWriter writer = context.getResponseWriter();
53
54         // set javascript main script file just once
55
renderScriptOnce(writer, component, context);
56         String JavaDoc clientId=component.getClientId(context);
57
58         // write out markup for the popup
59
writer.startElement("div", component);
60         writer.writeAttribute("id", clientId, "id");
61         
62         String JavaDoc styleClass=(String JavaDoc)getAttributeValue(context, component, "styleClass");
63         if (styleClass != null) {
64             // add custom class if necessary
65
writer.writeAttribute("class", "bpui_compA_popup " + styleClass, "styleClass");
66         } else {
67             // put in default class
68
writer.writeAttribute("class", "bpui_compA_popup", "styleClass");
69         }
70         String JavaDoc style=(String JavaDoc)getAttributeValue(context, component, "style");
71         if (style != null) {
72             writer.writeAttribute("style", style, "style");
73         }
74         
75
76         // Some say that the markup should be placed in the renderer for the component and all modifications should be handled only through
77
// as CSS. Some also say that the startElement and writeAttribute methods should be used when possible. I found it much too tedious.
78
// So for simplicity I wrote out the markup using the write method. I preferr to use templates as was done in CompBRenderer, but since
79
// these examples are supposed to show the different approaches, I hard coded the markup.
80
// ****
81
// start inside of popup display
82
writer.write("<div class=\"bpui_compA_popTop\">");
83         writer.write(" <div class=\"bpui_compA_cornerTL\"><div class=\"bpui_compA_cornerTR\">");
84         writer.write(" <center><font color=\"white\"><b><span id=\"" + clientId + "_title\">title</span></b></font></center>");
85         writer.write(" </div></div>");
86         writer.write("</div>");
87         writer.write("<div class=\"bpui_compA_popMid\">");
88         writer.write(" <table border=\"0\" style=\"width: 100%\">");
89         writer.write(" <tr>");
90         writer.write(" <td>");
91         writer.write(" <table border=\"0\" bgcolor=\"#ffffff\" cellpadding=\"5\" cellspacing=\"5\">");
92         writer.write(" <tr>");
93         writer.write(" <td><span id=\"" + clientId + "_message\">Value</span></td>");
94         writer.write(" </tr>");
95         writer.write(" </table>");
96         writer.write(" </td>");
97         writer.write(" </tr>");
98         writer.write(" </table>");
99         writer.write("</div>");
100         writer.write("<div class=\"bpui_compA_popBot\">");
101         writer.write(" <div class=\"bpui_compA_cornerBL\"><div class=\"bpui_compA_cornerBR\">");
102         writer.write(" </div></div>");
103         writer.write("</div>");
104         // end inside of popup display
105
// ****
106

107         // get properties from UIOutput that were set by the specific taghandler
108
writer.endElement("div");
109         
110         // get url of AJAX service, add context root so page position doesn't matter
111
String JavaDoc url=(String JavaDoc)getAttributeValue(context, component, "url");
112         if (url != null) {
113             url=context.getExternalContext().getRequestContextPath() + "/" + url;
114         }
115
116         writer.write("\n");
117         writer.startElement("script", component);
118         writer.writeAttribute("type", "text/javascript", null);
119         writer.write("\n");
120         writer.write("bpui.compA['" + clientId + "']=new bpui.compA.createPopup('"+ clientId +"', '"+ url +"');\n");
121         writer.endElement("script");
122         writer.write("\n");
123
124         if(bDebug) System.out.println("\n *** END encodeEnd");
125     }
126     
127     
128     /** Render the &lt;script&gt; tag which contains supporting JavaScript
129      * for this text field. This is a common method among the AJAX components from the other samples.
130      */

131     private void renderScriptOnce(ResponseWriter writer, UIComponent component, FacesContext context)
132     throws IOException JavaDoc {
133         
134         // Store attribute in request map when we've rendered the script such
135
// that we only do this once per page
136
Map JavaDoc requestMap=context.getExternalContext().getRequestMap();
137         Boolean JavaDoc scriptRendered=(Boolean JavaDoc)requestMap.get(RENDERED_SCRIPT_KEY);
138         
139         if (scriptRendered != null && scriptRendered.equals(Boolean.TRUE)) {
140             return;
141         }
142         
143         requestMap.put(RENDERED_SCRIPT_KEY, Boolean.TRUE);
144         String JavaDoc contextRoot=context.getExternalContext().getRequestContextPath();
145         
146         writer.startElement("script", component);
147         writer.writeAttribute("type", "text/javascript", null);
148         writer.writeAttribute("src", contextRoot + COMPA_SCRIPT_RESOURCE, null);
149         writer.endElement("script");
150         writer.write("\n");
151
152         writer.startElement("link", component);
153         writer.writeAttribute("type", "text/css", null);
154         writer.writeAttribute("rel", "stylesheet", null);
155         writer.writeAttribute("href", contextRoot + COMPA_CSS_RESOURCE, null);
156         writer.endElement("link");
157         writer.write("\n");
158     }
159     
160     public static Object JavaDoc getAttributeValue(FacesContext context, UIComponent component, String JavaDoc key) {
161         Object JavaDoc value=null;
162         try {
163             // check value expression
164
ValueExpression valuexx=component.getValueExpression(key);
165             if(valuexx != null) {
166                 value=valuexx.getValue(context.getELContext());
167             } else {
168                 value=component.getAttributes().get(key);
169             }
170         } catch(Exception JavaDoc ee) {
171             ee.printStackTrace();
172         }
173         return value;
174     }
175     
176 }
177
Popular Tags