KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > swapimage > HtmlSwapImageRenderer


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.swapimage;
17
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.apache.myfaces.component.html.util.AddResource;
21 import org.apache.myfaces.renderkit.JSFAttr;
22 import org.apache.myfaces.renderkit.RendererUtils;
23 import org.apache.myfaces.renderkit.html.HTML;
24 import org.apache.myfaces.renderkit.html.HtmlRenderer;
25 import org.apache.myfaces.renderkit.html.HtmlRendererUtils;
26 import org.apache.myfaces.custom.navigation.HtmlCommandNavigation;
27
28 import javax.faces.component.UIComponent;
29 import javax.faces.component.UIGraphic;
30 import javax.faces.context.FacesContext;
31 import javax.faces.context.ResponseWriter;
32 import java.io.IOException JavaDoc;
33
34 /**
35  * @author Thomas Spiegl
36  * @version $Revision: 1.2 $ $Date: 2005/01/09 21:56:33 $
37  * $Log: HtmlSwapImageRenderer.java,v $
38  * Revision 1.2 2005/01/09 21:56:33 tomsp
39  * added new component swapimage
40  *
41  * Revision 1.1 2005/01/09 12:36:58 tomsp
42  * added new component swapimage
43  *
44  */

45 public class HtmlSwapImageRenderer
46         extends HtmlRenderer
47 {
48     private static final Log log = LogFactory.getLog(HtmlSwapImageRenderer.class);
49
50     public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
51             throws IOException JavaDoc
52     {
53         RendererUtils.checkParamValidity(facesContext, uiComponent, UIGraphic.class);
54
55         ResponseWriter writer = facesContext.getResponseWriter();
56
57         AddResource.addJavaScriptToHeader(HtmlSwapImage.class, "swapimage.js", facesContext);
58
59         String JavaDoc url;
60         if (uiComponent instanceof HtmlSwapImage)
61         {
62             if (uiComponent.getParent() instanceof HtmlCommandNavigation)
63             {
64                 url = ((HtmlCommandNavigation) uiComponent.getParent()).isActive() ?
65                     ((HtmlSwapImage) uiComponent).getActiveImageUrl() : ((HtmlSwapImage)uiComponent).getUrl();
66             }
67             else
68             {
69                 url = ((HtmlSwapImage)uiComponent).getUrl();
70             }
71         }
72         else
73         {
74             url = (String JavaDoc)uiComponent.getAttributes().get(JSFAttr.URL_ATTR);
75         }
76
77         if ((url != null) && (url.length() > 0))
78         {
79             writer.startElement(HTML.IMG_ELEM, uiComponent);
80
81             String JavaDoc clientId = uiComponent.getClientId(facesContext);
82             writer.writeAttribute(HTML.ID_ATTR, clientId, null);
83
84             String JavaDoc src = facesContext.getApplication()
85                     .getViewHandler().getResourceURL(facesContext, url);
86             writer.writeURIAttribute(HTML.SRC_ATTR,
87                                      facesContext.getExternalContext().encodeResourceURL(src),
88                                      null);
89
90             if (uiComponent instanceof HtmlSwapImage)
91             {
92                 String JavaDoc swapImageUrl = ((HtmlSwapImage) uiComponent).getSwapImageUrl();
93                 if (swapImageUrl != null)
94                 {
95                     writer.writeAttribute(HTML.ONMOUSEOVER_ATTR, "SI_MM_swapImage('" + clientId + "','','" + swapImageUrl + "',1);", null);
96                     writer.writeAttribute(HTML.ONMOUSEOUT_ATTR, "SI_MM_swapImgRestore();", null);
97                 }
98             }
99
100             HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.IMG_PASSTHROUGH_ATTRIBUTES);
101
102             writer.endElement(HTML.IMG_ELEM);
103         }
104         else
105         {
106             if (log.isWarnEnabled()) log.warn("Graphic with id " + uiComponent.getClientId(facesContext) + " has no value (url).");
107         }
108     }
109 }
110
Popular Tags