1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.swapimage;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
24 import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
25 import org.apache.myfaces.renderkit.html.util.AddResource;
26 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
27 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
28 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
29 import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
30 import org.apache.myfaces.custom.navigation.HtmlCommandNavigation;
31
32 import javax.faces.component.UIComponent;
33 import javax.faces.component.UIGraphic;
34 import javax.faces.context.FacesContext;
35 import javax.faces.context.ResponseWriter;
36 import java.io.IOException;
37
38
39
40
41
42
43
44
45
46
47 public class HtmlSwapImageRenderer
48 extends HtmlRenderer
49 {
50 private static final Log log = LogFactory.getLog(HtmlSwapImageRenderer.class);
51
52 public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
53 throws IOException
54 {
55 RendererUtils.checkParamValidity(facesContext, uiComponent, UIGraphic.class);
56
57 ResponseWriter writer = facesContext.getResponseWriter();
58
59 AddResourceFactory.getInstance(facesContext).addJavaScriptAtPosition(facesContext, AddResource.HEADER_BEGIN,
60 HtmlSwapImage.class, "swapimage.js");
61
62 String url;
63 if (uiComponent instanceof HtmlSwapImage)
64 {
65 if (uiComponent.getParent() instanceof HtmlCommandNavigation)
66 {
67 url = ((HtmlCommandNavigation) uiComponent.getParent()).isActive() ?
68 ((HtmlSwapImage) uiComponent).getActiveImageUrl() : ((HtmlSwapImage)uiComponent).getUrl();
69 }
70 else
71 {
72 url = ((HtmlSwapImage)uiComponent).getUrl();
73 }
74 }
75 else
76 {
77 url = (String)uiComponent.getAttributes().get(JSFAttr.URL_ATTR);
78 }
79
80 if ((url != null) && (url.length() > 0))
81 {
82 writer.startElement(HTML.IMG_ELEM, uiComponent);
83
84 renderId(facesContext, uiComponent);
85
86 String src = facesContext.getApplication()
87 .getViewHandler().getResourceURL(facesContext, url);
88 writer.writeURIAttribute(HTML.SRC_ATTR,
89 facesContext.getExternalContext().encodeResourceURL(src),
90 null);
91
92 if (uiComponent instanceof HtmlSwapImage)
93 {
94 String swapImageUrl = ((HtmlSwapImage) uiComponent).getSwapImageUrl();
95 swapImageUrl = facesContext.getApplication()
96 .getViewHandler().getResourceURL(facesContext, swapImageUrl);
97
98 if (swapImageUrl != null)
99 {
100 writer.writeAttribute(HTML.ONMOUSEOVER_ATTR, "SI_MM_swapImage('" + getClientId(facesContext, uiComponent) + "','','" + facesContext.getExternalContext().encodeResourceURL(swapImageUrl) + "',1);", null);
101 writer.writeAttribute(HTML.ONMOUSEOUT_ATTR, "SI_MM_swapImgRestore();", null);
102 }
103 }
104
105 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.IMG_PASSTHROUGH_ATTRIBUTES_WITHOUT_ONMOUSEOVER_AND_ONMOUSEOUT);
106
107 writer.endElement(HTML.IMG_ELEM);
108 }
109 else
110 {
111 if (log.isWarnEnabled()) log.warn("Graphic with id " + uiComponent.getClientId(facesContext) + " has no value (url).");
112 }
113 }
114
115 protected boolean shouldRenderId(
116 FacesContext context,
117 UIComponent component)
118 {
119
120 return true;
121 }
122 }