KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > wap > renderkit > WmlRenderUtils


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;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.faces.component.UIComponent;
23 import javax.faces.context.ResponseWriter;
24 import javax.faces.model.SelectItem;
25 import javax.faces.model.SelectItemGroup;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /**
31  * @author <a HREF="mailto:Jiri.Zaloudek@ivancice.cz">Jiri Zaloudek</a> (latest modification by $Author: matzew $)
32  * @version $Revision: 1.1 $ $Date: 2004/12/30 09:37:27 $
33  * $Log: WmlRenderUtils.java,v $
34  * Revision 1.1 2004/12/30 09:37:27 matzew
35  * added a new RenderKit for WML. Thanks to Jirí Žaloudek
36  *
37  */

38 public class WmlRenderUtils {
39     private static Log log = LogFactory.getLog(RendererUtils.class);
40     
41     /** Writes options for the select element.
42      * @param items is an instance of SelectItem or SelectItemGroup object. Alternatively array, collection or map of SelectItem objects.
43      */

44     public static void writeOptions(Object JavaDoc items, UIComponent component, ResponseWriter writer) throws java.io.IOException JavaDoc {
45         log.debug("method writeOptions");
46                
47         if (items instanceof SelectItemGroup){
48             log.debug("item is an instanceof SelectItemGroup");
49             SelectItemGroup group = (SelectItemGroup)items;
50             
51             writer.startElement(Attributes.OPTGROUP,component);
52             RendererUtils.writeAttribute(Attributes.TITLE, group.getLabel(), writer);
53             
54             SelectItem[] array = group.getSelectItems();
55             for (int i = 0; i < array.length; i++)
56                 writeOption(array[i],component, writer);
57             
58             writer.endElement(Attributes.OPTGROUP);
59         }
60         else {
61             if (items instanceof SelectItem){
62                 log.debug("item is an instance of SelectItem");
63                 writeOption((SelectItem)items,component, writer);
64             }
65             
66             if (items instanceof SelectItem[]){
67                 log.debug("item is an instance of SelectItem[]");
68                 SelectItem[] array = (SelectItem[])items;
69                 for (int i = 0; i < array.length; i++)
70                     writeOption(array[i],component, writer);
71             }
72             
73             if (items instanceof Collection JavaDoc){
74                 log.debug("item is an instance of Collection");
75                 Iterator JavaDoc iter = ((Collection JavaDoc)items).iterator();
76                 while(iter.hasNext())
77                     writeOptions(iter.next(),component, writer);
78             }
79             
80             if (items instanceof Map JavaDoc){
81                 log.debug("item is an instance of Map");
82                 Iterator JavaDoc iter = ((Map JavaDoc)items).entrySet().iterator();
83                 while(iter.hasNext())
84                     writeOption((SelectItem)iter.next(),component, writer);
85             }
86         }
87     }
88     
89     /** Writes one option element for the select tag. */
90     public static void writeOption(SelectItem item, UIComponent component, ResponseWriter writer) throws java.io.IOException JavaDoc {
91         writer.startElement(Attributes.OPTION,component);
92         RendererUtils.writeAttribute(Attributes.VALUE, item.getValue(), writer);
93         writer.write(item.getLabel());
94         writer.endElement(Attributes.OPTION);
95     }
96 }
97
Popular Tags