KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > jsvalueset > HtmlJsValueSetRenderer


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.custom.jsvalueset;
17
18 import org.apache.myfaces.renderkit.RendererUtils;
19 import org.apache.myfaces.renderkit.html.HTML;
20 import org.apache.myfaces.renderkit.html.HtmlRenderer;
21
22 import javax.faces.application.Application;
23 import javax.faces.component.UIComponent;
24 import javax.faces.context.FacesContext;
25 import javax.faces.context.ResponseWriter;
26 import java.io.IOException JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * $Log: HtmlJsValueSetRenderer.java,v $
33  * Revision 1.3 2005/03/12 02:15:18 mmarinschek
34  * jsvalueset now supports maps of maps; ui component logging enhanced
35  *
36  * Revision 1.2 2004/12/27 04:11:11 mmarinschek
37  * Data Table stores the state of facets of children; script tag is rendered with type attribute instead of language attribute, popup works better as a column in a data table
38  *
39  * Revision 1.1 2004/12/19 00:50:55 mmarinschek
40  * JsValueSetTag
41  *
42  *
43  * @author Martin Marinschek (latest modification by $Author: mmarinschek $)
44  * @version $Revision: 1.3 $ $Date: 2005/03/12 02:15:18 $
45  */

46 public class HtmlJsValueSetRenderer
47         extends HtmlRenderer
48 {
49     public void encodeEnd(FacesContext facesContext, UIComponent component)
50             throws IOException JavaDoc
51     {
52         RendererUtils.checkParamValidity(facesContext, component, HtmlJsValueSet.class);
53
54         HtmlJsValueSet htmlJsValueSet = (HtmlJsValueSet) component;
55
56         Object JavaDoc value = htmlJsValueSet.getValue();
57         String JavaDoc name = htmlJsValueSet.getName();
58
59
60         ResponseWriter writer = getFacesContext().getResponseWriter();
61
62         writer.startElement(HTML.SCRIPT_ELEM,null);
63         writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR,HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT,null);
64
65         if(value instanceof String JavaDoc ||
66                 value instanceof Number JavaDoc || value instanceof Boolean JavaDoc || value == null)
67         {
68             writer.writeText("var " + name+"="+getValueString(value)+";",null);
69         }
70         else if(value instanceof Object JavaDoc[])
71         {
72             Object JavaDoc[] objs = (Object JavaDoc[]) value;
73
74             startArray(writer, name);
75
76             for (int i = 0; i < objs.length; i++)
77             {
78                 Object JavaDoc obj = objs[i];
79                 writeArrayElement(writer, name, i, obj);
80             }
81         }
82         else if(value instanceof Collection JavaDoc)
83         {
84             Collection JavaDoc coll = (Collection JavaDoc) value;
85
86             startArray(writer, name);
87
88             int i=0;
89
90             for (Iterator JavaDoc iterator = coll.iterator(); iterator.hasNext();)
91             {
92                 Object JavaDoc obj = iterator.next();
93                 writeArrayElement(writer, name, i, obj);
94                 i++;
95             }
96         }
97         else if(value instanceof Map JavaDoc)
98         {
99             Map JavaDoc map = (Map JavaDoc) value;
100             Iterator JavaDoc it = map.entrySet().iterator();
101
102             startArray(writer,name);
103
104             while (it.hasNext())
105             {
106                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
107                 writeArrayElement(writer, name, entry.getKey(), entry.getValue());
108             }
109         }
110         else
111             throw new IOException JavaDoc("Type of value not handled. Allowed are String, Number, Boolean, Object[], Collection, Map. Type of value : "+value.getClass());
112
113
114         writer.endElement(HTML.SCRIPT_ELEM);
115     }
116
117     private void writeArrayElement(ResponseWriter writer, String JavaDoc name, Object JavaDoc key, Object JavaDoc obj)
118         throws IOException JavaDoc
119     {
120         String JavaDoc prefix = name+"["+getValueString(key)+"]";
121
122         if(obj instanceof Map JavaDoc)
123         {
124             writer.writeText(prefix + "= new Array();",null);
125
126             Iterator JavaDoc it = ((Map JavaDoc) obj).entrySet().iterator();
127
128             while (it.hasNext())
129             {
130                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
131
132                 writeArrayElement(writer,prefix,entry.getKey(),entry.getValue());
133             }
134         }
135         else
136         {
137             writer.writeText(prefix + "="+getValueString(obj)+";",null);
138         }
139     }
140
141     private void writeArrayElement(ResponseWriter writer, String JavaDoc name, int i, Object JavaDoc obj)
142             throws IOException JavaDoc
143     {
144         writer.writeText(name+"["+i+"]="+getValueString(obj)+";",null);
145     }
146
147     private void startArray(ResponseWriter writer, String JavaDoc name)
148             throws IOException JavaDoc
149     {
150         writer.writeText("var " + name+"=new Array();",null);
151     }
152
153     private String JavaDoc getValueString(Object JavaDoc value) throws IOException JavaDoc
154     {
155         if(value instanceof String JavaDoc)
156         {
157             return "'"+value+"'";
158         }
159         else if(value instanceof Number JavaDoc)
160         {
161             return value.toString();
162         }
163         else if(value instanceof Boolean JavaDoc)
164         {
165             return value.toString();
166         }
167         else if(value == null)
168         {
169             return "null";
170         }
171         else
172         {
173             throw new IOException JavaDoc("value : "+value+" is of type : "+value.getClass()+
174                     ", provide a method to convert this.");
175         }
176     }
177
178     protected Application getApplication()
179     {
180         return getFacesContext().getApplication();
181     }
182
183     protected FacesContext getFacesContext()
184     {
185         return FacesContext.getCurrentInstance();
186     }
187
188 }
189
Popular Tags