Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/sun/facelets/tag/jsf/core/ValueChangeListenerHandler.java


1   /**
2    * Licensed under the Common Development and Distribution License,
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    * 
6    *   http://www.sun.com/cddl/
7    *   
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
11   * implied. See the License for the specific language governing
12   * permissions and limitations under the License.
13   */
14  
15  package com.sun.facelets.tag.jsf.core;
16  
17  import java.io.IOException;
18  
19  import javax.el.ELException;
20  import javax.el.ValueExpression;
21  import javax.faces.FacesException;
22  import javax.faces.component.EditableValueHolder;
23  import javax.faces.component.UIComponent;
24  import javax.faces.event.ValueChangeListener;
25  
26  import com.sun.facelets.FaceletContext;
27  import com.sun.facelets.FaceletException;
28  import com.sun.facelets.tag.TagAttribute;
29  import com.sun.facelets.tag.TagAttributeException;
30  import com.sun.facelets.tag.TagConfig;
31  import com.sun.facelets.tag.TagException;
32  import com.sun.facelets.tag.TagHandler;
33  
34  /**
35   * Register an ValueChangeListener instance on the UIComponent associated with
36   * the closest parent UIComponent custom action.<p/> See <a target="_new"
37   * href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/tlddocs/f/valueChangeListener.html">tag
38   * documentation</a>.
39   * 
40   * @author Jacob Hookom
41   * @version $Id: ValueChangeListenerHandler.java,v 1.2 2005/08/24 04:38:50 jhook Exp $
42   */
43  public final class ValueChangeListenerHandler extends TagHandler {
44  
45      private Class listenerType;
46  
47      private final TagAttribute type;
48  
49      private final TagAttribute binding;
50  
51      public ValueChangeListenerHandler(TagConfig config) {
52          super(config);
53          this.binding = this.getAttribute("binding");
54          this.type = this.getAttribute("type");
55          if (type != null) {
56              try {
57                  this.listenerType = Class.forName(type.getValue());
58              } catch (Exception e) {
59                  throw new TagAttributeException(this.tag, this.type, e);
60              }
61          }
62      }
63  
64      /**
65       * See taglib documentation.
66       * 
67       * @see com.sun.facelets.FaceletHandler#apply(com.sun.facelets.FaceletContext,
68       *      javax.faces.component.UIComponent)
69       */
70      public void apply(FaceletContext ctx, UIComponent parent)
71              throws IOException, FacesException, FaceletException, ELException {
72          if (parent instanceof EditableValueHolder) {
73              // only process if parent was just created
74              if (parent.getParent() == null) {
75                  EditableValueHolder evh = (EditableValueHolder) parent;
76                  ValueChangeListener listener = null;
77                  ValueExpression ve = null;
78                  if (this.binding != null) {
79                      ve = this.binding.getValueExpression(ctx,
80                              ValueChangeListener.class);
81                      listener = (ValueChangeListener) ve.getValue(ctx);
82                  }
83                  if (listener == null && this.listenerType != null) {
84                      try {
85                          listener = (ValueChangeListener) listenerType
86                                  .newInstance();
87                      } catch (Exception e) {
88                          throw new TagAttributeException(this.tag, this.type, e);
89                      }
90                      if (ve != null) {
91                          ve.setValue(ctx, ve);
92                      }
93                  } else {
94                      throw new TagAttributeException(this.tag, this.binding,
95                              "Binding evaluated to null, and there wasn't a 'type' Attribute Specified");
96                  }
97                  evh.addValueChangeListener(listener);
98              }
99          } else {
100             throw new TagException(this.tag,
101                     "Parent is not of type EditableValueHolder, type is: "
102                             + parent);
103         }
104     }
105 
106 }