KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > wap > renderkit > wml > SelectBooleanRenderer


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.wap.renderkit.wml;
17
18 import java.util.Map JavaDoc;
19
20 import javax.faces.component.UIComponent;
21 import javax.faces.context.FacesContext;
22 import javax.faces.context.ResponseWriter;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.myfaces.wap.component.SelectBoolean;
27 import org.apache.myfaces.wap.renderkit.Attributes;
28 import org.apache.myfaces.wap.renderkit.RendererUtils;
29 import org.apache.myfaces.wap.renderkit.WmlRenderer;
30
31 /**
32  * @author <a HREF="mailto:Jiri.Zaloudek@ivancice.cz">Jiri Zaloudek</a> (latest modification by $Author: matzew $)
33  * @version $Revision: 1.1 $ $Date: 2004/12/30 09:37:26 $
34  * $Log: SelectBooleanRenderer.java,v $
35  * Revision 1.1 2004/12/30 09:37:26 matzew
36  * added a new RenderKit for WML. Thanks to Jirí Žaloudek
37  *
38  */

39 public class SelectBooleanRenderer extends WmlRenderer {
40     private static Log log = LogFactory.getLog(SelectBooleanRenderer.class);
41     private static String JavaDoc OPTION_VALUE = "bool";
42     
43     /** Creates a new instance of renderer */
44     public SelectBooleanRenderer() {
45         super();
46         log.debug("created object " + this.getClass().getName());
47     }
48     
49     public void encodeBegin(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
50         log.debug("encodeBegin(" + component.getId() + ")");
51         if (context == null || component == null) {
52             throw new NullPointerException JavaDoc();
53         }
54         if (!component.isRendered()) return;
55
56         SelectBoolean comp = (SelectBoolean)component;
57         
58         ResponseWriter writer = context.getResponseWriter();
59         
60         writer.startElement(Attributes.SELECT, component);
61         RendererUtils.writeAttribute(Attributes.ID, comp.getClientId(context), writer);
62         RendererUtils.writeAttribute(Attributes.STYLE_CLASS, comp.getStyleClass(), writer);
63         RendererUtils.writeAttribute(Attributes.XML_LANG, comp.getXmllang(), writer);
64         
65         /* attribute name is not required. If is not set, name value equals component id */
66         if (comp.getName() == null) {
67             log.debug("getName is null");
68             comp.setName(comp.getClientId(context));
69         }
70         RendererUtils.writeAttribute(Attributes.NAME, comp.getName(), writer);
71         
72         RendererUtils.writeAttribute(Attributes.TABINDEX, comp.getTabindex(), writer);
73         RendererUtils.writeAttribute(Attributes.TITLE, comp.getTitle(), writer);
74                 
75         if (comp.isSelected())
76             RendererUtils.writeAttribute(Attributes.VALUE, OPTION_VALUE, writer);
77         else { /* don't check off */ }
78         
79         // multiple
80
RendererUtils.writeAttribute(Attributes.MULTIPLE, "true", writer);
81         
82         // write option
83
writer.startElement(Attributes.OPTION, component);
84         RendererUtils.writeAttribute(Attributes.VALUE, OPTION_VALUE, writer);
85     }
86     
87     public void encodeChildren(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
88         log.debug("encodeChildren(" + component.getId() + ")");
89         if (context == null || component == null) {
90             throw new NullPointerException JavaDoc();
91         }
92         if (!component.isRendered()) return;
93         
94         RendererUtils.renderChildren(context, component);
95     }
96     
97     public void encodeEnd(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
98         log.debug("encodeEnd(" + component.getId() + ")");
99         if (context == null || component == null) {
100             throw new NullPointerException JavaDoc();
101         }
102         if (!component.isRendered()) return;
103         
104         ResponseWriter writer = context.getResponseWriter();
105         
106         writer.endElement(Attributes.OPTION);
107         writer.endElement(Attributes.SELECT);
108     }
109     
110     public void decode(FacesContext context, UIComponent component) {
111         log.debug("decode(" + component.getId() + ")");
112         if (component == null || context == null) throw new NullPointerException JavaDoc();
113         if (!(component instanceof SelectBoolean))
114             log.error("Component " + component.getClass().getName() + " is no SelectMany component, cannot be converted!");
115         
116         SelectBoolean comp = (SelectBoolean)component;
117         
118         Map JavaDoc map = context.getExternalContext().getRequestParameterMap();
119         
120         // Set the submitted value of this UISelectOne component
121
if (map.containsKey(comp.getName())){
122             log.debug("Parameter:" + comp.getName() + " was found in the request. Value: " + (String JavaDoc)map.get(comp.getName()));
123             
124             String JavaDoc value = (String JavaDoc)map.get(comp.getName());
125             if (OPTION_VALUE.equalsIgnoreCase(value)) comp.setSelected(true);
126             else comp.setSelected(false);
127         }
128     }
129 }
130
131
Popular Tags