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.composite; |
20 | |
|
21 | |
import java.beans.BeanDescriptor; |
22 | |
import java.io.IOException; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.List; |
25 | |
import java.util.logging.Level; |
26 | |
import java.util.logging.Logger; |
27 | |
|
28 | |
import javax.faces.component.UIComponent; |
29 | |
import javax.faces.view.AttachedObjectTarget; |
30 | |
import javax.faces.view.facelets.FaceletContext; |
31 | |
import javax.faces.view.facelets.TagAttribute; |
32 | |
import javax.faces.view.facelets.TagConfig; |
33 | |
import javax.faces.view.facelets.TagHandler; |
34 | |
|
35 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute; |
36 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag; |
37 | |
import org.apache.myfaces.view.facelets.FaceletCompositionContext; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
@JSFFaceletTag(name="composite:clientBehavior") |
44 | |
public class ClientBehaviorHandler extends TagHandler implements InterfaceDescriptorCreator |
45 | |
{ |
46 | |
|
47 | 0 | private static final Logger log = Logger.getLogger(ClientBehaviorHandler.class.getName()); |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
@JSFFaceletAttribute(name="event", |
58 | |
className="javax.el.ValueExpression", |
59 | |
deferredValueType="java.lang.String", |
60 | |
required=true) |
61 | |
protected final TagAttribute _event; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
@JSFFaceletAttribute(name="name", |
71 | |
className="javax.el.ValueExpression", |
72 | |
deferredValueType="java.lang.String", |
73 | |
required=true) |
74 | |
protected final TagAttribute _name; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
@JSFFaceletAttribute(name="default", |
83 | |
className="javax.el.ValueExpression", |
84 | |
deferredValueType="boolean") |
85 | |
protected final TagAttribute _default; |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
@JSFFaceletAttribute(name="targets", |
94 | |
className="javax.el.ValueExpression", |
95 | |
deferredValueType="java.lang.String") |
96 | |
protected final TagAttribute _targets; |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
private boolean _cacheable; |
103 | |
|
104 | |
private ClientBehaviorAttachedObjectTargetImpl _target; |
105 | |
|
106 | |
public ClientBehaviorHandler(TagConfig config) |
107 | |
{ |
108 | 0 | super(config); |
109 | 0 | _name = getRequiredAttribute("name"); |
110 | 0 | _event = getAttribute("event"); |
111 | 0 | _default = getAttribute("default"); |
112 | 0 | _targets = getAttribute("targets"); |
113 | 0 | if (_name.isLiteral() && |
114 | |
(_event == null || _event.isLiteral()) && |
115 | |
(_default == null || _default.isLiteral() )) |
116 | |
{ |
117 | 0 | _cacheable = true; |
118 | |
} |
119 | |
else |
120 | |
{ |
121 | 0 | _cacheable = false; |
122 | |
} |
123 | 0 | } |
124 | |
|
125 | |
@SuppressWarnings("unchecked") |
126 | |
public void apply(FaceletContext ctx, UIComponent parent) |
127 | |
throws IOException |
128 | |
{ |
129 | 0 | UIComponent compositeBaseParent |
130 | |
= FaceletCompositionContext.getCurrentInstance(ctx).getCompositeComponentFromStack(); |
131 | |
|
132 | 0 | CompositeComponentBeanInfo beanInfo = |
133 | |
(CompositeComponentBeanInfo) compositeBaseParent.getAttributes() |
134 | |
.get(UIComponent.BEANINFO_KEY); |
135 | |
|
136 | 0 | if (beanInfo == null) |
137 | |
{ |
138 | 0 | if (log.isLoggable(Level.SEVERE)) |
139 | |
{ |
140 | 0 | log.severe("Cannot find composite bean descriptor UIComponent.BEANINFO_KEY "); |
141 | |
} |
142 | 0 | return; |
143 | |
} |
144 | |
|
145 | 0 | BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor(); |
146 | |
|
147 | |
|
148 | 0 | List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>) |
149 | |
beanDescriptor.getValue( |
150 | |
AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY); |
151 | |
|
152 | 0 | if (targetList == null) |
153 | |
{ |
154 | |
|
155 | 0 | targetList = new ArrayList<AttachedObjectTarget>(); |
156 | 0 | beanDescriptor.setValue( |
157 | |
AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY, |
158 | |
targetList); |
159 | |
} |
160 | |
|
161 | |
|
162 | 0 | if (isCacheable()) |
163 | |
{ |
164 | 0 | if (_target == null) |
165 | |
{ |
166 | 0 | _target = createAttachedObjectTarget(ctx); |
167 | |
} |
168 | 0 | targetList.add(_target); |
169 | |
} |
170 | |
else |
171 | |
{ |
172 | 0 | ClientBehaviorAttachedObjectTargetImpl target = createAttachedObjectTarget(ctx); |
173 | 0 | targetList.add(target); |
174 | |
} |
175 | |
|
176 | 0 | this.nextHandler.apply(ctx, parent); |
177 | 0 | } |
178 | |
|
179 | |
public boolean isCacheable() |
180 | |
{ |
181 | 0 | return _cacheable; |
182 | |
} |
183 | |
|
184 | |
public void setCacheable(boolean cacheable) |
185 | |
{ |
186 | 0 | _cacheable = cacheable; |
187 | 0 | } |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
protected ClientBehaviorAttachedObjectTargetImpl createAttachedObjectTarget(FaceletContext ctx) |
196 | |
{ |
197 | 0 | ClientBehaviorAttachedObjectTargetImpl target = new ClientBehaviorAttachedObjectTargetImpl(); |
198 | |
|
199 | 0 | if (_event != null) |
200 | |
{ |
201 | 0 | target.setEvent(_event.getValueExpression(ctx, String.class)); |
202 | |
} |
203 | 0 | if (_name != null) |
204 | |
{ |
205 | 0 | target.setName(_name.getValueExpression(ctx, String.class)); |
206 | |
} |
207 | 0 | if (_default != null) |
208 | |
{ |
209 | 0 | target.setDefault(_default.getBoolean(ctx)); |
210 | |
} |
211 | 0 | if (_targets != null) |
212 | |
{ |
213 | 0 | target.setTargets(_targets.getValueExpression(ctx, String.class)); |
214 | |
} |
215 | 0 | return target; |
216 | |
} |
217 | |
} |