KickJava   Java API By Example, From Geeks To Geeks.

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


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: CompBRenderer.java,v 1.3 2006/06/26 16:29:05 basler Exp $
5 */

6 /*
7  * CompBRenderer.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.util.Map JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.io.BufferedReader JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.net.URLConnection JavaDoc;
27
28 import javax.faces.component.UIComponent;
29 import javax.faces.component.UIOutput;
30 import javax.faces.context.FacesContext;
31 import javax.faces.render.Renderer;
32 import javax.faces.context.ResponseWriter;
33 import javax.faces.context.ResponseStream;
34 import javax.faces.component.UIViewRoot;
35 import javax.faces.component.html.HtmlInputText;
36 import javax.el.ValueExpression;
37
38 /**
39  *
40  * @author basler
41  */

42 public class CompBRenderer extends Renderer {
43     private static final boolean bDebug=false;
44     private static final String JavaDoc RENDERED_SCRIPT_KEY = "JSF_EXAMPLE_COMPB";
45     private static final String JavaDoc COMPB_SCRIPT_RESOURCE="/jsf-example/compB/compB.js";
46     private static final String JavaDoc COMPB_CSS_RESOURCE="/jsf-example/compB/compB.css";
47     private static final String JavaDoc COMPB_TEMPLATE_RESOURCE="/META-INF/jsf-example/compB/compB_template.txt";
48     
49     
50     /**
51      * <p>Encode the end of the RichTextEditor tag with the instance specific javascript and html/p>
52      *
53      * @param context <code>FacesContext</code>for the current request
54      * @param component <code>UIComponent</code> to be decoded
55      */

56     public void encodeEnd(FacesContext context, UIComponent component) throws IOException JavaDoc {
57         if(bDebug) System.out.println("\n *** START encodeEnd");
58         if ((context == null) || (component == null)) {
59             throw new NullPointerException JavaDoc();
60         }
61         
62         UIOutput outComp = (UIOutput)component;
63         ResponseWriter writer = context.getResponseWriter();
64
65         // set javascript main script file just once
66
renderScriptOnce(writer, component, context);
67         String JavaDoc clientId=component.getClientId(context);
68         
69         // write out the popup markup that is stored in a template
70
URL JavaDoc urlxx=CompBRenderer.class.getResource(COMPB_TEMPLATE_RESOURCE);
71         String JavaDoc sxTemplate=readInFragmentAsString(urlxx);
72         
73         writer.startElement("div", component);
74         writer.writeAttribute("id", clientId, "id");
75         
76         String JavaDoc styleClass=(String JavaDoc)getAttributeValue(context, component, "styleClass");
77         if (styleClass != null) {
78             // add custom class if necessary
79
writer.writeAttribute("class", "bpui_compB_popup " + styleClass, "styleClass");
80         } else {
81             // put in default class
82
writer.writeAttribute("class", "bpui_compB_popup", "styleClass");
83         }
84         String JavaDoc style=(String JavaDoc)getAttributeValue(context, component, "style");
85         if (style != null) {
86             writer.writeAttribute("style", style, "style");
87         }
88         
89         // populate hashmap with tokens and replacement values
90
HashMap JavaDoc hmSub=new HashMap JavaDoc();
91         hmSub.put("%%%ID%%%", clientId);
92         
93         // parse page and put in substitutions, then write to output
94
writer.write(parseString(sxTemplate, hmSub, false));
95         
96         // get properties from UIOutput that were set by the specific taghandler
97
writer.endElement("div");
98         
99         // get url of AJAX service, add context root so page position doesn't matter
100
String JavaDoc url=(String JavaDoc)getAttributeValue(context, component, "url");
101         if (url != null) {
102             url=context.getExternalContext().getRequestContextPath() + "/" + url;
103         }
104         
105         writer.write("\n");
106         writer.startElement("script", component);
107         writer.writeAttribute("type", "text/javascript", null);
108         writer.write("\n");
109         writer.write("bpui.compB['" + clientId + "']=new bpui.compB.createPopup('"+ clientId +"', '"+ url +"');\n");
110         writer.endElement("script");
111         writer.write("\n");
112
113         if(bDebug) System.out.println("\n *** END encodeEnd");
114     }
115     
116     
117     /** Render the &lt;script&gt; tag which contains supporting JavaScript
118      * for this text field. This is a common method among the AJAX components from the other samples.
119      */

120     private void renderScriptOnce(ResponseWriter writer, UIComponent component, FacesContext context)
121     throws IOException JavaDoc {
122         
123         // Store attribute in request map when we've rendered the script such
124
// that we only do this once per page
125
Map JavaDoc requestMap=context.getExternalContext().getRequestMap();
126         Boolean JavaDoc scriptRendered=(Boolean JavaDoc)requestMap.get(RENDERED_SCRIPT_KEY);
127         
128         if (scriptRendered != null && scriptRendered.equals(Boolean.TRUE)) {
129             return;
130         }
131         
132         requestMap.put(RENDERED_SCRIPT_KEY, Boolean.TRUE);
133         String JavaDoc contextRoot=context.getExternalContext().getRequestContextPath();
134         
135         writer.startElement("script", component);
136         writer.writeAttribute("type", "text/javascript", null);
137         writer.writeAttribute("src", contextRoot + "/faces" + COMPB_SCRIPT_RESOURCE, null);
138         writer.endElement("script");
139         writer.write("\n");
140
141         writer.startElement("link", component);
142         writer.writeAttribute("type", "text/css", null);
143         writer.writeAttribute("rel", "stylesheet", null);
144         writer.writeAttribute("href", contextRoot + "/faces" + COMPB_CSS_RESOURCE, null);
145         writer.endElement("link");
146         writer.write("\n");
147     }
148     
149     
150     
151     
152     public static Object JavaDoc getAttributeValue(FacesContext context, UIComponent component, String JavaDoc key) {
153         Object JavaDoc value=null;
154         try {
155             // check value expression
156
ValueExpression valuexx=component.getValueExpression(key);
157             if(valuexx != null) {
158                 value=valuexx.getValue(context.getELContext());
159             } else {
160                 value=component.getAttributes().get(key);
161             }
162         } catch(Exception JavaDoc ee) {
163             ee.printStackTrace();
164         }
165         return value;
166     }
167     
168     private static InputStream JavaDoc openStream(URL JavaDoc sxURL) throws IOException JavaDoc {
169         // set cache to false, so the ./lib/bp-popup-balloon.jar file doesn't get locked on redeploy or undeploy on windows.
170
InputStream JavaDoc isx=null;
171         if(sxURL!= null) {
172             URLConnection JavaDoc urlConn = sxURL.openConnection();
173             urlConn.setUseCaches(false);
174             isx=urlConn.getInputStream();
175         }
176         return isx;
177     }
178     
179    /**
180     * Performs Substitution on passed in strings. This method could also use a cache to loop up strings previously used
181     *
182     * @param sxString String to parse
183     * @param hmSub A Hashmap of of keys/variables to change
184     * @param bLookup Whether to see if the value is to be looked up in the cache. If true the sxString is used as a key
185     */

186     public static String JavaDoc parseString(String JavaDoc sxString, HashMap JavaDoc hmSub, boolean bLookup) {
187
188         int iPos=0;
189         String JavaDoc sxStringx="";
190
191         if(bLookup) {
192             // pull from cache
193
//sxStringx=(String)getPages().get(sxString);
194
} else {
195             sxStringx=sxString;
196         }
197
198         if (sxStringx != null) {
199             // populate other values that are page specific
200
Iterator JavaDoc iter=hmSub.keySet().iterator();
201             String JavaDoc sxKey, sxValue;
202             while(iter.hasNext()) {
203
204                 // get key and vaule for substitution
205
sxKey=(String JavaDoc)iter.next();
206                 sxValue=(String JavaDoc)hmSub.get(sxKey);
207                 sxStringx=sxStringx.replaceAll(sxKey, sxValue);
208             }
209         }
210         return sxStringx;
211     }
212     
213     
214     /*
215      * Reads in a file in for processing, using rendering class location
216      *
217      * @param sxFile The file name to read in
218      * @return File in string format
219      */

220     public static String JavaDoc readInFragmentAsString(URL JavaDoc sxURL) throws IOException JavaDoc {
221         BufferedReader JavaDoc bfReader=null;
222         StringBuffer JavaDoc sxOut=new StringBuffer JavaDoc();
223
224         try {
225             bfReader=new BufferedReader JavaDoc(new InputStreamReader JavaDoc(openStream(sxURL)));
226             
227             int byteCnt=0;
228             char[] buffer=new char[4096];
229             while ((byteCnt=bfReader.read(buffer)) != -1) {
230                 if (byteCnt > 0) {
231                    sxOut.append(buffer, 0, byteCnt);
232                 }
233             }
234         } catch (IOException JavaDoc e) {
235             e.printStackTrace();
236             throw e;
237         } finally {
238             if(bfReader != null) {
239                 try {
240                     bfReader.close();
241                 } catch (Exception JavaDoc ee){}
242             }
243         }
244         
245         return sxOut.toString();
246     }
247 }
248
Popular Tags