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.ui; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.util.Collection; |
23 | |
import java.util.HashMap; |
24 | |
import java.util.Map; |
25 | |
import java.util.logging.Level; |
26 | |
import java.util.logging.Logger; |
27 | |
|
28 | |
import javax.el.ELException; |
29 | |
import javax.faces.FacesException; |
30 | |
import javax.faces.application.StateManager; |
31 | |
import javax.faces.component.UIComponent; |
32 | |
import javax.faces.event.PhaseId; |
33 | |
import javax.faces.view.facelets.FaceletContext; |
34 | |
import javax.faces.view.facelets.FaceletException; |
35 | |
import javax.faces.view.facelets.TagAttribute; |
36 | |
import javax.faces.view.facelets.TagConfig; |
37 | |
import javax.faces.view.facelets.TagHandler; |
38 | |
|
39 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute; |
40 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag; |
41 | |
import org.apache.myfaces.view.facelets.AbstractFaceletContext; |
42 | |
import org.apache.myfaces.view.facelets.FaceletCompositionContext; |
43 | |
import org.apache.myfaces.view.facelets.TemplateClient; |
44 | |
import org.apache.myfaces.view.facelets.tag.ComponentContainerHandler; |
45 | |
import org.apache.myfaces.view.facelets.tag.TagHandlerUtils; |
46 | |
import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
@JSFFaceletTag(name="ui:decorate") |
62 | |
public final class DecorateHandler extends TagHandler implements TemplateClient, ComponentContainerHandler |
63 | |
{ |
64 | |
|
65 | |
|
66 | 0 | private static final Logger log = Logger.getLogger(DecorateHandler.class.getName()); |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
@JSFFaceletAttribute( |
73 | |
name="template", |
74 | |
className="javax.el.ValueExpression", |
75 | |
deferredValueType="java.lang.String") |
76 | |
private final TagAttribute _template; |
77 | |
|
78 | |
private final Map<String, DefineHandler> _handlers; |
79 | |
|
80 | |
private final ParamHandler[] _params; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public DecorateHandler(TagConfig config) |
86 | |
{ |
87 | 0 | super(config); |
88 | 0 | _template = getRequiredAttribute("template"); |
89 | 0 | _handlers = new HashMap<String, DefineHandler>(); |
90 | |
|
91 | 0 | for (DefineHandler handler : TagHandlerUtils.findNextByType(nextHandler, DefineHandler.class)) |
92 | |
{ |
93 | 0 | _handlers.put(handler.getName(), handler); |
94 | 0 | if (log.isLoggable(Level.FINE)) |
95 | |
{ |
96 | 0 | log.fine(tag + " found Define[" + handler.getName() + "]"); |
97 | |
} |
98 | 0 | } |
99 | |
|
100 | 0 | Collection<ParamHandler> params = TagHandlerUtils.findNextByType(nextHandler, ParamHandler.class); |
101 | 0 | if (!params.isEmpty()) |
102 | |
{ |
103 | 0 | int i = 0; |
104 | 0 | _params = new ParamHandler[params.size()]; |
105 | 0 | for (ParamHandler handler : params) |
106 | |
{ |
107 | 0 | _params[i++] = handler; |
108 | 0 | } |
109 | 0 | } |
110 | |
else |
111 | |
{ |
112 | 0 | _params = null; |
113 | |
} |
114 | 0 | } |
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException, |
123 | |
ELException |
124 | |
{ |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | 0 | AbstractFaceletContext actx = (AbstractFaceletContext) ctx; |
137 | 0 | actx.pushClient(this); |
138 | 0 | FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx); |
139 | 0 | String uniqueId = null; |
140 | |
try |
141 | |
{ |
142 | 0 | if (!_template.isLiteral()) |
143 | |
{ |
144 | 0 | uniqueId = actx.generateUniqueFaceletTagId( |
145 | |
fcc.startComponentUniqueIdSection(), tagId); |
146 | |
} |
147 | 0 | else if (_params != null) |
148 | |
{ |
149 | 0 | uniqueId = actx.generateUniqueFaceletTagId( |
150 | |
fcc.generateUniqueComponentId(), tagId); |
151 | |
} |
152 | 0 | if (_params != null) |
153 | |
{ |
154 | |
|
155 | |
|
156 | 0 | for (int i = 0; i < _params.length; i++) |
157 | |
{ |
158 | 0 | _params[i].apply(ctx, parent, _params[i].getName(ctx), _params[i].getValue(ctx), uniqueId); |
159 | |
} |
160 | |
} |
161 | |
|
162 | |
String path; |
163 | 0 | boolean markInitialState = false; |
164 | 0 | if (!_template.isLiteral()) |
165 | |
{ |
166 | |
|
167 | |
|
168 | 0 | String restoredPath = (String) ComponentSupport.restoreInitialTagState(ctx, fcc, parent, uniqueId); |
169 | 0 | if (restoredPath != null) |
170 | |
{ |
171 | |
|
172 | |
|
173 | 0 | if (!PhaseId.RESTORE_VIEW.equals(ctx.getFacesContext().getCurrentPhaseId())) |
174 | |
{ |
175 | 0 | path = this._template.getValue(ctx); |
176 | 0 | if (path == null || path.length() == 0) |
177 | |
{ |
178 | |
return; |
179 | |
} |
180 | 0 | if (!path.equals(restoredPath)) |
181 | |
{ |
182 | 0 | markInitialState = true; |
183 | |
} |
184 | |
} |
185 | |
else |
186 | |
{ |
187 | 0 | path = restoredPath; |
188 | |
} |
189 | |
} |
190 | |
else |
191 | |
{ |
192 | |
|
193 | 0 | path = this._template.getValue(ctx); |
194 | |
} |
195 | 0 | ComponentSupport.saveInitialTagState(ctx, fcc, parent, uniqueId, path); |
196 | 0 | } |
197 | |
else |
198 | |
{ |
199 | 0 | path = _template.getValue(ctx); |
200 | |
} |
201 | |
try |
202 | |
{ |
203 | 0 | boolean oldMarkInitialState = false; |
204 | 0 | Boolean isBuildingInitialState = null; |
205 | 0 | if (markInitialState) |
206 | |
{ |
207 | |
|
208 | 0 | oldMarkInitialState = fcc.isMarkInitialState(); |
209 | 0 | fcc.setMarkInitialState(true); |
210 | 0 | isBuildingInitialState = (Boolean) ctx.getFacesContext().getAttributes().put( |
211 | |
StateManager.IS_BUILDING_INITIAL_STATE, Boolean.TRUE); |
212 | |
} |
213 | |
try |
214 | |
{ |
215 | 0 | ctx.includeFacelet(parent, path); |
216 | |
} |
217 | |
finally |
218 | |
{ |
219 | 0 | if (markInitialState) |
220 | |
{ |
221 | |
|
222 | 0 | if (isBuildingInitialState == null) |
223 | |
{ |
224 | 0 | ctx.getFacesContext().getAttributes().remove( |
225 | |
StateManager.IS_BUILDING_INITIAL_STATE); |
226 | |
} |
227 | |
else |
228 | |
{ |
229 | 0 | ctx.getFacesContext().getAttributes().put( |
230 | |
StateManager.IS_BUILDING_INITIAL_STATE, isBuildingInitialState); |
231 | |
} |
232 | 0 | fcc.setMarkInitialState(oldMarkInitialState); |
233 | |
} |
234 | |
} |
235 | |
} |
236 | |
finally |
237 | |
{ |
238 | |
|
239 | 0 | actx.popClient(this); |
240 | 0 | } |
241 | |
} |
242 | |
finally |
243 | |
{ |
244 | 0 | if (!_template.isLiteral()) |
245 | |
{ |
246 | 0 | fcc.endComponentUniqueIdSection(); |
247 | |
} |
248 | |
} |
249 | 0 | if (!_template.isLiteral() && fcc.isUsingPSSOnThisView() && fcc.isRefreshTransientBuildOnPSS() && |
250 | |
!fcc.isRefreshingTransientBuild()) |
251 | |
{ |
252 | |
|
253 | 0 | ComponentSupport.markComponentToRestoreFully(ctx.getFacesContext(), parent); |
254 | |
} |
255 | 0 | if (!_template.isLiteral() && fcc.isDynamicComponentSection()) |
256 | |
{ |
257 | 0 | ComponentSupport.markComponentToRefreshDynamically(ctx.getFacesContext(), parent); |
258 | |
} |
259 | 0 | } |
260 | |
|
261 | |
public boolean apply(FaceletContext ctx, UIComponent parent, String name) throws IOException, FacesException, |
262 | |
FaceletException, ELException |
263 | |
{ |
264 | 0 | if (name != null) |
265 | |
{ |
266 | 0 | DefineHandler handler = _handlers.get(name); |
267 | 0 | if (handler != null) |
268 | |
{ |
269 | 0 | handler.applyDefinition(ctx, parent); |
270 | 0 | return true; |
271 | |
} |
272 | |
else |
273 | |
{ |
274 | 0 | return false; |
275 | |
} |
276 | |
} |
277 | |
else |
278 | |
{ |
279 | 0 | this.nextHandler.apply(ctx, parent); |
280 | 0 | return true; |
281 | |
} |
282 | |
} |
283 | |
} |