KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > faces > samples > components > renderkit > MapRenderer


1 /*
2  * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistribution in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following
13  * disclaimer in the documentation and/or other materials
14  * provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any
21  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
22  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
24  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
25  * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
26  * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
27  * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE
28  * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,
29  * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
30  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF
31  * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that this software is not designed, licensed or
35  * intended for use in the design, construction, operation or
36  * maintenance of any nuclear facility.
37  */

38
39 package org.apache.cocoon.faces.samples.components.renderkit;
40
41
42 import org.apache.cocoon.faces.samples.components.components.MapComponent;
43
44 import javax.faces.component.UIComponent;
45 import javax.faces.context.FacesContext;
46 import javax.faces.context.ResponseWriter;
47
48 import java.io.IOException JavaDoc;
49
50
51 /**
52  * <p>Renderer for {@link MapComponent} in an HTML environment.</p>
53  */

54
55 public class MapRenderer extends BaseRenderer {
56
57
58     // -------------------------------------------------------- Renderer Methods
59

60
61     /**
62      * <p>Decode the incoming request parameters to determine which
63      * hotspot (if any) has been selected.</p>
64      *
65      * @param context <code>FacesContext</code>for the current request
66      * @param component <code>UIComponent</code> to be decoded
67      */

68     public void decode(FacesContext context, UIComponent component) {
69
70         if ((context == null) || (component == null)) {
71             throw new NullPointerException JavaDoc();
72         }
73         MapComponent map = (MapComponent) component;
74
75         String JavaDoc key = getName(context, map);
76         String JavaDoc value = (String JavaDoc)
77             context.getExternalContext().getRequestParameterMap().get(key);
78         if (value != null) {
79             map.setCurrent(value);
80         }
81
82     }
83
84
85     /**
86      * <p>Encode the beginning of this component.</p>
87      *
88      * @param context <code>FacesContext</code>for the current request
89      * @param component <code>UIComponent</code> to be decoded
90      */

91     public void encodeBegin(FacesContext context, UIComponent component)
92         throws IOException JavaDoc {
93
94         if ((context == null) || (component == null)) {
95             throw new NullPointerException JavaDoc();
96         }
97         MapComponent map = (MapComponent) component;
98         ResponseWriter writer = context.getResponseWriter();
99
100         writer.startElement("map", map);
101         writer.writeAttribute("name", map.getId(), "id");
102
103     }
104
105
106     /**
107      * <p>Encode the children of this component.</p>
108      *
109      * @param context <code>FacesContext</code>for the current request
110      * @param component <code>UIComponent</code> to be decoded
111      */

112     public void encodeChildren(FacesContext context, UIComponent component)
113         throws IOException JavaDoc {
114
115         if ((context == null) || (component == null)) {
116             throw new NullPointerException JavaDoc();
117         }
118
119     }
120
121
122     /**
123      * <p>Encode the ending of this component.</p>
124      *
125      * @param context <code>FacesContext</code>for the current request
126      * @param component <code>UIComponent</code> to be decoded
127      */

128     public void encodeEnd(FacesContext context, UIComponent component)
129         throws IOException JavaDoc {
130
131         if ((context == null) || (component == null)) {
132             throw new NullPointerException JavaDoc();
133         }
134         MapComponent map = (MapComponent) component;
135         ResponseWriter writer = context.getResponseWriter();
136
137         writer.startElement("input", map);
138         writer.writeAttribute("type", "hidden", null);
139         writer.writeAttribute("name", getName(context, map), "clientId");
140         writer.endElement("input");
141         writer.endElement("map");
142
143     }
144
145
146     // --------------------------------------------------------- Private Methods
147

148
149     /**
150      * <p>Return the calculated name for the hidden input field.</p>
151      *
152      * @param context Context for the current request
153      * @param component Component we are rendering
154      */

155     private String JavaDoc getName(FacesContext context, UIComponent component) {
156         return (component.getId() + "_current");
157     }
158
159
160     /**
161      * <p>Return the context-relative path for the current page.</p>
162      *
163      * @param context Context for the current request
164      */

165     private String JavaDoc getURI(FacesContext context) {
166
167         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
168         sb.append(context.getExternalContext().getRequestContextPath());
169         // PENDING(craigmcc) - will need to change if this is generalized
170
sb.append("/faces");
171         sb.append(context.getViewRoot().getViewId());
172         return (sb.toString());
173
174     }
175
176
177 }
178
Popular Tags