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 java.io.IOException;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.faces.application.ResourceDependency;
26  import javax.faces.component.UIComponent;
27  import javax.faces.component.UIGraphic;
28  import javax.faces.component.behavior.ClientBehavior;
29  import javax.faces.component.behavior.ClientBehaviorHolder;
30  import javax.faces.context.FacesContext;
31  import javax.faces.context.ResponseWriter;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.apache.myfaces.custom.navigation.HtmlCommandNavigation;
36  import org.apache.myfaces.shared_tomahawk.renderkit.ClientBehaviorEvents;
37  import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
38  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
39  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
40  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
41  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
42  import org.apache.myfaces.shared_tomahawk.renderkit.html.util.ResourceUtils;
43  
44  /**
45   * @JSFRenderer
46   *   renderKitId = "HTML_BASIC" 
47   *   family = "javax.faces.Graphic"
48   *   type = "org.apache.myfaces.SwapImage"
49   * 
50   * @author Thomas Spiegl
51   * @version $Revision: 709234 $ $Date: 2008-10-30 15:15:18 -0500 (jue, 30 oct 2008) $
52   */
53  @ResourceDependency(library="oam.custom.swapimage",name="swapimage.js")
54  public class HtmlSwapImageRenderer
55          extends HtmlRenderer
56  {
57      private static final Log log = LogFactory.getLog(HtmlSwapImageRenderer.class);
58  
59      @Override
60      public void decode(FacesContext context, UIComponent component)
61      {
62          super.decode(context, component);
63          
64          HtmlRendererUtils.decodeClientBehaviors(context, component);
65      }
66      
67      public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
68              throws IOException
69      {
70          RendererUtils.checkParamValidity(facesContext, uiComponent, UIGraphic.class);
71  
72          ResponseWriter writer = facesContext.getResponseWriter();
73  
74          //AddResourceFactory.getInstance(facesContext).addJavaScriptAtPosition(facesContext, AddResource.HEADER_BEGIN,
75          //        HtmlSwapImage.class, "swapimage.js");
76          
77          HtmlSwapImage swapImage = (HtmlSwapImage) uiComponent;
78          
79          Map<String, List<ClientBehavior>> behaviors = swapImage.getClientBehaviors();
80          if (!behaviors.isEmpty())
81          {
82              ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, facesContext.getResponseWriter());
83          }
84  
85          String url;
86          if (uiComponent.getParent() instanceof HtmlCommandNavigation)
87          {
88              url = ((HtmlCommandNavigation) uiComponent.getParent()).isActive() ?
89                  ((HtmlSwapImage) uiComponent).getActiveImageUrl() : ((HtmlSwapImage)uiComponent).getUrl();
90          }
91          else
92          {
93              url = ((HtmlSwapImage)uiComponent).getUrl();
94          }
95  
96          if ((url != null) && (url.length() > 0))
97          {
98              writer.startElement(HTML.IMG_ELEM, uiComponent);
99  
100             renderId(facesContext, uiComponent);
101 
102             String src = facesContext.getApplication()
103                     .getViewHandler().getResourceURL(facesContext, url);
104             writer.writeURIAttribute(HTML.SRC_ATTR,
105                                      facesContext.getExternalContext().encodeResourceURL(src),
106                                      null);
107 
108             String swapImageUrl = ((HtmlSwapImage) uiComponent).getSwapImageUrl();
109             swapImageUrl = facesContext.getApplication()
110             .getViewHandler().getResourceURL(facesContext, swapImageUrl);
111 
112             if (behaviors != null && !behaviors.isEmpty())
113             {
114                 if (swapImageUrl != null)
115                 {
116                     HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEOVER_ATTR, uiComponent, 
117                             ClientBehaviorEvents.MOUSEOVER, null, behaviors, HTML.ONMOUSEOVER_ATTR, swapImage.getOnmouseover(),
118                             "SI_MM_swapImage('" + getClientId(facesContext, uiComponent) + "','','" + facesContext.getExternalContext().encodeResourceURL(swapImageUrl) + "',1);");
119                     HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEOUT_ATTR, uiComponent, 
120                             ClientBehaviorEvents.MOUSEOUT, null, behaviors, HTML.ONMOUSEOUT_ATTR, swapImage.getOnmouseout(),
121                             "SI_MM_swapImgRestore();");
122                 }
123                 else
124                 {
125                     HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEOVER_ATTR, uiComponent, 
126                             ClientBehaviorEvents.MOUSEOVER, null, behaviors, HTML.ONMOUSEOVER_ATTR, swapImage.getOnmouseover(), null);
127                     HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, HTML.ONMOUSEOUT_ATTR, uiComponent, 
128                             ClientBehaviorEvents.MOUSEOUT, null, behaviors, HTML.ONMOUSEOUT_ATTR, swapImage.getOnmouseout(), null);
129                 }
130                 
131                 HtmlRendererUtils.renderBehaviorizedEventHandlersWithoutOnmouseoverAndOnmouseout(facesContext, writer, uiComponent, behaviors);
132 
133                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.UNIVERSAL_ATTRIBUTES);
134                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.IMG_ATTRIBUTES);
135             }
136             else
137             {
138                 if (swapImageUrl != null)
139                 {
140                     writer.writeAttribute(HTML.ONMOUSEOVER_ATTR, "SI_MM_swapImage('" + getClientId(facesContext, uiComponent) + "','','" + facesContext.getExternalContext().encodeResourceURL(swapImageUrl) + "',1);", null);
141                     writer.writeAttribute(HTML.ONMOUSEOUT_ATTR, "SI_MM_swapImgRestore();", null);
142                 }
143                 else
144                 {
145                     HtmlRendererUtils.renderHTMLAttribute(writer, uiComponent, HTML.ONMOUSEOVER_ATTR, HTML.ONMOUSEOVER_ATTR);
146                     HtmlRendererUtils.renderHTMLAttribute(writer, uiComponent, HTML.ONMOUSEOUT_ATTR, HTML.ONMOUSEOUT_ATTR);
147                 }
148                 
149                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.IMG_PASSTHROUGH_ATTRIBUTES_WITHOUT_ONMOUSEOVER_AND_ONMOUSEOUT);
150             }
151 
152             writer.endElement(HTML.IMG_ELEM);
153         }
154         else
155         {
156             if (log.isWarnEnabled()) log.warn("Graphic with id " + uiComponent.getClientId(facesContext) + " has no value (url).");
157         }
158     }
159     
160     protected boolean shouldRenderId(
161             FacesContext context,
162             UIComponent  component)
163     {
164         //Always return true, since javascript needs it rendered
165         return true;
166     }
167 }