KickJava   Java API By Example, From Geeks To Geeks.

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


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.EditableValueHolder;
18 import javax.faces.component.UIComponent;
19 import javax.faces.context.FacesContext;
20 import javax.faces.event.MethodExpressionValueChangeListener;
21 import javax.faces.event.ValueChangeEvent;
22 import javax.faces.validator.MethodExpressionValidator;
23
24 import com.sun.facelets.FaceletContext;
25 import com.sun.facelets.el.LegacyMethodBinding;
26 import com.sun.facelets.tag.TagAttribute;
27 import com.sun.facelets.tag.Metadata;
28 import com.sun.facelets.tag.MetaRule;
29 import com.sun.facelets.tag.MetadataTarget;
30 import com.sun.facelets.util.FacesAPI;
31
32 /**
33  *
34  * @author Jacob Hookom
35  * @version $Id: EditableValueHolderRule.java,v 1.2 2005/08/24 04:38:51 jhook Exp $
36  */

37 public final class EditableValueHolderRule extends MetaRule {
38
39     final static class LiteralValidatorMetadata extends Metadata {
40
41         private final String JavaDoc validatorId;
42
43         public LiteralValidatorMetadata(String JavaDoc validatorId) {
44             this.validatorId = validatorId;
45         }
46
47         public void applyMetadata(FaceletContext ctx, Object JavaDoc instance) {
48             ((EditableValueHolder) instance).addValidator(ctx.getFacesContext()
49                     .getApplication().createValidator(this.validatorId));
50         }
51     }
52
53     final static class ValueChangedExpressionMetadata extends Metadata {
54         private final TagAttribute attr;
55
56         public ValueChangedExpressionMetadata(TagAttribute attr) {
57             this.attr = attr;
58         }
59
60         public void applyMetadata(FaceletContext ctx, Object JavaDoc instance) {
61             ((EditableValueHolder) instance)
62                     .addValueChangeListener(new MethodExpressionValueChangeListener(
63                             this.attr.getMethodExpression(ctx, null,
64                                     VALUECHANGE_SIG)));
65         }
66     }
67
68     final static class ValueChangedBindingMetadata extends Metadata {
69         private final TagAttribute attr;
70
71         public ValueChangedBindingMetadata(TagAttribute attr) {
72             this.attr = attr;
73         }
74
75         public void applyMetadata(FaceletContext ctx, Object JavaDoc instance) {
76             ((EditableValueHolder) instance)
77                     .setValueChangeListener(new LegacyMethodBinding(this.attr
78                             .getMethodExpression(ctx, null, VALUECHANGE_SIG)));
79         }
80     }
81
82     final static class ValidatorExpressionMetadata extends Metadata {
83         private final TagAttribute attr;
84
85         public ValidatorExpressionMetadata(TagAttribute attr) {
86             this.attr = attr;
87         }
88
89         public void applyMetadata(FaceletContext ctx, Object JavaDoc instance) {
90             ((EditableValueHolder) instance)
91                     .addValidator(new MethodExpressionValidator(this.attr
92                             .getMethodExpression(ctx, null, VALIDATOR_SIG)));
93         }
94     }
95
96     final static class ValidatorBindingMetadata extends Metadata {
97         private final TagAttribute attr;
98
99         public ValidatorBindingMetadata(TagAttribute attr) {
100             this.attr = attr;
101         }
102
103         public void applyMetadata(FaceletContext ctx, Object JavaDoc instance) {
104             ((EditableValueHolder) instance)
105                     .setValidator(new LegacyMethodBinding(this.attr
106                             .getMethodExpression(ctx, null, VALIDATOR_SIG)));
107         }
108     }
109
110     private final static Class JavaDoc[] VALIDATOR_SIG = new Class JavaDoc[] {
111             FacesContext.class, UIComponent.class, Object JavaDoc.class };
112
113     private final static Class JavaDoc[] VALUECHANGE_SIG = new Class JavaDoc[] { ValueChangeEvent.class };
114
115     public final static EditableValueHolderRule Instance = new EditableValueHolderRule();
116
117     public Metadata applyRule(String JavaDoc name, TagAttribute attribute,
118             MetadataTarget meta) {
119
120         if (meta.isTargetInstanceOf(EditableValueHolder.class)) {
121
122             boolean elSupport = FacesAPI.getComponentVersion(meta
123                     .getTargetClass()) >= 12;
124
125             if ("validator".equals(name)) {
126                 if (attribute.isLiteral()) {
127                     return new LiteralValidatorMetadata(attribute.getValue());
128                 } else if (elSupport) {
129                     return new ValidatorExpressionMetadata(attribute);
130                 } else {
131                     return new ValidatorBindingMetadata(attribute);
132                 }
133             }
134
135             if ("valueChangeListener".equals(name)) {
136                 if (elSupport) {
137                     return new ValueChangedExpressionMetadata(attribute);
138                 } else {
139                     return new ValueChangedBindingMetadata(attribute);
140                 }
141             }
142
143         }
144         return null;
145     }
146
147 }
148
Popular Tags