KickJava   Java API By Example, From Geeks To Geeks.

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


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.AreaComponent;
43 import org.apache.cocoon.faces.samples.components.components.MapComponent;
44 import org.apache.cocoon.faces.samples.components.model.ImageArea;
45
46 import javax.faces.component.UIComponent;
47 import javax.faces.context.FacesContext;
48 import javax.faces.context.ResponseWriter;
49
50 import java.io.IOException JavaDoc;
51
52
53 /**
54  * This class converts the internal representation of a <code>UIArea</code>
55  * component into the output stream associated with the response to a
56  * particular request.
57  */

58
59 public class AreaRenderer extends BaseRenderer {
60
61
62     // -------------------------------------------------------- Renderer Methods
63

64
65     /**
66      * <p>No decoding is required.</p>
67      *
68      * @param context <code>FacesContext</code>for the current request
69      * @param component <code>UIComponent</code> to be decoded
70      */

71     public void decode(FacesContext context, UIComponent component) {
72
73         if ((context == null) || (component == null)) {
74             throw new NullPointerException JavaDoc();
75         }
76
77     }
78
79
80     /**
81      * <p>No begin encoding is required.</p>
82      *
83      * @param context <code>FacesContext</code>for the current request
84      * @param component <code>UIComponent</code> to be decoded
85      */

86     public void encodeBegin(FacesContext context, UIComponent component)
87         throws IOException JavaDoc {
88
89         if ((context == null) || (component == null)) {
90             throw new NullPointerException JavaDoc();
91         }
92
93     }
94
95
96     /**
97      * <p>No children encoding is required.</p>
98      *
99      * @param context <code>FacesContext</code>for the current request
100      * @param component <code>UIComponent</code> to be decoded
101      */

102     public void encodeChildren(FacesContext context, UIComponent component)
103         throws IOException JavaDoc {
104
105         if ((context == null) || (component == null)) {
106             throw new NullPointerException JavaDoc();
107         }
108
109     }
110
111
112     /**
113      * <p>Encode this component.</p>
114      *
115      * @param context <code>FacesContext</code>for the current request
116      * @param component <code>UIComponent</code> to be decoded
117      */

118     public void encodeEnd(FacesContext context, UIComponent component)
119         throws IOException JavaDoc {
120
121         if ((context == null) || (component == null)) {
122             throw new NullPointerException JavaDoc();
123         }
124         AreaComponent area = (AreaComponent) component;
125         String JavaDoc targetImageId =
126             area.findComponent(area.getTargetImage()).getClientId(context);
127         ImageArea iarea = (ImageArea) area.getValue();
128         ResponseWriter writer = context.getResponseWriter();
129         StringBuffer JavaDoc sb = null;
130
131         writer.startElement("area", area);
132         writer.writeAttribute("alt", iarea.getAlt(), "alt");
133         writer.writeAttribute("coords", iarea.getCoords(), "coords");
134         writer.writeAttribute("shape", iarea.getShape(), "shape");
135         // PENDING(craigmcc) - onmouseout only works on first form of a page
136
sb =
137             new StringBuffer JavaDoc("document.forms[0]['").append(targetImageId)
138             .append("'].src='");
139         sb.append(
140             getURI(context, (String JavaDoc) area.getAttributes().get("onmouseout")));
141         sb.append("'");
142         writer.writeAttribute("onmouseout", sb.toString(), "onmouseout");
143         // PENDING(craigmcc) - onmouseover only works on first form of a page
144
sb =
145             new StringBuffer JavaDoc("document.forms[0]['").append(targetImageId)
146             .append("'].src='");
147         sb.append(
148             getURI(context, (String JavaDoc) area.getAttributes().get("onmouseover")));
149         sb.append("'");
150         writer.writeAttribute("onmouseover", sb.toString(), "onmouseover");
151         // PENDING(craigmcc) - onclick only works on first form of a page
152
sb = new StringBuffer JavaDoc("document.forms[0]['");
153         sb.append(getName(context, area));
154         sb.append("'].value='");
155         sb.append(iarea.getAlt());
156         sb.append("'; document.forms[0].submit()");
157         writer.writeAttribute("onclick", sb.toString(), "value");
158         writer.endElement("area");
159
160     }
161
162
163     // --------------------------------------------------------- Private Methods
164

165
166     /**
167      * <p>Return the calculated name for the hidden input field.</p>
168      *
169      * @param context Context for the current request
170      * @param component Component we are rendering
171      */

172     private String JavaDoc getName(FacesContext context, UIComponent component) {
173         while (component != null) {
174             if (component instanceof MapComponent) {
175                 return (component.getId() + "_current");
176             }
177             component = component.getParent();
178         }
179         throw new IllegalArgumentException JavaDoc();
180     }
181
182
183     /**
184      * <p>Return the path to be passed into JavaScript for the specified
185      * value.</p>
186      *
187      * @param context Context for the current request
188      * @param value Partial path to be (potentially) modified
189      */

190     private String JavaDoc getURI(FacesContext context, String JavaDoc value) {
191         if (value.startsWith("/")) {
192             return (context.getExternalContext().getRequestContextPath() +
193                 value);
194         } else {
195             return (value);
196         }
197     }
198
199
200 }
201
Popular Tags