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