1 33 34 package com.icesoft.faces.renderkit.dom_html_basic; 35 36 import com.icesoft.faces.context.DOMContext; 37 import com.icesoft.faces.context.effects.JavascriptContext; 38 import com.icesoft.faces.context.effects.LocalEffectEncoder; 39 import org.w3c.dom.Element ; 40 41 import javax.faces.component.EditableValueHolder; 42 import javax.faces.component.UIComponent; 43 import javax.faces.context.FacesContext; 44 import javax.faces.convert.ConverterException; 45 import java.io.IOException ; 46 import java.util.HashSet ; 47 import java.util.Iterator ; 48 import java.util.Map ; 49 import java.util.Set ; 50 51 public class CheckboxRenderer extends DomBasicInputRenderer { 52 53 public boolean getRendersChildren() { 54 return true; 55 } 56 57 public void decode(FacesContext facesContext, UIComponent uiComponent) { 58 validateParameters(facesContext, uiComponent, null); 59 if (isStatic(uiComponent)) { 60 return; 61 } 62 63 Map requestParameterMap = 64 facesContext.getExternalContext().getRequestParameterMap(); 65 String componentClientId = uiComponent.getClientId(facesContext); 66 String decodedValue = 67 (String ) requestParameterMap.get(componentClientId); 68 if (decodedValue == null) { 69 decodedValue = "false"; 70 } else if (decodedValue.equalsIgnoreCase("on") || 71 decodedValue.equalsIgnoreCase("yes") || 72 decodedValue.equalsIgnoreCase("true")) { 73 decodedValue = "true"; 74 } 75 ((EditableValueHolder) uiComponent).setSubmittedValue(decodedValue); 76 } 77 78 public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) 79 throws IOException { 80 validateParameters(facesContext, uiComponent, null); 81 DOMContext domContext = 82 DOMContext.attachDOMContext(facesContext, uiComponent); 83 String clientId = uiComponent.getClientId(facesContext); 84 85 Element input = null; 86 if (!domContext.isInitialized()) { 87 if (uiComponent.getChildCount() > 0) { 88 Element root = domContext.createRootElement(HTML.SPAN_ELEM); 89 root.setAttribute(HTML.ID_ATTR, clientId + "span"); 90 root.setAttribute(HTML.STYLE_ATTR, "float:left"); 91 input = domContext.createElement(HTML.INPUT_ELEM); 92 root.appendChild(input); 93 } else { 94 input = (Element ) domContext.createRootElement(HTML.INPUT_ELEM); 95 } 96 input.setAttribute("type", "checkbox"); 97 input.setAttribute("id", clientId); 98 input.setAttribute("name", clientId); 99 } 100 101 if (uiComponent.getChildCount() > 0) 102 input = (Element ) domContext.getRootNode().getFirstChild(); 103 else 104 input = (Element ) domContext.getRootNode(); 105 106 String currentValue = getValue(facesContext, uiComponent); 107 if (currentValue != null && currentValue.equalsIgnoreCase("true")) { 108 input.setAttribute("checked", "checked"); 109 } else { 110 input.removeAttribute("checked"); 111 } 112 113 String styleClass = 114 (String ) uiComponent.getAttributes().get("styleClass"); 115 if (styleClass != null) { 116 input.setAttribute("class", styleClass); 117 } 118 JavascriptContext.fireEffect(uiComponent, facesContext); 119 LocalEffectEncoder.encodeLocalEffects(uiComponent, input, facesContext); 120 renderPassThruAttributes(uiComponent, input); 121 HashSet excludes = new HashSet (); 122 addJavaScript(facesContext, uiComponent, input, excludes); 123 } 124 125 126 public void renderPassThruAttributes(UIComponent uiComponent, 127 Element input) { 128 Iterator passTrhuAttributes = PassThruAttributeRenderer 129 .getpassThruAttributeNames().iterator(); 130 while (passTrhuAttributes.hasNext()) { 131 String attribute = passTrhuAttributes.next().toString(); 132 renderAttribute(uiComponent, input, attribute, attribute); 133 } 134 renderAttribute(uiComponent, input, HTML.DISABLED_ATTR, 136 HTML.DISABLED_ATTR); 137 String original = (String ) uiComponent.getAttributes().get("onfocus"); 139 String onfocus = "setFocus(this.id);"; 140 if (original == null) original = ""; 141 input.setAttribute(HTML.ONFOCUS_ATTR, onfocus + original); 142 original = (String ) uiComponent.getAttributes().get("onblur"); 144 String onblur = "setFocus('');"; 145 if (original == null) original = ""; 146 input.setAttribute(HTML.ONBLUR_ATTR, onblur + original); 147 148 } 149 150 151 public void encodeChildren(FacesContext facesContext, 152 UIComponent uiComponent) 153 throws IOException { 154 validateParameters(facesContext, uiComponent, null); 155 DOMContext domContext = 156 DOMContext.getDOMContext(facesContext, uiComponent); 157 Iterator children = uiComponent.getChildren().iterator(); 158 domContext.setCursorParent(domContext.getRootNode()); 159 while (children.hasNext()) { 160 UIComponent nextChild = (UIComponent) children.next(); 161 if (nextChild.isRendered()) { 162 encodeParentAndChildren(facesContext, nextChild); 163 } 164 } 165 domContext.stepOver(); 166 domContext.streamWrite(facesContext, uiComponent); 167 } 168 169 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) 170 throws IOException { 171 } 172 173 public Object getConvertedValue(FacesContext facesContext, UIComponent 174 uiComponent, Object submittedValue) throws ConverterException { 175 if (!(submittedValue instanceof String )) { 176 throw new ConverterException( 177 "Expecting submittedValue to be String"); 178 } 179 return Boolean.valueOf((String ) submittedValue); 180 } 181 182 protected void addJavaScript(FacesContext facesContext, 183 UIComponent uiComponent, Element root, 184 Set excludes) { 185 } 186 } 187 | Popular Tags |