KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > renderkit > dom_html_basic > CheckboxRenderer


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

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 JavaDoc;
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 JavaDoc;
46 import java.util.HashSet JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.Map JavaDoc;
49 import java.util.Set JavaDoc;
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 JavaDoc requestParameterMap =
64                 facesContext.getExternalContext().getRequestParameterMap();
65         String JavaDoc componentClientId = uiComponent.getClientId(facesContext);
66         String JavaDoc decodedValue =
67                 (String JavaDoc) 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 JavaDoc {
80         validateParameters(facesContext, uiComponent, null);
81         DOMContext domContext =
82                 DOMContext.attachDOMContext(facesContext, uiComponent);
83         String JavaDoc clientId = uiComponent.getClientId(facesContext);
84
85         Element JavaDoc input = null;
86         if (!domContext.isInitialized()) {
87             if (uiComponent.getChildCount() > 0) {
88                 Element JavaDoc 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 JavaDoc) 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 JavaDoc) domContext.getRootNode().getFirstChild();
103         else
104             input = (Element JavaDoc) domContext.getRootNode();
105
106         String JavaDoc 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 JavaDoc styleClass =
114                 (String JavaDoc) 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 JavaDoc excludes = new HashSet JavaDoc();
122         addJavaScript(facesContext, uiComponent, input, excludes);
123     }
124
125
126     public void renderPassThruAttributes(UIComponent uiComponent,
127                                          Element JavaDoc input) {
128         Iterator JavaDoc passTrhuAttributes = PassThruAttributeRenderer
129                 .getpassThruAttributeNames().iterator();
130         while (passTrhuAttributes.hasNext()) {
131             String JavaDoc attribute = passTrhuAttributes.next().toString();
132             renderAttribute(uiComponent, input, attribute, attribute);
133         }
134         //only "disabled" boolean attribute applies on a checkbox
135
renderAttribute(uiComponent, input, HTML.DISABLED_ATTR,
136                         HTML.DISABLED_ATTR);
137         // onfocus
138
String JavaDoc original = (String JavaDoc) uiComponent.getAttributes().get("onfocus");
139         String JavaDoc onfocus = "setFocus(this.id);";
140         if (original == null) original = "";
141         input.setAttribute(HTML.ONFOCUS_ATTR, onfocus + original);
142         // onblur
143
original = (String JavaDoc) uiComponent.getAttributes().get("onblur");
144         String JavaDoc 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 JavaDoc {
154         validateParameters(facesContext, uiComponent, null);
155         DOMContext domContext =
156                 DOMContext.getDOMContext(facesContext, uiComponent);
157         Iterator JavaDoc 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 JavaDoc {
171     }
172
173     public Object JavaDoc getConvertedValue(FacesContext facesContext, UIComponent
174             uiComponent, Object JavaDoc submittedValue) throws ConverterException {
175         if (!(submittedValue instanceof String JavaDoc)) {
176             throw new ConverterException(
177                     "Expecting submittedValue to be String");
178         }
179         return Boolean.valueOf((String JavaDoc) submittedValue);
180     }
181
182     protected void addJavaScript(FacesContext facesContext,
183                                  UIComponent uiComponent, Element JavaDoc root,
184                                  Set JavaDoc excludes) {
185     }
186 }
187
Popular Tags