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 java.io.IOException;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import javax.faces.component.UIComponent;
26  import javax.faces.context.FacesContext;
27  import javax.faces.context.ResponseWriter;
28  import javax.servlet.http.HttpServletRequest;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.myfaces.renderkit.html.util.AddResource;
33  import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
34  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
35  import org.apache.myfaces.custom.ajax.api.AjaxRenderer;
36  import org.apache.myfaces.custom.ajax.util.AjaxRendererUtils;
37  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
38  import org.apache.myfaces.renderkit.html.ext.HtmlCheckboxRenderer;
39  
40  /**
41   * For onSuccess, onFailure, onStart functions, they should accept (elname, elvalue) attributes for the specific item
42   * <p/>
43   * 
44   * @JSFRenderer
45   *   renderKitId = "HTML_BASIC" 
46   *   family = "javax.faces.SelectMany"
47   *   type = "org.apache.myfaces.CheckboxAjax"
48   *
49   * User: treeder
50   * Date: Oct 20, 2005
51   * Time: 3:28:04 PM
52   */
53  public class HtmlSelectManyCheckboxAjaxRenderer extends HtmlCheckboxRenderer implements AjaxRenderer
54  {
55      private static final Log log = LogFactory.getLog(HtmlSelectManyCheckboxAjaxRenderer.class);
56      private static final String JAVASCRIPT_ENCODED = "org.apache.myfaces.custom.inputAjax.HtmlSelectManyCheckbox.JAVASCRIPT_ENCODED";
57  
58  
59      /**
60       * Encodes any stand-alone javascript functions that are needed.
61       * Uses either the extension filter, or a
62       * user-supplied location for the javascript files.
63       *
64       * @param context   FacesContext
65       * @param component UIComponent
66       * @throws java.io.IOException
67       */
68      private void encodeJavascript(FacesContext context, UIComponent component) throws IOException
69      {
70  
71          HtmlSelectManyCheckboxAjax selectManyCheckbox = (HtmlSelectManyCheckboxAjax) component;
72  
73          AddResource addResource = AddResourceFactory.getInstance(context);
74  
75          AjaxRendererUtils.addPrototypeScript(context, component, addResource);
76  
77          ResponseWriter out = context.getResponseWriter();
78  
79          String extraParams =("&checked=\" + el.checked + \"");
80          AjaxRendererUtils.writeAjaxScript(context, out, selectManyCheckbox, extraParams);
81  
82          context.getExternalContext().getRequestMap().put(JAVASCRIPT_ENCODED, Boolean.TRUE);
83      }
84  
85  
86      public void encodeEnd(FacesContext context, UIComponent component) throws IOException
87      {
88          RendererUtils.checkParamValidity(context, component, HtmlSelectManyCheckboxAjax.class);
89  
90          if (HtmlRendererUtils.isDisplayValueOnly(component) || isDisabled(context, component))
91          {
92              super.encodeEnd(context, component);
93              return;
94          }
95  
96          String clientId = component.getClientId(context);
97  
98          HtmlSelectManyCheckboxAjax selectManyCheckbox = (HtmlSelectManyCheckboxAjax) component;
99  
100         // allow for user defined onclick's as well
101         String onClick = selectManyCheckbox.getOnclick();
102         if(onClick == null){
103             onClick = "";
104         }
105         // todo: this was doublling up, so took out the + onclick below, need a way for user onclicks to work still
106         onClick = AjaxRendererUtils.JS_MYFACES_NAMESPACE + "ajaxSubmit2(this, '" + clientId + "'); "; // + onClick;
107         selectManyCheckbox.setOnclick(onClick);
108 
109         this.encodeJavascript(context, component);
110 
111         super.encodeEnd(context, component);
112         AjaxRendererUtils.writeLoadingImage(context, component);
113 
114     }
115 
116     public void encodeAjax(FacesContext context, UIComponent component) throws IOException
117     {
118         log.debug("encodeAjax in HtmlSelectManyCheckboxAjaxRenderer");
119         // check for request type (portlet support)
120         //HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
121         Map extraReturnAttributes = new HashMap();
122         //extraReturnAttributes.put("checked", request.getParameter("checked"));
123         extraReturnAttributes.put("checked", context.getExternalContext().getRequestParameterMap().get("checked"));
124         extraReturnAttributes.put("eltype", "checkbox");
125         AjaxRendererUtils.encodeAjax(context, component, extraReturnAttributes);
126 
127 
128     }
129 
130 
131 
132 }