KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > updateactionlistener > UpdateActionListenerTag


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.custom.updateactionlistener;
17
18 import javax.faces.application.Application;
19 import javax.faces.component.ActionSource;
20 import javax.faces.component.UIComponent;
21 import javax.faces.context.FacesContext;
22 import javax.faces.convert.Converter;
23 import javax.faces.webapp.UIComponentTag;
24 import javax.servlet.jsp.JspException JavaDoc;
25 import javax.servlet.jsp.tagext.Tag JavaDoc;
26 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
27
28 /**
29  * @author Manfred Geiler (latest modification by $Author: matze $)
30  * @version $Revision: 1.3 $ $Date: 2004/10/13 11:50:59 $
31  * $Log: UpdateActionListenerTag.java,v $
32  * Revision 1.3 2004/10/13 11:50:59 matze
33  * renamed packages to org.apache
34  *
35  * Revision 1.2 2004/07/01 21:53:11 mwessendorf
36  * ASF switch
37  *
38  * Revision 1.1 2004/03/31 12:15:28 manolito
39  * custom component refactoring
40  *
41  */

42 public class UpdateActionListenerTag
43         extends TagSupport JavaDoc
44 {
45     //private static final Log log = LogFactory.getLog(UpdateActionListenerTag.class);
46

47     private String JavaDoc _property;
48     private String JavaDoc _value;
49     private String JavaDoc _converter;
50
51     public UpdateActionListenerTag()
52     {
53     }
54
55     public void setProperty(String JavaDoc property)
56     {
57         _property = property;
58     }
59
60     public void setValue(String JavaDoc value)
61     {
62         _value = value;
63     }
64
65     public void setConverter(String JavaDoc converter)
66     {
67         _converter = converter;
68     }
69
70     public int doStartTag() throws JspException JavaDoc
71     {
72         if (_property == null) throw new JspException JavaDoc("property attribute not set");
73         if (_value == null) throw new JspException JavaDoc("value attribute not set");
74         if (!UIComponentTag.isValueReference(_property)) throw new JspException JavaDoc("property attribute is no valid value reference: " + _property);
75
76         //Find parent UIComponentTag
77
UIComponentTag componentTag = UIComponentTag.getParentUIComponentTag(pageContext);
78         if (componentTag == null)
79         {
80             throw new JspException JavaDoc("UpdateActionListenerTag has no UIComponentTag ancestor");
81         }
82
83         if (componentTag.getCreated())
84         {
85             //Component was just created, so we add the Listener
86
UIComponent component = componentTag.getComponentInstance();
87             if (component instanceof ActionSource)
88             {
89                 FacesContext facesContext = FacesContext.getCurrentInstance();
90                 Application application = facesContext.getApplication();
91                 UpdateActionListener al = new UpdateActionListener();
92                 al.setPropertyBinding(application.createValueBinding(_property));
93                 if (UIComponentTag.isValueReference(_value))
94                 {
95                     al.setValueBinding(application.createValueBinding(_value));
96                 }
97                 else
98                 {
99                     al.setValue(_value);
100                 }
101                 if (_converter != null)
102                 {
103                     Converter converter = application.createConverter(_converter);
104                     al.setConverter(converter);
105                 }
106                 ((ActionSource)component).addActionListener(al);
107             }
108             else
109             {
110                 throw new JspException JavaDoc("Component " + component.getId() + " is no ActionSource");
111             }
112         }
113
114         return Tag.SKIP_BODY;
115     }
116 }
117
Popular Tags