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.shared_tomahawk.renderkit.html.HtmlTextRendererBase;
23  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
24  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
25  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
26  import org.apache.myfaces.renderkit.html.util.AddResource;
27  import org.apache.myfaces.custom.ajax.api.AjaxRenderer;
28  import org.apache.myfaces.custom.ajax.util.AjaxRendererUtils;
29  import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  
33  import javax.faces.context.FacesContext;
34  import javax.faces.context.ResponseWriter;
35  import javax.faces.component.UIComponent;
36  import java.io.IOException;
37  
38  /**
39   * 
40   * @JSFRenderer
41   *   renderKitId = "HTML_BASIC" 
42   *   family = "javax.faces.Input"
43   *   type = "org.apache.myfaces.InputTextAjax"
44   *
45   * User: treeder
46   * Date: Oct 28, 2005
47   * Time: 7:54:08 PM
48   */
49  public class HtmlInputTextAjaxRenderer extends HtmlTextRendererBase implements AjaxRenderer
50  {
51      private static final Log log = LogFactory.getLog(HtmlInputTextAjaxRenderer.class);
52      private static final String JAVASCRIPT_ENCODED = "org.apache.myfaces.custom.inputAjax.HtmlInputTextAjax.JAVASCRIPT_ENCODED";
53  
54  
55      /**
56       * Encodes any stand-alone javascript functions that are needed.
57       * Uses either the extension filter, or a
58       * user-supplied location for the javascript files.
59       *
60       * @param context   FacesContext
61       * @param component UIComponent
62       * @throws java.io.IOException
63       */
64      private void encodeJavascript(FacesContext context, UIComponent component) throws IOException
65      {
66          HtmlInputTextAjax comp = (HtmlInputTextAjax) component;
67  
68          AddResource addResource = AddResourceFactory.getInstance(context);
69  
70          AjaxRendererUtils.addPrototypeScript(context, component, addResource);
71  
72          ResponseWriter out = context.getResponseWriter();
73          AjaxRendererUtils.writeAjaxScript(context, out, comp);
74  
75          context.getExternalContext().getRequestMap().put(JAVASCRIPT_ENCODED, Boolean.TRUE);
76      }
77  
78  
79  
80  
81      public void encodeEnd(FacesContext context, UIComponent component) throws IOException
82      {
83          log.debug("encodeEnd in HtmlInputTextAjaxRenderer");
84          RendererUtils.checkParamValidity(context, component, HtmlInputTextAjax.class);
85  
86          if (HtmlRendererUtils.isDisplayValueOnly(component) || isDisabled(context, component))
87          {
88              super.encodeEnd(context, component);
89              return;
90          }
91  
92          String clientId = component.getClientId(context);
93          String submitFunctionStart = AjaxRendererUtils.JS_MYFACES_NAMESPACE + "ajaxSubmit1('" + clientId + "');";
94          HtmlInputTextAjax comp = (HtmlInputTextAjax) component;
95          String loadingStyleClass = AjaxRendererUtils.STYLECLASS_LOADER;
96          //comp.setStyleClass(comp.getStyleClass() == null ? loadingStyleClass : comp.getStyleClass() + ";" + loadingStyleClass);
97          String styleClass = comp.getStyleClass();
98          if(styleClass != null && styleClass.length() > 0)
99          {
100             comp.setStyleClass(loadingStyleClass + " " + styleClass);
101         }
102         else
103         {
104             comp.setStyleClass(loadingStyleClass);
105         }
106 
107         if(!comp.getShowOkButton().booleanValue()){
108             // then submit on change
109             //comp.setOnchange(comp.getOnchange() == null ? submitFunctionStart : comp.getOnchange() + ";" + submitFunctionStart);
110             String onchange = comp.getOnchange();
111             if(onchange != null && onchange.length() > 0) 
112             {
113                 if(!onchange.endsWith(";")) 
114                 {
115                     onchange += ";";
116                 }
117                 comp.setOnchange(submitFunctionStart+onchange);
118             } 
119             else 
120             {
121                 comp.setOnchange(submitFunctionStart);
122             }
123         }
124         this.encodeJavascript(context, component);
125         super.encodeEnd(context, component);
126 
127         ResponseWriter writer = context.getResponseWriter();
128         if(comp.getShowOkButton() != null && comp.getShowOkButton().booleanValue()){
129             // then show an ok button
130             writer.startElement(HTML.INPUT_ELEM, comp);
131             writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_BUTTON, null);
132             String okText = comp.getOkText() != null ? comp.getOkText() : "Ok";
133             writer.writeAttribute(HTML.VALUE_ATTR, okText, null);
134             writer.writeAttribute(HTML.ONCLICK_ATTR, submitFunctionStart, null);
135             writer.endElement(HTML.INPUT_TYPE_BUTTON);
136             if(comp.getShowCancelButton() != null && comp.getShowCancelButton().booleanValue()){
137                 // put a space here
138                 writer.write(" ");
139                 writer.startElement(HTML.ANCHOR_ELEM, comp);
140                 writer.writeAttribute(HTML.HREF_ATTR, "javascript:" + AjaxRendererUtils.JS_MYFACES_NAMESPACE + "clearById('" + clientId + "');", null);
141                 String cancelText = comp.getCancelText() != null ? comp.getCancelText() : "cancel";
142                 writer.writeText(cancelText, null);
143                 writer.endElement(HTML.ANCHOR_ELEM);
144             }
145         }
146 
147         // output a span for error messages
148         /*writer.startElement(HTML.SPAN_ELEM, component);
149         writer.writeAttribute(HTML.ID_ATTR, "msgFor_" + clientId, null);
150         writer.writeText(" ", null);
151         writer.endElement(HTML.SPAN_ELEM);*/
152 
153 
154     }
155 
156     public void encodeAjax(FacesContext context, UIComponent component) throws IOException
157     {
158         log.debug("encodeAjax in HtmlInputTextAjaxRenderer");
159         AjaxRendererUtils.encodeAjax(context, component);
160     }
161 }