KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > faces > taglib > ActionListenerTag


1 /*
2  * Copyright 1999-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.cocoon.faces.taglib;
17
18 import org.apache.cocoon.taglib.TagSupport;
19
20 import org.apache.cocoon.faces.FacesUtils;
21 import org.xml.sax.Attributes JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 import javax.faces.component.ActionSource;
25 import javax.faces.component.UIComponent;
26 import javax.faces.event.ActionListener;
27
28 /**
29  * @version CVS $Id: ActionListenerTag.java 46253 2004-09-17 14:36:29Z vgritsenko $
30  */

31 public class ActionListenerTag extends TagSupport {
32
33     private String JavaDoc type;
34
35     public String JavaDoc getType() {
36         return this.type;
37     }
38
39     public void setType(String JavaDoc type) {
40         this.type = type;
41     }
42
43     public int doStartTag(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc atts)
44     throws SAXException JavaDoc {
45         UIComponentTag tag = FacesUtils.findParentUIComponentTag(this);
46         if (tag == null) {
47             throw new SAXException JavaDoc("Tag <" + getClass().getName() + "> have to be nested within a UIComponentTag");
48         }
49
50         if (!tag.getCreated()) {
51             return 0;
52         }
53
54         UIComponent component = tag.getComponentInstance();
55         if (component == null) {
56             throw new SAXException JavaDoc("Parent tag <" + tag.getClass().getName() + "> has no component instance");
57         }
58
59         if (component instanceof ActionSource) {
60             String JavaDoc clazz = (String JavaDoc) tag.createValueBinding(this.type).getValue(tag.getFacesContext());
61             ActionListener handler = null;
62             try {
63                 handler = (ActionListener) Class.forName(clazz).newInstance();
64             } catch (Exception JavaDoc e) {
65                 throw new SAXException JavaDoc("Tag <" + tag.getClass().getName() + "> could not create action listener <" + clazz + ">", e);
66             }
67             ((ActionSource)component).addActionListener(handler);
68         }
69
70         return SKIP_BODY;
71     }
72
73     public void recycle() {
74         super.recycle();
75         this.type = null;
76     }
77 }
78
Popular Tags