001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.renderkit.dom_html_basic;
035:
036: import com.icesoft.faces.context.DOMContext;
037: import com.icesoft.faces.context.effects.JavascriptContext;
038: import com.icesoft.faces.context.effects.LocalEffectEncoder;
039: import org.w3c.dom.Element;
040:
041: import javax.faces.component.EditableValueHolder;
042: import javax.faces.component.UIComponent;
043: import javax.faces.context.FacesContext;
044: import javax.faces.convert.ConverterException;
045: import java.io.IOException;
046: import java.util.HashSet;
047: import java.util.Iterator;
048: import java.util.Map;
049: import java.util.Set;
050:
051: public class CheckboxRenderer extends DomBasicInputRenderer {
052:
053: public boolean getRendersChildren() {
054: return true;
055: }
056:
057: public void decode(FacesContext facesContext,
058: UIComponent uiComponent) {
059: validateParameters(facesContext, uiComponent, null);
060: if (isStatic(uiComponent)) {
061: return;
062: }
063:
064: Map requestParameterMap = facesContext.getExternalContext()
065: .getRequestParameterMap();
066: String componentClientId = uiComponent
067: .getClientId(facesContext);
068: String decodedValue = (String) requestParameterMap
069: .get(componentClientId);
070: if (decodedValue == null) {
071: decodedValue = "false";
072: } else if (decodedValue.equalsIgnoreCase("on")
073: || decodedValue.equalsIgnoreCase("yes")
074: || decodedValue.equalsIgnoreCase("true")) {
075: decodedValue = "true";
076: }
077: ((EditableValueHolder) uiComponent)
078: .setSubmittedValue(decodedValue);
079: }
080:
081: public void encodeBegin(FacesContext facesContext,
082: UIComponent uiComponent) throws IOException {
083: validateParameters(facesContext, uiComponent, null);
084: DOMContext domContext = DOMContext.attachDOMContext(
085: facesContext, uiComponent);
086: String clientId = uiComponent.getClientId(facesContext);
087:
088: Element input = null;
089: if (!domContext.isInitialized()) {
090: if (uiComponent.getChildCount() > 0) {
091: Element root = domContext
092: .createRootElement(HTML.SPAN_ELEM);
093: root.setAttribute(HTML.ID_ATTR, clientId + "span");
094: root.setAttribute(HTML.STYLE_ATTR, "float:left");
095: input = domContext.createElement(HTML.INPUT_ELEM);
096: root.appendChild(input);
097: } else {
098: input = (Element) domContext
099: .createRootElement(HTML.INPUT_ELEM);
100: }
101: input.setAttribute("type", "checkbox");
102: input.setAttribute("id", clientId);
103: input.setAttribute("name", clientId);
104: }
105:
106: if (uiComponent.getChildCount() > 0)
107: input = (Element) domContext.getRootNode().getFirstChild();
108: else
109: input = (Element) domContext.getRootNode();
110:
111: String currentValue = getValue(facesContext, uiComponent);
112: if (currentValue != null
113: && currentValue.equalsIgnoreCase("true")) {
114: input.setAttribute("checked", "checked");
115: } else {
116: input.removeAttribute("checked");
117: }
118:
119: String styleClass = (String) uiComponent.getAttributes().get(
120: "styleClass");
121: if (styleClass != null) {
122: input.setAttribute("class", styleClass);
123: }
124: JavascriptContext.fireEffect(uiComponent, facesContext);
125: LocalEffectEncoder.encodeLocalEffects(uiComponent, input,
126: facesContext);
127: renderPassThruAttributes(uiComponent, input);
128: HashSet excludes = new HashSet();
129: addJavaScript(facesContext, uiComponent, input, excludes);
130: }
131:
132: public void renderPassThruAttributes(UIComponent uiComponent,
133: Element input) {
134: Iterator passTrhuAttributes = PassThruAttributeRenderer
135: .getpassThruAttributeNames().iterator();
136: while (passTrhuAttributes.hasNext()) {
137: String attribute = passTrhuAttributes.next().toString();
138: renderAttribute(uiComponent, input, attribute, attribute);
139: }
140: //only "disabled" boolean attribute applies on a checkbox
141: renderAttribute(uiComponent, input, HTML.DISABLED_ATTR,
142: HTML.DISABLED_ATTR);
143: // onfocus
144: String original = (String) uiComponent.getAttributes().get(
145: "onfocus");
146: String onfocus = "setFocus(this.id);";
147: if (original == null)
148: original = "";
149: input.setAttribute(HTML.ONFOCUS_ATTR, onfocus + original);
150: // onblur
151: original = (String) uiComponent.getAttributes().get("onblur");
152: String onblur = "setFocus('');";
153: if (original == null)
154: original = "";
155: input.setAttribute(HTML.ONBLUR_ATTR, onblur + original);
156:
157: }
158:
159: public void encodeChildren(FacesContext facesContext,
160: UIComponent uiComponent) throws IOException {
161: validateParameters(facesContext, uiComponent, null);
162: DOMContext domContext = DOMContext.getDOMContext(facesContext,
163: uiComponent);
164: Iterator children = uiComponent.getChildren().iterator();
165: domContext.setCursorParent(domContext.getRootNode());
166: while (children.hasNext()) {
167: UIComponent nextChild = (UIComponent) children.next();
168: if (nextChild.isRendered()) {
169: encodeParentAndChildren(facesContext, nextChild);
170: }
171: }
172: domContext.stepOver();
173: domContext.streamWrite(facesContext, uiComponent);
174: }
175:
176: public void encodeEnd(FacesContext facesContext,
177: UIComponent uiComponent) throws IOException {
178: }
179:
180: public Object getConvertedValue(FacesContext facesContext,
181: UIComponent uiComponent, Object submittedValue)
182: throws ConverterException {
183: if (!(submittedValue instanceof String)) {
184: throw new ConverterException(
185: "Expecting submittedValue to be String");
186: }
187: return Boolean.valueOf((String) submittedValue);
188: }
189:
190: protected void addJavaScript(FacesContext facesContext,
191: UIComponent uiComponent, Element root, Set excludes) {
192: }
193: }
|