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.inputAjax;
20  
21  import org.apache.myfaces.renderkit.html.ext.HtmlButtonRenderer;
22  import org.apache.myfaces.renderkit.html.util.AddResource;
23  import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
24  import org.apache.myfaces.custom.ajax.util.AjaxRendererUtils;
25  import org.apache.myfaces.custom.ajax.api.AjaxRenderer;
26  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
27  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  import javax.faces.context.FacesContext;
32  import javax.faces.context.ResponseWriter;
33  import javax.faces.component.UIComponent;
34  import java.io.IOException;
35  
36  /**
37   * 
38   * @JSFRenderer
39   *   renderKitId = "HTML_BASIC" 
40   *   family = "javax.faces.Command"
41   *   type = "org.apache.myfaces.CommandButtonAjax"
42   *
43   * User: Travis Reeder
44   * Date: Mar 22, 2006
45   * Time: 4:38:13 PM
46   */
47  public class HtmlCommandButtonAjaxRenderer extends HtmlButtonRenderer implements AjaxRenderer
48  {
49      private static final Log log = LogFactory.getLog(HtmlCommandButtonAjaxRenderer.class);
50      private static final String JAVASCRIPT_ENCODED = "org.apache.myfaces.custom.inputAjax.HtmlCommandButtonAjax.JAVASCRIPT_ENCODED";
51  
52  
53      /**
54       * Encodes any stand-alone javascript functions that are needed.
55       * Uses either the extension filter, or a
56       * user-supplied location for the javascript files.
57       *
58       * @param context   FacesContext
59       * @param component UIComponent
60       * @throws java.io.IOException
61       */
62      private void encodeJavascript(FacesContext context, UIComponent component) throws IOException
63      {
64          HtmlCommandButtonAjax comp = (HtmlCommandButtonAjax) component;
65  
66          AddResource addResource = AddResourceFactory.getInstance(context);
67  
68          AjaxRendererUtils.addPrototypeScript(context, component, addResource);
69  
70          ResponseWriter out = context.getResponseWriter();
71          AjaxRendererUtils.writeAjaxScript(context, out, comp);
72  
73          context.getExternalContext().getRequestMap().put(JAVASCRIPT_ENCODED, Boolean.TRUE);
74      }
75  
76  
77  
78  
79      public void encodeEnd(FacesContext context, UIComponent component) throws IOException
80      {
81          log.debug("encodeEnd in HtmlCommandButtonAjaxRenderer");
82          RendererUtils.checkParamValidity(context, component, HtmlCommandButtonAjax.class);
83  
84          if (HtmlRendererUtils.isDisplayValueOnly(component) || isDisabled(context, component))
85          {
86              super.encodeEnd(context, component);
87              return;
88          }
89  
90          this.encodeJavascript(context, component);
91          super.encodeEnd(context, component);
92          // now write loading image
93          AjaxRendererUtils.writeLoadingImage(context, component);
94      }
95  
96  
97      protected StringBuffer buildOnClick(UIComponent uiComponent, FacesContext facesContext, ResponseWriter writer) throws IOException {
98          String clientId = uiComponent.getClientId(facesContext);
99          String submitFunctionStart = AjaxRendererUtils.JS_MYFACES_NAMESPACE + "ajaxSubmit3('" + clientId + "');";
100 
101         StringBuffer buf = super.buildOnClick(uiComponent, facesContext, writer);
102 
103         if(buf.length()!=0 && !(buf.charAt(buf.length()-1)==';'))
104         {
105             buf.append(";");
106         }
107         buf.append(submitFunctionStart);
108 
109         return buf;
110     }
111 
112     public void encodeAjax(FacesContext context, UIComponent component) throws IOException
113     {
114         log.debug("encodeAjax in HtmlCommandButtonAjaxRenderer");
115         AjaxRendererUtils.encodeAjax(context, component);
116     }
117 
118 }