1 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 ; 33 34 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 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 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 )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 clientId = uiComponent.getClientId(facesContext); 82 writer.writeAttribute(HTML.ID_ATTR, clientId, null); 83 84 String 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 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 |