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;
20
21 import java.io.IOException;
22
23 import javax.el.ValueExpression;
24 import javax.faces.component.UIComponent;
25 import javax.faces.component.behavior.Behavior;
26 import javax.faces.component.behavior.ClientBehavior;
27 import javax.faces.component.behavior.ClientBehaviorHolder;
28 import javax.faces.context.FacesContext;
29 import javax.faces.view.BehaviorHolderAttachedObjectHandler;
30 import javax.faces.view.facelets.BehaviorHandler;
31 import javax.faces.view.facelets.ComponentHandler;
32 import javax.faces.view.facelets.FaceletContext;
33 import javax.faces.view.facelets.MetaRuleset;
34 import javax.faces.view.facelets.TagAttribute;
35 import javax.faces.view.facelets.TagAttributeException;
36 import javax.faces.view.facelets.TagException;
37 import javax.faces.view.facelets.TagHandlerDelegate;
38
39 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
40 import org.apache.myfaces.view.facelets.tag.MetaRulesetImpl;
41 import org.apache.myfaces.view.facelets.tag.jsf.core.AjaxHandler;
42
43
44
45
46
47
48
49 public class BehaviorTagHandlerDelegate extends TagHandlerDelegate implements BehaviorHolderAttachedObjectHandler
50 {
51
52 private BehaviorHandler _delegate;
53
54 public BehaviorTagHandlerDelegate(BehaviorHandler delegate)
55 {
56 _delegate = delegate;
57 }
58
59 @Override
60 public void apply(FaceletContext ctx, UIComponent parent) throws IOException
61 {
62 if (!ComponentHandler.isNew(parent))
63 {
64 return;
65 }
66
67
68
69 if (parent instanceof ClientBehaviorHolder)
70 {
71 applyAttachedObject(ctx.getFacesContext(), parent);
72 }
73 else if (UIComponent.isCompositeComponent(parent))
74 {
75 FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(ctx);
76
77
78
79
80
81
82
83
84 mctx.addAttachedObjectHandler(parent, _delegate);
85 }
86 else
87 {
88 throw new TagException(_delegate.getTag(), "Parent not composite component or an instance of ClientBehaviorHolder: " + parent);
89 }
90
91 }
92
93 protected Behavior createBehavior(FaceletContext ctx)
94 {
95 if (_delegate.getBehaviorId() == null)
96 {
97 throw new TagException(
98 _delegate.getTag(),
99 "No behavior id defined");
100 }
101 return ctx.getFacesContext().getApplication().createBehavior(_delegate.getBehaviorId());
102 }
103
104
105
106
107
108
109 @Override
110 public MetaRuleset createMetaRuleset(Class type)
111 {
112 MetaRuleset ruleset = new MetaRulesetImpl(_delegate.getTag(), type);
113 ruleset.ignore("binding");
114 ruleset.ignore("event");
115 return ruleset;
116 }
117
118
119
120
121 public void applyAttachedObject(FacesContext context, UIComponent parent)
122 {
123
124 FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(
125 FaceletContext.FACELET_CONTEXT_KEY);
126
127 ValueExpression ve = null;
128 Behavior behavior = null;
129 if (_delegate.getBinding() != null)
130 {
131 ve = _delegate.getBinding().getValueExpression(faceletContext, Behavior.class);
132 behavior = (Behavior) ve.getValue(faceletContext);
133 }
134 if (behavior == null)
135 {
136 behavior = this.createBehavior(faceletContext);
137 if (ve != null)
138 {
139 ve.setValue(faceletContext, behavior);
140 }
141 }
142 if (behavior == null)
143 {
144 throw new TagException(_delegate.getTag(), "No Validator was created");
145 }
146 _delegate.setAttributes(faceletContext, behavior);
147
148 if (behavior instanceof ClientBehavior)
149 {
150
151 ClientBehaviorHolder cvh = (ClientBehaviorHolder) parent;
152
153
154
155
156
157
158
159 String eventName = getEventName();
160 if (eventName == null)
161 {
162 eventName = cvh.getDefaultEventName();
163 }
164 if (eventName == null)
165 {
166 throw new TagAttributeException(_delegate.getEvent(), "eventName could not be defined for client behavior "+ behavior.toString());
167 }
168 else if (!cvh.getEventNames().contains(eventName))
169 {
170 throw new TagAttributeException(_delegate.getEvent(), "eventName "+eventName+" not found on component instance");
171 }
172 else
173 {
174 cvh.addClientBehavior(eventName, (ClientBehavior) behavior);
175 }
176
177 AjaxHandler.registerJsfAjaxDefaultResource(faceletContext, parent);
178 }
179 }
180
181 public String getFor()
182 {
183 TagAttribute forAttribute = _delegate.getTagAttribute("for");
184
185 if (forAttribute == null)
186 {
187 return null;
188 }
189 else
190 {
191 return forAttribute.getValue();
192 }
193 }
194
195 public String getEventName()
196 {
197 return _delegate.getEventName();
198 }
199
200 }