KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > tag > jsf > ActionSourceRule


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;
16
17 import javax.faces.component.ActionSource;
18 import javax.faces.component.ActionSource2;
19 import javax.faces.event.ActionEvent;
20 import javax.faces.event.MethodExpressionActionListener;
21
22 import com.sun.facelets.FaceletContext;
23 import com.sun.facelets.el.LegacyMethodBinding;
24 import com.sun.facelets.tag.TagAttribute;
25 import com.sun.facelets.tag.Metadata;
26 import com.sun.facelets.tag.MetaRule;
27 import com.sun.facelets.tag.MetadataTarget;
28 import com.sun.facelets.util.FacesAPI;
29
30 /**
31  *
32  * @author Jacob Hookom
33  * @version $Id: ActionSourceRule.java,v 1.3 2006/03/29 04:10:02 jhook Exp $
34  */

35 final class ActionSourceRule extends MetaRule {
36
37     public final static Class JavaDoc[] ACTION_SIG = new Class JavaDoc[0];
38
39     public final static Class JavaDoc[] ACTION_LISTENER_SIG = new Class JavaDoc[] { ActionEvent.class };
40
41     final static class ActionMapper extends Metadata {
42
43         private final TagAttribute attr;
44
45         public ActionMapper(TagAttribute attr) {
46             this.attr = attr;
47         }
48
49         public void applyMetadata(FaceletContext ctx, Object JavaDoc instance) {
50             ((ActionSource) instance).setAction(new LegacyMethodBinding(
51                     this.attr.getMethodExpression(ctx, String JavaDoc.class,
52                             ActionSourceRule.ACTION_SIG)));
53         }
54     }
55
56     final static class ActionMapper2 extends Metadata {
57
58         private final TagAttribute attr;
59
60         public ActionMapper2(TagAttribute attr) {
61             this.attr = attr;
62         }
63
64         public void applyMetadata(FaceletContext ctx, Object JavaDoc instance) {
65             ((ActionSource2) instance).setActionExpression(this.attr
66                     .getMethodExpression(ctx, String JavaDoc.class,
67                             ActionSourceRule.ACTION_SIG));
68         }
69
70     }
71
72     final static class ActionListenerMapper extends Metadata {
73
74         private final TagAttribute attr;
75
76         public ActionListenerMapper(TagAttribute attr) {
77             this.attr = attr;
78         }
79
80         public void applyMetadata(FaceletContext ctx, Object JavaDoc instance) {
81             ((ActionSource) instance)
82                     .setActionListener(new LegacyMethodBinding(this.attr
83                             .getMethodExpression(ctx, null,
84                                     ActionSourceRule.ACTION_LISTENER_SIG)));
85         }
86
87     }
88
89     final static class ActionListenerMapper2 extends Metadata {
90
91         private final TagAttribute attr;
92
93         public ActionListenerMapper2(TagAttribute attr) {
94             this.attr = attr;
95         }
96
97         public void applyMetadata(FaceletContext ctx, Object JavaDoc instance) {
98             ((ActionSource) instance)
99                     .addActionListener(new MethodExpressionActionListener(
100                             this.attr.getMethodExpression(ctx, null,
101                                     ActionSourceRule.ACTION_LISTENER_SIG)));
102
103         }
104
105     }
106
107     public final static ActionSourceRule Instance = new ActionSourceRule();
108
109     public ActionSourceRule() {
110         super();
111     }
112
113     public Metadata applyRule(String JavaDoc name, TagAttribute attribute,
114             MetadataTarget meta) {
115         if (meta.isTargetInstanceOf(ActionSource.class)) {
116
117             boolean elSupport = FacesAPI.getComponentVersion(meta
118                     .getTargetClass()) >= 12;
119
120             if ("action".equals(name)) {
121                 if (elSupport && meta.isTargetInstanceOf(ActionSource2.class)) {
122                     return new ActionMapper2(attribute);
123                 } else {
124                     return new ActionMapper(attribute);
125                 }
126             }
127
128             if ("actionListener".equals(name)) {
129                 if (elSupport) {
130                     return new ActionListenerMapper2(attribute);
131                 } else {
132                     return new ActionListenerMapper(attribute);
133                 }
134             }
135         }
136         return null;
137     }
138 }
139
Popular Tags