KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > catalog > validator > ColorRenderer


1 /* Copyright 2004-2005 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: http://developer.sun.com/berkeley_license.html
2 $Id: ColorRenderer.java,v 1.3 2005/03/23 14:49:22 jenniferb Exp $ */

3
4 package com.sun.j2ee.blueprints.catalog.validator;
5
6 import javax.faces.component.*;
7 import javax.faces.context.FacesContext;
8 import javax.faces.context.ResponseWriter;
9 import java.io.IOException JavaDoc;
10
11
12 /**
13  * This class renders JavaScript
14  */

15
16
17
18 public class ColorRenderer extends BaseRenderer {
19
20
21     // -------------------------------------------------------- Renderer Methods
22

23     /**
24      * <p>No decoding is required.</p>
25      *
26      * @param context <code>FacesContext</code>for the current request
27      * @param component <code>UIComponent</code> to be decoded
28      */

29
30     public void decode(FacesContext context, UIComponent component) {
31
32         if ((context == null) || (component == null)) {
33             throw new NullPointerException JavaDoc();
34         }
35     }
36
37     /**
38      * <p>No begin encoding is required.</p>
39      *
40      * @param context <code>FacesContext</code>for the current request
41      * @param component <code>UIComponent</code> to be decoded
42      */

43     public void encodeBegin(FacesContext context, UIComponent component)
44         throws IOException JavaDoc {
45
46         if ((context == null) || (component == null)) {
47             throw new NullPointerException JavaDoc();
48         }
49     }
50
51     /**
52      * <p>No children encoding is required.</p>
53      *
54      * @param context <code>FacesContext</code>for the current request
55      * @param component <code>UIComponent</code> to be decoded
56      */

57     public void encodeChildren(FacesContext context, UIComponent component)
58         throws IOException JavaDoc {
59
60         if ((context == null) || (component == null)) {
61             throw new NullPointerException JavaDoc();
62         }
63
64     }
65
66     /**
67      * <p>Encodes this component.</p>
68      *
69      * @param context <code>FacesContext</code>for the current request
70      * @param component <code>UIComponent</code> to be decoded
71      */

72     public void encodeEnd(FacesContext context, UIComponent component)
73         throws IOException JavaDoc {
74
75         if ((context == null) || (component == null)) {
76             throw new NullPointerException JavaDoc();
77         }
78
79         UIOutput script = (UIOutput) component;
80         UIInput currentColor = (UIInput) component.findComponent("color");
81         ResponseWriter writer = context.getResponseWriter();
82
83         StringBuffer JavaDoc sb = null;
84         System.out.println("currentColor =" + currentColor);
85         if (currentColor == null) return;
86         sb = new StringBuffer JavaDoc("function client_validation() {\n");
87         sb.append("if (document.forms[0]['");
88         sb.append(currentColor.getClientId(context));
89         sb.append("'].value=='red' ) {").append("\n");
90         sb.append("return true;");
91         sb.append("\n} else if (document.forms[0]['");
92         sb.append(currentColor.getClientId(context));
93         sb.append("'].value=='green' ) {").append("\n");
94         sb.append("return true;");
95         sb.append("\n} else if (document.forms[0]['");
96         sb.append(currentColor.getClientId(context));
97         sb.append("'].value=='blue' ) {").append("\n");
98         sb.append("return true;");
99         sb.append("\n } else {\n");
100         sb.append("alert('Enter red, green, or blue for the color.');");
101         sb.append("\nreturn false;\n");
102     sb.append("} \n }");
103         sb.append("\n");
104     
105         if (writer != null) {
106             writer.startElement("script", script);
107             writer.writeAttribute("language", "JavaScript", "script");
108             writer.write(sb.toString());
109             writer.endElement("script");
110         }
111     }
112
113 }
114
115
Popular Tags