View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @JSFRenderer
40   *   renderKitId = "HTML_BASIC" 
41   *   family = "javax.faces.Graphic"
42   *   type = "org.apache.myfaces.SwapImage"
43   * 
44   * @author Thomas Spiegl
45   * @version $Revision: 709234 $ $Date: 2008-10-30 15:15:18 -0500 (Thu, 30 Oct 2008) $
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         //Always return true, since javascript needs it rendered
120         return true;
121     }
122 }