KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > taglib > core > ActionListenerTag


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.taglib.core;
17
18 import org.apache.myfaces.util.ClassUtils;
19
20 import javax.faces.component.ActionSource;
21 import javax.faces.component.UIComponent;
22 import javax.faces.context.FacesContext;
23 import javax.faces.el.ValueBinding;
24 import javax.faces.event.ActionListener;
25 import javax.faces.webapp.UIComponentTag;
26 import javax.servlet.jsp.JspException JavaDoc;
27 import javax.servlet.jsp.tagext.Tag JavaDoc;
28 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
29
30 /**
31  * @author Manfred Geiler (latest modification by $Author: matze $)
32  * @version $Revision: 1.19 $ $Date: 2004/10/13 11:51:00 $
33  */

34 public class ActionListenerTag
35         extends TagSupport JavaDoc
36 {
37     //private static final Log log = LogFactory.getLog(ActionListenerTag.class);
38

39     private String JavaDoc _type = null;
40
41     public ActionListenerTag()
42     {
43     }
44
45     public void setType(String JavaDoc type)
46     {
47         _type = type;
48     }
49
50
51     public int doStartTag() throws JspException JavaDoc
52     {
53         if (_type == null)
54         {
55             throw new JspException JavaDoc("type attribute not set");
56         }
57
58         //Find parent UIComponentTag
59
UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
60         if (componentTag == null)
61         {
62             throw new JspException JavaDoc("ActionListenerTag has no UIComponentTag ancestor");
63         }
64
65         if (componentTag.getCreated())
66         {
67             //Component was just created, so we add the Listener
68
UIComponent component = componentTag.getComponentInstance();
69             if (component instanceof ActionSource)
70             {
71                 String JavaDoc className;
72                 if (UIComponentTag.isValueReference(_type))
73                 {
74                     FacesContext facesContext = FacesContext.getCurrentInstance();
75                     ValueBinding vb = facesContext.getApplication().createValueBinding(_type);
76                     className = (String JavaDoc)vb.getValue(facesContext);
77                 }
78                 else
79                 {
80                     className = _type;
81                 }
82                 ActionListener al = (ActionListener)ClassUtils.newInstance(className);
83                 ((ActionSource)component).addActionListener(al);
84             }
85             else
86             {
87                 throw new JspException JavaDoc("Component " + component.getId() + " is no ActionSource");
88             }
89         }
90
91         return Tag.SKIP_BODY;
92     }
93 }
94
Popular Tags