1 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 ; 10 11 12 15 16 17 18 public class ColorRenderer extends BaseRenderer { 19 20 21 23 29 30 public void decode(FacesContext context, UIComponent component) { 31 32 if ((context == null) || (component == null)) { 33 throw new NullPointerException (); 34 } 35 } 36 37 43 public void encodeBegin(FacesContext context, UIComponent component) 44 throws IOException { 45 46 if ((context == null) || (component == null)) { 47 throw new NullPointerException (); 48 } 49 } 50 51 57 public void encodeChildren(FacesContext context, UIComponent component) 58 throws IOException { 59 60 if ((context == null) || (component == null)) { 61 throw new NullPointerException (); 62 } 63 64 } 65 66 72 public void encodeEnd(FacesContext context, UIComponent component) 73 throws IOException { 74 75 if ((context == null) || (component == null)) { 76 throw new NullPointerException (); 77 } 78 79 UIOutput script = (UIOutput) component; 80 UIInput currentColor = (UIInput) component.findComponent("color"); 81 ResponseWriter writer = context.getResponseWriter(); 82 83 StringBuffer sb = null; 84 System.out.println("currentColor =" + currentColor); 85 if (currentColor == null) return; 86 sb = new StringBuffer ("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 |