01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.faces.taglib;
18:
19: import org.apache.cocoon.taglib.TagSupport;
20:
21: import org.apache.cocoon.faces.FacesUtils;
22: import org.xml.sax.Attributes;
23: import org.xml.sax.SAXException;
24:
25: import javax.faces.component.EditableValueHolder;
26: import javax.faces.component.UIComponent;
27: import javax.faces.event.ValueChangeListener;
28:
29: /**
30: * @version CVS $Id: ValueChangeListenerTag.java 433543 2006-08-22 06:22:54Z crossley $
31: */
32: public class ValueChangeListenerTag extends TagSupport {
33:
34: private String type;
35:
36: public String getType() {
37: return this .type;
38: }
39:
40: public void setType(String type) {
41: this .type = type;
42: }
43:
44: public int doStartTag(String namespaceURI, String localName,
45: String qName, Attributes atts) throws SAXException {
46:
47: UIComponentTag tag = FacesUtils.findParentUIComponentTag(this );
48: if (tag == null) {
49: throw new SAXException("Tag <" + getClass().getName()
50: + "> have to be nested within a UIComponentTag");
51: }
52:
53: if (!tag.getCreated()) {
54: return 0;
55: }
56:
57: UIComponent component = tag.getComponentInstance();
58: if (component == null) {
59: throw new SAXException("Parent tag <"
60: + tag.getClass().getName()
61: + "> has no component instance");
62: }
63:
64: if (component instanceof EditableValueHolder) {
65: String clazz = (String) FacesUtils.evaluate(tag
66: .getFacesContext(), this .type);
67: ValueChangeListener handler = null;
68: try {
69: handler = (ValueChangeListener) Class.forName(clazz)
70: .newInstance();
71: } catch (Exception e) {
72: throw new SAXException("Tag <"
73: + tag.getClass().getName()
74: + "> could not create action listener <"
75: + clazz + ">", e);
76: }
77: ((EditableValueHolder) component)
78: .addValueChangeListener(handler);
79: }
80:
81: return SKIP_BODY;
82: }
83:
84: public void recycle() {
85: super.recycle();
86: this.type = null;
87: }
88: }
|