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 java.io.IOException;
23  import java.util.Map;
24  
25  import javax.faces.component.EditableValueHolder;
26  import javax.faces.context.FacesContext;
27  import javax.faces.render.Renderer;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.myfaces.component.html.ext.HtmlSelectBooleanCheckbox;
32  import org.apache.myfaces.custom.ajax.AjaxCallbacks;
33  import org.apache.myfaces.custom.ajax.api.AjaxRenderer;
34  import org.apache.myfaces.custom.ajax.api.DeprecatedAjaxComponent;
35  
36  /**
37   * Extends standard selectBooleanCheckbox by user role support.
38   * 
39   * @JSFComponent
40   *   name = "s:selectBooleanCheckboxAjax"
41   *   class = "org.apache.myfaces.custom.inputAjax.HtmlSelectBooleanCheckboxAjax"
42   *   tagClass = "org.apache.myfaces.custom.inputAjax.HtmlSelectBooleanCheckboxAjaxTag"
43   *   
44   * User: treeder
45   * Date: Nov 21, 2005
46   * Time: 8:47:27 AM
47   */
48  public abstract class AbstractHtmlSelectBooleanCheckboxAjax extends HtmlSelectBooleanCheckbox
49          implements DeprecatedAjaxComponent, AjaxCallbacks
50  {
51  
52      private static final Log log = LogFactory.getLog(AbstractHtmlSelectBooleanCheckboxAjax.class);
53      public static final String COMPONENT_TYPE = "org.apache.myfaces.SelectBooleanCheckboxAjax";
54      public static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.CheckboxAjax";
55  
56      public AbstractHtmlSelectBooleanCheckboxAjax()
57      {
58          super();
59          setRendererType(DEFAULT_RENDERER_TYPE);
60      }
61  
62      /**
63       * Decode ajax request and apply value changes
64       *
65       * @param context
66       */
67      public void decodeAjax(FacesContext context)
68      {
69          log.debug("entering HtmlSelectBooleanCheckboxAjax.decodeAjax");
70  
71          // Handled differently
72          Map requestParams = context.getExternalContext().getRequestParameterMap();
73          String checkedStr = (String) requestParams.get("elvalue");
74          log.debug("elvalue: " + checkedStr);
75          boolean checked = Boolean.valueOf(checkedStr).booleanValue();
76          if (checked)
77          {
78              processDecodes(context);
79          }
80          else
81          {
82              // needs special handling
83              decodeUISelectBoolean(context, this);
84          }
85          //System.out.println("SUBMITTED VALUE: " + ((EditableValueHolder) this).getSubmittedValue());
86          processValidators(context);
87          processUpdates(context);
88          //context.getViewRoot().processApplication(context);
89  
90          //String elname = (String) requestParams.get("elname");
91          //String elvalue = (String) requestParams.get("elvalue");
92  
93          if (log.isDebugEnabled())
94          {
95              Object valOb = this.getValue();
96              log.debug("value object after decodeAjax: " + valOb);
97          }
98  
99      }
100 
101     private void decodeUISelectBoolean(FacesContext facesContext, AbstractHtmlSelectBooleanCheckboxAjax component)
102     {
103         if (!(component instanceof EditableValueHolder))
104         {
105             throw new IllegalArgumentException("Component "
106                     + component.getClientId(facesContext)
107                     + " is not an EditableValueHolder");
108         }
109         ((EditableValueHolder) component).setSubmittedValue(Boolean.FALSE);
110 
111     }
112 
113     public void encodeAjax(FacesContext context) throws IOException
114     {
115         log.debug("entering HtmlSelectBooleanCheckboxAjax.encodeAjax");
116         if (context == null) throw new NullPointerException("context");
117         if (!isRendered()) return;
118         Renderer renderer = getRenderer(context);
119 
120         if (renderer != null && renderer instanceof AjaxRenderer)
121         {
122             ((AjaxRenderer) renderer).encodeAjax(context, this);
123         }
124         else
125         {
126             log.warn("Renderer does not implement AjaxRenderer! " + renderer);
127         }
128     }
129 
130     /**
131      * Javascript method to call on successful ajax update
132      * 
133      * @JSFProperty
134      */
135     public abstract String getOnSuccess();
136 
137     /**
138      * Javascript method to call on failed ajax update
139      * 
140      * @JSFProperty
141      */
142     public abstract String getOnFailure();
143 
144     /**
145      * Javascript method to call on start of ajax update
146      * 
147      * @JSFProperty
148      */
149     public abstract String getOnStart();
150 
151     /**
152      * Image to show instead of checkox when boolean value is true
153      * 
154      * @JSFProperty
155      */
156     public abstract String getOnImage();
157 
158     /**
159      * Image to show instead of checkox when boolean value is false
160      * 
161      * @JSFProperty
162      */
163     public abstract String getOffImage();
164 
165 }