KickJava   Java API By Example, From Geeks To Geeks.

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


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.EditableValueHolder;
25 import javax.faces.component.UIComponent;
26 import javax.faces.event.ValueChangeListener;
27
28 /**
29  * @version CVS $Id: ValueChangeListenerTag.java 46253 2004-09-17 14:36:29Z vgritsenko $
30  */

31 public class ValueChangeListenerTag 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
46         UIComponentTag tag = FacesUtils.findParentUIComponentTag(this);
47         if (tag == null) {
48             throw new SAXException JavaDoc("Tag <" + getClass().getName() + "> have to be nested within a UIComponentTag");
49         }
50
51         if (!tag.getCreated()) {
52             return 0;
53         }
54
55         UIComponent component = tag.getComponentInstance();
56         if (component == null) {
57             throw new SAXException JavaDoc("Parent tag <" + tag.getClass().getName() + "> has no component instance");
58         }
59
60         if (component instanceof EditableValueHolder) {
61             String JavaDoc clazz = (String JavaDoc) FacesUtils.evaluate(tag.getFacesContext(), this.type);
62             ValueChangeListener handler = null;
63             try {
64                 handler = (ValueChangeListener) Class.forName(clazz).newInstance();
65             } catch (Exception JavaDoc e) {
66                 throw new SAXException JavaDoc("Tag <" + tag.getClass().getName() + "> could not create action listener <" + clazz + ">", e);
67             }
68             ((EditableValueHolder)component).addValueChangeListener(handler);
69         }
70
71         return SKIP_BODY;
72     }
73
74     public void recycle() {
75         super.recycle();
76         this.type = null;
77     }
78 }
79
Popular Tags