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.imageloop;
20  
21  import org.apache.myfaces.custom.dojo.DojoConfig;
22  import org.apache.myfaces.custom.dojo.DojoUtils;
23  import org.apache.myfaces.renderkit.html.util.AddResource;
24  import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
25  import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
26  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
27  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
28  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
29  
30  import javax.faces.component.UIComponent;
31  import javax.faces.context.FacesContext;
32  import javax.faces.context.ResponseWriter;
33  import java.io.IOException;
34  
35  /**
36   * 
37   * @JSFRenderer
38   *   renderKitId = "HTML_BASIC" 
39   *   family = "javax.faces.Output"
40   *   type = "org.apache.myfaces.HtmlImageLoop"
41   *
42   * HTML image loop renderer. 
43   * @author Felix Röthenbacher (latest modification by $Author:$)
44   * @version $Revision:$ $Date:$
45   */
46  public class HtmlImageLoopRenderer extends HtmlRenderer {
47      
48      private static final Integer DEFAULT_DELAY = new Integer(1000);
49      private static final Integer DEFAULT_TRANSITION_TIME = new Integer(1000);
50  
51      /**
52       * Add the javascript files needed by Dojo and the custom javascript for
53       * this component
54       */
55      private void encodeJavascript(FacesContext context, UIComponent uiComponent)
56      throws IOException
57      {
58          String javascriptLocation = (String) uiComponent.getAttributes().get(JSFAttr.JAVASCRIPT_LOCATION);
59          DojoUtils.addMainInclude(context, uiComponent, javascriptLocation, new DojoConfig());
60          DojoUtils.addRequire(context, uiComponent, "dojo.event.*");
61          DojoUtils.addRequire(context, uiComponent, "dojo.lfx.*");
62          DojoUtils.addRequire(context, uiComponent, "dojo.html.*");
63  
64          AddResource addResource = AddResourceFactory.getInstance(context);
65  
66          addResource.addJavaScriptAtPosition(context,
67              AddResource.HEADER_BEGIN, HtmlImageLoop.class, "javascript/imageloop.js");
68      }
69  
70      public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException {
71          // Component is rendered in encodeEnd().
72      }
73  
74      public void encodeChildren(FacesContext facesContext, UIComponent uiComponent) throws IOException {
75          // Children are rendered in encodeEnd().
76      }
77  
78      public void encodeEnd(FacesContext facesContext, UIComponent uiComponent) throws IOException {
79          RendererUtils.checkParamValidity(facesContext, uiComponent,
80                  HtmlImageLoop.class);
81          
82          encodeJavascript(facesContext, uiComponent);
83  
84          ResponseWriter writer = facesContext.getResponseWriter();
85          
86          String clientId = uiComponent.getClientId(facesContext);
87          String clientIdImage1 = clientId + ":IMG1";
88          String clientIdImage2 = clientId + ":IMG2";
89          
90          HtmlImageLoop imageLoop = (HtmlImageLoop)uiComponent;
91          
92          Integer delay = imageLoop.getDelay() != null ? imageLoop.getDelay() : DEFAULT_DELAY;
93          Integer minDelay = imageLoop.getMinDelay() != null ? imageLoop.getMinDelay() : DEFAULT_DELAY;
94          Integer maxDelay = imageLoop.getMaxDelay() != null ? imageLoop.getMaxDelay() : DEFAULT_DELAY;
95          Integer transitionTime =
96              imageLoop.getTransitionTime() != null ? imageLoop.getTransitionTime() : DEFAULT_TRANSITION_TIME;
97          Integer width = imageLoop.getWidth();
98          Integer height = imageLoop.getHeight();
99          
100         writer.startElement(HTML.DIV_ELEM, uiComponent);
101         writer.writeAttribute(HTML.ID_ATTR, clientId, null);
102         writer.writeAttribute(HTML.STYLE_ATTR,
103                 "position:relative;" +
104                 "width:" + width + ";" +
105                 "height:" + height, null);
106 
107         writer.startElement(HTML.IMG_ELEM, uiComponent);
108         writer.writeAttribute(HTML.ID_ATTR, clientIdImage1, null);
109         writer.writeAttribute(HTML.SRC_ATTR, "", null);
110         writer.writeAttribute(HTML.STYLE_ATTR,
111                 "position:absolute;" +
112                 "top:0px;" +
113                 "left:0px;" +
114                 "border-style:none", null);
115         writer.endElement(HTML.IMG_ELEM);
116         
117         writer.startElement(HTML.IMG_ELEM, uiComponent);
118         writer.writeAttribute(HTML.ID_ATTR, clientIdImage2, null);
119         writer.writeAttribute(HTML.SRC_ATTR, "", null);
120         writer.writeAttribute(HTML.STYLE_ATTR,
121                 "position:absolute;" +
122                 "top:0px;" +
123                 "left:0px;" +
124                 "border-style:none", null);
125         writer.endElement(HTML.IMG_ELEM);
126         
127         writer.startElement(HTML.SCRIPT_ELEM, uiComponent);
128         writer.writeAttribute(HTML.SCRIPT_LANGUAGE_ATTR, HTML.SCRIPT_LANGUAGE_JAVASCRIPT, null);
129         writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
130         String jsImageArray = getJavascriptImageArray(facesContext, uiComponent);
131         writer.writeText("new ImageLoop('" + clientId + "', " +
132                 jsImageArray + ", " + delay + ", " + minDelay + ", " + maxDelay + ", "+ transitionTime + ")", null);
133         writer.endElement(HTML.SCRIPT_ELEM);
134         
135         writer.endElement(HTML.DIV_ELEM);
136     }
137     
138     private String getJavascriptImageArray(FacesContext facesContext, UIComponent uiComponent) {
139         String jsArray = new String();
140 
141         ImageLoopItemsIterator itemIter = new ImageLoopItemsIterator(uiComponent);
142 
143         while(itemIter.hasNext()) {
144             GraphicItem graphicItem = (GraphicItem)itemIter.next();
145             String url = graphicItem.getUrl();
146             String src = facesContext.getApplication().getViewHandler().getResourceURL(facesContext, url);
147                 
148             if (jsArray.length() == 0)
149                 jsArray += "'" + facesContext.getExternalContext().encodeResourceURL(src) + "'";
150             else
151                 jsArray += ",'" + facesContext.getExternalContext().encodeResourceURL(src) + "'";
152         }
153         return "[" + jsArray + "]";
154     }
155     
156     public boolean getRendersChildren() {
157         return true;
158     }
159 }