1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.view.facelets.tag.jsf.core; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.io.Serializable; |
23 | |
|
24 | |
import javax.el.ELContext; |
25 | |
import javax.el.ELException; |
26 | |
import javax.el.ExpressionFactory; |
27 | |
import javax.el.ValueExpression; |
28 | |
import javax.faces.FacesException; |
29 | |
import javax.faces.component.ActionSource; |
30 | |
import javax.faces.component.UIComponent; |
31 | |
import javax.faces.context.FacesContext; |
32 | |
import javax.faces.event.AbortProcessingException; |
33 | |
import javax.faces.event.ActionEvent; |
34 | |
import javax.faces.event.ActionListener; |
35 | |
import javax.faces.view.ActionSource2AttachedObjectHandler; |
36 | |
import javax.faces.view.Location; |
37 | |
import javax.faces.view.facelets.ComponentHandler; |
38 | |
import javax.faces.view.facelets.FaceletContext; |
39 | |
import javax.faces.view.facelets.FaceletException; |
40 | |
import javax.faces.view.facelets.TagAttribute; |
41 | |
import javax.faces.view.facelets.TagConfig; |
42 | |
import javax.faces.view.facelets.TagException; |
43 | |
import javax.faces.view.facelets.TagHandler; |
44 | |
|
45 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute; |
46 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag; |
47 | |
import org.apache.myfaces.view.facelets.FaceletCompositionContext; |
48 | |
import org.apache.myfaces.view.facelets.el.ContextAware; |
49 | |
import org.apache.myfaces.view.facelets.el.ContextAwareELException; |
50 | |
|
51 | |
@JSFFaceletTag( |
52 | |
name = "f:setPropertyActionListener", |
53 | |
bodyContent = "empty", |
54 | |
tagClass="org.apache.myfaces.taglib.core.SetPropertyActionListenerTag") |
55 | |
public class SetPropertyActionListenerHandler extends TagHandler |
56 | |
implements ActionSource2AttachedObjectHandler |
57 | |
{ |
58 | |
private final TagAttribute _target; |
59 | |
private final TagAttribute _value; |
60 | |
|
61 | |
public SetPropertyActionListenerHandler(TagConfig config) |
62 | |
{ |
63 | 0 | super(config); |
64 | 0 | this._value = this.getRequiredAttribute("value"); |
65 | 0 | this._target = this.getRequiredAttribute("target"); |
66 | 0 | } |
67 | |
|
68 | |
public void apply(FaceletContext ctx, UIComponent parent) |
69 | |
throws IOException, FacesException, FaceletException, ELException |
70 | |
{ |
71 | |
|
72 | 0 | if (!ComponentHandler.isNew(parent)) |
73 | |
{ |
74 | 0 | return; |
75 | |
} |
76 | 0 | if (parent instanceof ActionSource) |
77 | |
{ |
78 | 0 | applyAttachedObject(ctx.getFacesContext(), parent); |
79 | |
} |
80 | 0 | else if (UIComponent.isCompositeComponent(parent)) |
81 | |
{ |
82 | 0 | FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(ctx); |
83 | 0 | mctx.addAttachedObjectHandler(parent, this); |
84 | 0 | } |
85 | |
else |
86 | |
{ |
87 | 0 | throw new TagException(this.tag, |
88 | |
"Parent is not composite component or of type ActionSource, type is: " + parent); |
89 | |
} |
90 | 0 | } |
91 | |
|
92 | |
private static class SetPropertyListener implements ActionListener, Serializable |
93 | |
{ |
94 | |
private ValueExpression _target; |
95 | |
private ValueExpression _value; |
96 | |
|
97 | |
public SetPropertyListener() |
98 | 0 | { |
99 | 0 | }; |
100 | |
|
101 | |
public SetPropertyListener(ValueExpression value, ValueExpression target) |
102 | 0 | { |
103 | 0 | _value = value; |
104 | 0 | _target = target; |
105 | 0 | } |
106 | |
|
107 | |
public void processAction(ActionEvent evt) throws AbortProcessingException |
108 | |
{ |
109 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
110 | |
|
111 | 0 | ELContext elContext = facesContext.getELContext(); |
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | 0 | Object value = _value.getValue(elContext); |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | 0 | if (value != null) |
123 | |
{ |
124 | 0 | Class<?> targetType = _target.getType(elContext); |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | 0 | ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory(); |
132 | |
try |
133 | |
{ |
134 | 0 | value = expressionFactory.coerceToType(value, targetType); |
135 | |
} |
136 | 0 | catch (ELException e) |
137 | |
{ |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | 0 | Location location = null; |
148 | |
|
149 | |
|
150 | 0 | if (_value instanceof ContextAware) |
151 | |
{ |
152 | 0 | ContextAware contextAware = (ContextAware) _value; |
153 | 0 | location = contextAware.getLocation(); |
154 | |
} |
155 | 0 | throw new ContextAwareELException(location, |
156 | |
_value.getExpressionString(), "value", e); |
157 | 0 | } |
158 | |
} |
159 | |
|
160 | |
|
161 | 0 | _target.setValue(elContext, value); |
162 | 0 | } |
163 | |
} |
164 | |
|
165 | |
public void applyAttachedObject(FacesContext context, UIComponent parent) |
166 | |
{ |
167 | |
|
168 | 0 | FaceletContext faceletContext = (FaceletContext) context.getAttributes().get( |
169 | |
FaceletContext.FACELET_CONTEXT_KEY); |
170 | |
|
171 | 0 | ActionSource src = (ActionSource) parent; |
172 | 0 | ValueExpression valueExpr = _value.getValueExpression(faceletContext, Object.class); |
173 | 0 | ValueExpression targetExpr = _target.getValueExpression(faceletContext, Object.class); |
174 | |
|
175 | 0 | src.addActionListener(new SetPropertyListener(valueExpr, targetExpr)); |
176 | 0 | } |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
@JSFFaceletAttribute |
182 | |
public String getFor() |
183 | |
{ |
184 | 0 | TagAttribute forAttribute = getAttribute("for"); |
185 | |
|
186 | 0 | if (forAttribute == null) |
187 | |
{ |
188 | 0 | return null; |
189 | |
} |
190 | |
else |
191 | |
{ |
192 | 0 | return forAttribute.getValue(); |
193 | |
} |
194 | |
} |
195 | |
} |