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.updateactionlistener;
20  
21  import javax.faces.application.Application;
22  import javax.faces.component.ActionSource;
23  import javax.faces.component.UIComponent;
24  import javax.faces.context.FacesContext;
25  import javax.faces.convert.Converter;
26  import javax.faces.webapp.UIComponentTag;
27  import javax.servlet.jsp.JspException;
28  import javax.servlet.jsp.tagext.Tag;
29  import javax.servlet.jsp.tagext.TagSupport;
30  
31  /**
32   * Registers an org.apache.myfaces.custom.updateactionlistener.UpdateActionListener 
33   * at the parent component (which must be an ActionSource). 
34   * 
35   * When the parent's action fires the specified value is evaluated, 
36   * then written into the specified property. 
37   * 
38   * Unless otherwise specified, all attributes accept static values or EL expressions. 
39   * 
40   * JSF 1.2 introduces a "setPropertyActionListener" with the same functionality like this. 
41   *
42   * @JSFJspTag
43   *   name="t:updateActionListener"
44   *   bodyContent="JSP"
45   *   tagHandler="org.apache.myfaces.custom.updateactionlistener.UpdateActionListenerTagHandler"
46   *   
47   * @author Manfred Geiler (latest modification by $Author: skitching $)
48   * @version $Revision: 673833 $ $Date: 2008-07-03 16:58:05 -0500 (Thu, 03 Jul 2008) $
49   */
50  public class UpdateActionListenerTag
51          extends TagSupport
52  {
53      private static final long serialVersionUID = -6916153064327074092L;
54      //private static final Log log = LogFactory.getLog(UpdateActionListenerTag.class);
55      private String _property;
56      private String _value;
57      private String _converter;
58  
59      public UpdateActionListenerTag()
60      {
61      }
62  
63      /**
64       * A value-binding that specifies a property to be updated when 
65       * the parent's action occurs.
66       * 
67       * @JSFJspAttribute
68       *   required="true"
69       */
70      public void setProperty(String property)
71      {
72          _property = property;
73      }
74  
75      /**
76       *  A literal value or value-binding that specifies what 
77       *  will be assigned to the destination specified by the 
78       *  property attribute.
79       * 
80       * @JSFJspAttribute
81       *   required="true"
82       */
83      public void setValue(String value)
84      {
85          _value = value;
86      }
87  
88      /**
89       * The name of a registered Converter object which will be 
90       * invoked to convert the value into an appropriate datatype 
91       * for assigning to the specified property. If not specified 
92       * then an appropriate converter will be selected automatically.
93       * 
94       * @JSFJspAttribute
95       */
96      public void setConverter(String converter)
97      {
98          _converter = converter;
99      }
100 
101     public int doStartTag() throws JspException
102     {
103         if (_property == null) throw new JspException("property attribute not set");
104         if (_value == null) throw new JspException("value attribute not set");
105         if (!UIComponentTag.isValueReference(_property)) throw new JspException("property attribute is no valid value reference: " + _property);
106 
107         //Find parent UIComponentTag
108         UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
109         if (componentTag == null)
110         {
111             throw new JspException("UpdateActionListenerTag has no UIComponentTag ancestor");
112         }
113 
114         if (componentTag.getCreated())
115         {
116             //Component was just created, so we add the Listener
117             UIComponent component = componentTag.getComponentInstance();
118             if (component instanceof ActionSource)
119             {
120                 FacesContext facesContext = FacesContext.getCurrentInstance();
121                 Application application = facesContext.getApplication();
122                 UpdateActionListener al = new UpdateActionListener();
123                 al.setPropertyBinding(application.createValueBinding(_property));
124                 if (UIComponentTag.isValueReference(_value))
125                 {
126                     al.setValueBinding(application.createValueBinding(_value));
127                 }
128                 else
129                 {
130                     al.setValue(_value);
131                 }
132                 if (_converter != null)
133                 {
134                     Converter converter = application.createConverter(_converter);
135                     al.setConverter(converter);
136                 }
137                 ((ActionSource)component).addActionListener(al);
138             }
139             else
140             {
141                 throw new JspException("Component " + component.getId() + " is no ActionSource");
142             }
143         }
144 
145         return Tag.SKIP_BODY;
146     }
147 
148     public void release()
149     {
150         _property = null;
151         _converter = null;
152         _value = null;
153     }
154 
155 }