KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.sun.facelets.tag.jsf.core;
2
3 import java.io.IOException JavaDoc;
4 import java.io.Serializable JavaDoc;
5
6 import javax.el.ELContext;
7 import javax.el.ELException;
8 import javax.el.ValueExpression;
9 import javax.faces.FacesException;
10 import javax.faces.component.ActionSource;
11 import javax.faces.component.ActionSource2;
12 import javax.faces.component.UIComponent;
13 import javax.faces.context.FacesContext;
14 import javax.faces.el.ValueBinding;
15 import javax.faces.event.AbortProcessingException;
16 import javax.faces.event.ActionEvent;
17 import javax.faces.event.ActionListener;
18
19 import com.sun.facelets.FaceletContext;
20 import com.sun.facelets.FaceletException;
21 import com.sun.facelets.el.LegacyValueBinding;
22 import com.sun.facelets.tag.TagAttribute;
23 import com.sun.facelets.tag.TagConfig;
24 import com.sun.facelets.tag.TagException;
25 import com.sun.facelets.tag.TagHandler;
26 import com.sun.facelets.tag.jsf.ComponentSupport;
27 import com.sun.facelets.util.FacesAPI;
28
29 public class SetPropertyActionListenerHandler extends TagHandler {
30
31     private final TagAttribute value;
32
33     private final TagAttribute target;
34
35     public SetPropertyActionListenerHandler(TagConfig config) {
36         super(config);
37         this.value = this.getRequiredAttribute("value");
38         this.target = this.getRequiredAttribute("target");
39     }
40
41     public void apply(FaceletContext ctx, UIComponent parent)
42             throws IOException JavaDoc, FacesException, FaceletException, ELException {
43         if (parent instanceof ActionSource) {
44             ActionSource src = (ActionSource) parent;
45             if (ComponentSupport.isNew(parent)) {
46                 ValueExpression valueExpr = this.value.getValueExpression(ctx,
47                         Object JavaDoc.class);
48                 ValueExpression targetExpr = this.target.getValueExpression(
49                         ctx, Object JavaDoc.class);
50
51                 ActionListener listener;
52
53                 if (FacesAPI.getVersion() >= 12 && src instanceof ActionSource2) {
54                     listener = new SetPropertyListener(valueExpr, targetExpr);
55                 } else {
56                     listener = new LegacySetPropertyListener(
57                             new LegacyValueBinding(valueExpr),
58                             new LegacyValueBinding(targetExpr));
59                 }
60
61                 src.addActionListener(listener);
62             }
63         } else {
64             throw new TagException(this.tag,
65                     "Parent is not of type ActionSource, type is: " + parent);
66         }
67     }
68
69     private static class LegacySetPropertyListener implements ActionListener,
70             Serializable JavaDoc {
71
72         private ValueBinding value;
73
74         private ValueBinding target;
75
76         public LegacySetPropertyListener() {
77         };
78
79         public LegacySetPropertyListener(ValueBinding value, ValueBinding target) {
80             this.value = value;
81             this.target = target;
82         }
83
84         public void processAction(ActionEvent evt)
85                 throws AbortProcessingException {
86             FacesContext faces = FacesContext.getCurrentInstance();
87             Object JavaDoc valueObj = this.value.getValue(faces);
88             this.target.setValue(faces, valueObj);
89         }
90
91     }
92
93     private static class SetPropertyListener implements ActionListener,
94             Serializable JavaDoc {
95
96         private ValueExpression value;
97
98         private ValueExpression target;
99
100         public SetPropertyListener() {
101         };
102
103         public SetPropertyListener(ValueExpression value, ValueExpression target) {
104             this.value = value;
105             this.target = target;
106         }
107
108         public void processAction(ActionEvent evt)
109                 throws AbortProcessingException {
110             FacesContext faces = FacesContext.getCurrentInstance();
111             ELContext el = faces.getELContext();
112             Object JavaDoc valueObj = this.value.getValue(el);
113             this.target.setValue(el, valueObj);
114         }
115
116     }
117
118 }
119
Popular Tags