KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import javax.faces.component.UIComponent;
22 import javax.faces.component.UISelectItem;
23 import javax.faces.component.UISelectItems;
24 import javax.faces.context.FacesContext;
25 import javax.faces.context.ResponseWriter;
26 import javax.faces.model.SelectItem;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.myfaces.wap.component.SelectOne;
31 import org.apache.myfaces.wap.renderkit.Attributes;
32 import org.apache.myfaces.wap.renderkit.RendererUtils;
33 import org.apache.myfaces.wap.renderkit.WmlRenderUtils;
34 import org.apache.myfaces.wap.renderkit.WmlRenderer;
35
36 /**
37  * @author <a HREF="mailto:Jiri.Zaloudek@ivancice.cz">Jiri Zaloudek</a> (latest modification by $Author: matzew $)
38  * @version $Revision: 1.1 $ $Date: 2004/12/30 09:37:26 $
39  * $Log: SelectOneRenderer.java,v $
40  * Revision 1.1 2004/12/30 09:37:26 matzew
41  * added a new RenderKit for WML. Thanks to Jirí Žaloudek
42  *
43  */

44 public class SelectOneRenderer extends WmlRenderer {
45     private static Log log = LogFactory.getLog(SelectOneRenderer.class);
46     
47     /** Creates a new instance of TextRenderer */
48     public SelectOneRenderer() {
49         super();
50         log.debug("created object " + this.getClass().getName());
51     }
52     
53     public void encodeBegin(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
54         log.debug("encodeBegin(" + component.getId() + ")");
55         if (context == null || component == null) {
56             throw new NullPointerException JavaDoc();
57         }
58     }
59     
60     public void encodeChildren(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
61         log.debug("encodeChildren(" + component.getId() + ")");
62         if (context == null || component == null) {
63             throw new NullPointerException JavaDoc();
64         }
65     }
66     
67     public void encodeEnd(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
68         log.debug("encodeEnd(" + component.getId() + ")");
69         if (context == null || component == null) {
70             throw new NullPointerException JavaDoc();
71         }
72         
73         if (!component.isRendered()) return;
74         
75         SelectOne comp = (SelectOne)component;
76         
77         ResponseWriter writer = context.getResponseWriter();
78         
79         writer.startElement(Attributes.SELECT, component);
80         RendererUtils.writeAttribute(Attributes.ID, comp.getClientId(context), writer);
81         RendererUtils.writeAttribute(Attributes.STYLE_CLASS, comp.getStyleClass(), writer);
82         RendererUtils.writeAttribute(Attributes.XML_LANG, comp.getXmllang(), writer);
83         /* TODO: attribute "required" */
84         
85         /* attribute name is not required. If is not set, name value equals component id */
86         if (comp.getName() == null) {
87             log.debug("getName is null");
88             comp.setName(comp.getClientId(context));
89         }
90         RendererUtils.writeAttribute(Attributes.NAME, comp.getName(), writer);
91         
92         RendererUtils.writeAttribute(Attributes.TABINDEX, comp.getTabindex(), writer);
93         RendererUtils.writeAttribute(Attributes.TITLE, comp.getTitle(), writer);
94         RendererUtils.writeAttribute(Attributes.VALUE, comp.getValue(), writer);
95         
96         // "false" is default. Don't must be written.
97
//RenderUtils.writeAttribute(Attributes.MULTIPLE, "false", writer);
98

99         List JavaDoc child = component.getChildren();
100         for (int i = 0; i < child.size(); i++){
101             if (child.get(i) instanceof UISelectItem){
102                 UISelectItem item = (UISelectItem)child.get(i);
103                 WmlRenderUtils.writeOption((SelectItem)item.getValue(), comp, writer);
104             }
105             if (child.get(i) instanceof UISelectItems){
106                 UISelectItems item = (UISelectItems)child.get(i);
107                 WmlRenderUtils.writeOptions(item.getValue(), comp, writer);
108             }
109         }
110         
111         writer.endElement(Attributes.SELECT);
112     }
113     
114     public void decode(FacesContext context, UIComponent component) {
115         log.debug("decode(" + component.getId() + ")");
116         if (component == null || context == null) throw new NullPointerException JavaDoc();
117         if (!(component instanceof SelectOne))
118             log.error("Component " + component.getClass().getName() + " is no SelectOne component, cannot be converted!");
119         
120         SelectOne comp = (SelectOne)component;
121         
122         Map JavaDoc map = context.getExternalContext().getRequestParameterMap();
123         
124         // Set the submitted value of this UISelectOne component
125
if (map.containsKey(comp.getName())){
126             log.debug("Parameter:" + comp.getName() + " was found in the request. Value: " + (String JavaDoc)map.get(comp.getName()));
127             comp.setSubmittedValue(map.get(comp.getName()));
128             //comp.setValue(map.get(comp.getName()));
129
}
130     }
131     
132     /** Overrides method getConvertedValue */
133     public Object JavaDoc getConvertedValue(FacesContext context, UIComponent component, Object JavaDoc submittedValue) throws javax.faces.convert.ConverterException {
134         return(RendererUtils.convertToObject(context, component));
135     }
136 }
137
138
Popular Tags