KickJava   Java API By Example, From Geeks To Geeks.

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


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

46 public class SelectManyRenderer extends WmlRenderer {
47     private static Log log = LogFactory.getLog(SelectManyRenderer.class);
48     
49     /** Creates a new instance of TextRenderer */
50     public SelectManyRenderer() {
51         super();
52         log.debug("created object " + this.getClass().getName());
53     }
54     
55     public void encodeBegin(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
56         log.debug("encodeBegin(" + component.getId() + ")");
57         if (context == null || component == null) {
58             throw new NullPointerException JavaDoc();
59         }
60     }
61     
62     public void encodeChildren(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
63         log.debug("encodeChildren(" + component.getId() + ")");
64         if (context == null || component == null) {
65             throw new NullPointerException JavaDoc();
66         }
67     }
68     
69     public void encodeEnd(FacesContext context, UIComponent component) throws java.io.IOException JavaDoc {
70         log.debug("encodeEnd(" + component.getId() + ")");
71         if (context == null || component == null) {
72             throw new NullPointerException JavaDoc();
73         }
74         
75         if (!component.isRendered()) return;
76         
77         SelectMany comp = (SelectMany)component;
78         
79         ResponseWriter writer = context.getResponseWriter();
80         
81         writer.startElement(Attributes.SELECT, component);
82         RendererUtils.writeAttribute(Attributes.ID, comp.getClientId(context), writer);
83         RendererUtils.writeAttribute(Attributes.STYLE_CLASS, comp.getStyleClass(), writer);
84         RendererUtils.writeAttribute(Attributes.XML_LANG, comp.getXmllang(), writer);
85         /* TODO: attribute "required" */
86         
87         /* attribute name is not required. If is not set, name value equals component id */
88         if (comp.getName() == null) {
89             log.debug("getName is null");
90             comp.setName(comp.getClientId(context));
91         }
92         RendererUtils.writeAttribute(Attributes.NAME, comp.getName(), writer);
93         
94         RendererUtils.writeAttribute(Attributes.TABINDEX, comp.getTabindex(), writer);
95         RendererUtils.writeAttribute(Attributes.TITLE, comp.getTitle(), writer);
96         
97 // RenderUtils.writeAttribute(Attributes.VALUE, convertSelectedValuesToString(comp.getSelectedValues()), writer);
98
RendererUtils.writeAttribute(Attributes.VALUE, convertSelectedValuesToString(comp.getSelectedValues()), writer);
99         
100         // multiple
101
RendererUtils.writeAttribute(Attributes.MULTIPLE, "true", writer);
102         
103         List JavaDoc child = component.getChildren();
104         for (int i = 0; i < child.size(); i++){
105             if (child.get(i) instanceof UISelectItem){
106                 UISelectItem item = (UISelectItem)child.get(i);
107                 WmlRenderUtils.writeOption((SelectItem)item.getValue(), comp, writer);
108             }
109             if (child.get(i) instanceof UISelectItems){
110                 UISelectItems item = (UISelectItems)child.get(i);
111                 WmlRenderUtils.writeOptions(item.getValue(), comp, writer);
112             }
113         }
114         
115         writer.endElement(Attributes.SELECT);
116     }
117     
118     public void decode(FacesContext context, UIComponent component) {
119         log.debug("decode(" + component.getId() + ")");
120         if (component == null || context == null) throw new NullPointerException JavaDoc();
121         if (!(component instanceof SelectMany))
122             log.error("Component " + component.getClass().getName() + " is no SelectMany component, cannot be converted!");
123         
124         SelectMany comp = (SelectMany)component;
125         
126         Map JavaDoc map = context.getExternalContext().getRequestParameterMap();
127         
128         // Set the submitted value of this UISelectOne component
129
if (map.containsKey(comp.getName())){
130             log.debug("Parameter:" + comp.getName() + " was found in the request. Value: " + (String JavaDoc)map.get(comp.getName()));
131             
132             String JavaDoc value = (String JavaDoc)map.get(comp.getName());
133             comp.setSelectedValues(convertSubmittedValueToObject(value));
134         }
135     }
136     
137     /** Overrides method getConvertedValue */
138     public Object JavaDoc getConvertedValue(FacesContext context, UIComponent component, Object JavaDoc submittedValue) throws javax.faces.convert.ConverterException {
139         if (!(component instanceof UISelectMany))
140             throw new IllegalArgumentException JavaDoc("Expected component of type UISelectMany. Cannot convert selected value.");
141         
142         UISelectMany comp = (UISelectMany)component;
143         
144         return(RendererUtils.convertUISelectManyToObject(context, comp, submittedValue));
145     }
146     
147     /** Returns WML String representating the selected options.
148      * @param obj Object returned from getSelectedValue(). This object must be a String[] type.
149      * @return String representing selected options for wml code. Is rendered as a value of "value" attribute in the select element.
150      */

151     private String JavaDoc convertSelectedValuesToString(Object JavaDoc obj){
152         if (obj == null) return(null);
153         
154         if (!(obj instanceof String JavaDoc[]))
155             throw new ClassCastException JavaDoc("Value of SelectMany component must be a String[] type.");
156                 
157         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
158         
159         String JavaDoc[] selected = (String JavaDoc[])obj;
160
161         for (int i = 0 ; i < selected.length; i++){
162             String JavaDoc item = selected[i];
163             
164             buff.append(item);
165             
166             // insert separator
167
if (i < selected.length - 1) buff.append(Attributes.SELECT_MANY_SEPARATOR);
168         }
169         return (buff.toString());
170     }
171     
172     /** Tokenizes submitted value from request parameter to the String array . */
173     private String JavaDoc[] convertSubmittedValueToObject(String JavaDoc value){
174         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(value, Attributes.SELECT_MANY_SEPARATOR);
175         
176         String JavaDoc[] ret = new String JavaDoc[st.countTokens()];
177         
178         log.debug("Selected option:");
179         for (int i = 0; st.hasMoreTokens(); i++){
180             ret[i] = st.nextToken();
181             log.debug(ret[i]);
182         }
183         return (ret);
184     }
185 }
186
187
Popular Tags