KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > tag > jsf > core > ActionListenerHandler


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.tag.jsf.core;
16
17 import java.io.IOException JavaDoc;
18
19 import javax.el.ELException;
20 import javax.el.ValueExpression;
21 import javax.faces.FacesException;
22 import javax.faces.component.ActionSource;
23 import javax.faces.component.UIComponent;
24 import javax.faces.event.ActionListener;
25
26 import com.sun.facelets.FaceletContext;
27 import com.sun.facelets.FaceletException;
28 import com.sun.facelets.tag.TagAttribute;
29 import com.sun.facelets.tag.TagAttributeException;
30 import com.sun.facelets.tag.TagConfig;
31 import com.sun.facelets.tag.TagException;
32 import com.sun.facelets.tag.TagHandler;
33
34 /**
35  * Register an ActionListener instance on the UIComponent associated with the
36  * closest parent UIComponent custom action. <p/> See <a target="_new"
37  * HREF="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/tlddocs/f/actionListener.html">tag
38  * documentation</a>.
39  *
40  * @see javax.faces.event.ActionListener
41  * @see javax.faces.component.ActionSource
42  * @author Jacob Hookom
43  * @version $Id: ActionListenerHandler.java,v 1.3 2006/02/23 02:54:52 jhook Exp $
44  */

45 public final class ActionListenerHandler extends TagHandler {
46
47     private Class JavaDoc listenerType;
48
49     private final TagAttribute type;
50
51     private final TagAttribute binding;
52
53     /**
54      * @param config
55      */

56     public ActionListenerHandler(TagConfig config) {
57         super(config);
58         this.binding = this.getAttribute("binding");
59         this.type = this.getAttribute("type");
60         if (type != null) {
61             if (!type.isLiteral()) {
62                 throw new TagAttributeException(this.tag, this.type, "Must be literal");
63             }
64             try {
65                 this.listenerType = Class.forName(type.getValue());
66             } catch (Exception JavaDoc e) {
67                 throw new TagAttributeException(this.tag, this.type, e);
68             }
69         }
70     }
71
72     /*
73      * (non-Javadoc)
74      *
75      * @see com.sun.facelets.FaceletHandler#apply(com.sun.facelets.FaceletContext,
76      * javax.faces.component.UIComponent)
77      */

78     public void apply(FaceletContext ctx, UIComponent parent)
79             throws IOException JavaDoc, FacesException, FaceletException, ELException {
80         if (parent instanceof ActionSource) {
81             // only process if parent was just created
82
if (parent.getParent() == null) {
83                 ActionSource src = (ActionSource) parent;
84                 ActionListener listener = null;
85                 ValueExpression ve = null;
86                 if (this.binding != null) {
87                     ve = this.binding.getValueExpression(ctx,
88                             ActionListener.class);
89                     listener = (ActionListener) ve.getValue(ctx);
90                 }
91                 if (listener == null) {
92                     try {
93                         listener = (ActionListener) listenerType.newInstance();
94                     } catch (Exception JavaDoc e) {
95                         throw new TagAttributeException(this.tag, this.type, e.getCause());
96                     }
97                     if (ve != null) {
98                         ve.setValue(ctx, ve);
99                     }
100                 }
101                 src.addActionListener(listener);
102             }
103         } else {
104             throw new TagException(this.tag,
105                     "Parent is not of type ActionSource, type is: " + parent);
106         }
107     }
108 }
109
Popular Tags