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.jstl.core; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.util.ArrayList; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.List; |
25 | |
|
26 | |
import javax.el.ELException; |
27 | |
import javax.faces.FacesException; |
28 | |
import javax.faces.application.StateManager; |
29 | |
import javax.faces.component.UIComponent; |
30 | |
import javax.faces.event.PhaseId; |
31 | |
import javax.faces.view.facelets.FaceletContext; |
32 | |
import javax.faces.view.facelets.FaceletException; |
33 | |
import javax.faces.view.facelets.TagConfig; |
34 | |
import javax.faces.view.facelets.TagException; |
35 | |
import javax.faces.view.facelets.TagHandler; |
36 | |
|
37 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag; |
38 | |
import org.apache.myfaces.view.facelets.AbstractFaceletContext; |
39 | |
import org.apache.myfaces.view.facelets.FaceletCompositionContext; |
40 | |
import org.apache.myfaces.view.facelets.tag.ComponentContainerHandler; |
41 | |
import org.apache.myfaces.view.facelets.tag.TagHandlerUtils; |
42 | |
import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
@JSFFaceletTag(name="c:choose") |
53 | |
public final class ChooseHandler extends TagHandler implements ComponentContainerHandler |
54 | |
{ |
55 | |
|
56 | |
private final ChooseOtherwiseHandler otherwise; |
57 | |
private final ChooseWhenHandler[] when; |
58 | |
|
59 | |
public ChooseHandler(TagConfig config) |
60 | |
{ |
61 | 0 | super(config); |
62 | |
|
63 | 0 | List<ChooseWhenHandler> whenList = new ArrayList<ChooseWhenHandler>(); |
64 | 0 | for (ChooseWhenHandler handler : TagHandlerUtils.findNextByType(nextHandler, ChooseWhenHandler.class)) |
65 | |
{ |
66 | 0 | whenList.add(handler); |
67 | 0 | } |
68 | 0 | if (whenList.isEmpty()) |
69 | |
{ |
70 | 0 | throw new TagException(this.tag, "Choose Tag must have one or more When Tags"); |
71 | |
} |
72 | |
|
73 | 0 | this.when = (ChooseWhenHandler[]) whenList.toArray(new ChooseWhenHandler[whenList.size()]); |
74 | |
|
75 | 0 | Iterator<ChooseOtherwiseHandler> itrOtherwise = |
76 | |
TagHandlerUtils.findNextByType(nextHandler, ChooseOtherwiseHandler.class).iterator(); |
77 | 0 | if (itrOtherwise.hasNext()) |
78 | |
{ |
79 | 0 | this.otherwise = itrOtherwise.next(); |
80 | |
} |
81 | |
else |
82 | |
{ |
83 | 0 | this.otherwise = null; |
84 | |
} |
85 | 0 | } |
86 | |
|
87 | |
public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException, |
88 | |
ELException |
89 | |
{ |
90 | 0 | FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx); |
91 | 0 | boolean processed = false; |
92 | |
|
93 | 0 | AbstractFaceletContext actx = (AbstractFaceletContext) ctx; |
94 | 0 | String uniqueId = actx.generateUniqueFaceletTagId( |
95 | |
fcc.startComponentUniqueIdSection(), tagId); |
96 | 0 | Integer savedOption = null; |
97 | |
try |
98 | |
{ |
99 | 0 | Integer restoredSavedOption = getSavedOption(ctx, fcc, parent, uniqueId); |
100 | |
|
101 | 0 | if (restoredSavedOption != null) |
102 | |
{ |
103 | 0 | if (!PhaseId.RESTORE_VIEW.equals(ctx.getFacesContext().getCurrentPhaseId())) |
104 | |
{ |
105 | 0 | for (int i = 0; i < this.when.length; i++) |
106 | |
{ |
107 | |
|
108 | 0 | fcc.startComponentUniqueIdSection(); |
109 | |
try |
110 | |
{ |
111 | 0 | if (!processed) |
112 | |
{ |
113 | 0 | if (this.when[i].isTestTrue(ctx)) |
114 | |
{ |
115 | 0 | boolean markInitialState = !restoredSavedOption.equals(i); |
116 | 0 | boolean oldMarkInitialState = false; |
117 | 0 | Boolean isBuildingInitialState = null; |
118 | |
try |
119 | |
{ |
120 | 0 | if (markInitialState) |
121 | |
{ |
122 | |
|
123 | 0 | oldMarkInitialState = fcc.isMarkInitialState(); |
124 | 0 | fcc.setMarkInitialState(true); |
125 | 0 | isBuildingInitialState = (Boolean) ctx.getFacesContext(). |
126 | |
getAttributes().put( |
127 | |
StateManager.IS_BUILDING_INITIAL_STATE, Boolean.TRUE); |
128 | |
} |
129 | 0 | this.when[i].apply(ctx, parent); |
130 | |
} |
131 | |
finally |
132 | |
{ |
133 | 0 | if (markInitialState) |
134 | |
{ |
135 | |
|
136 | 0 | if (isBuildingInitialState == null) |
137 | |
{ |
138 | 0 | ctx.getFacesContext().getAttributes().remove( |
139 | |
StateManager.IS_BUILDING_INITIAL_STATE); |
140 | |
} |
141 | |
else |
142 | |
{ |
143 | 0 | ctx.getFacesContext().getAttributes().put( |
144 | |
StateManager.IS_BUILDING_INITIAL_STATE, isBuildingInitialState); |
145 | |
} |
146 | 0 | fcc.setMarkInitialState(oldMarkInitialState); |
147 | |
} |
148 | |
} |
149 | 0 | processed = true; |
150 | 0 | savedOption = i; |
151 | |
|
152 | |
} |
153 | |
} |
154 | |
} |
155 | |
finally |
156 | |
{ |
157 | 0 | fcc.endComponentUniqueIdSection(); |
158 | 0 | } |
159 | |
} |
160 | |
|
161 | |
} |
162 | |
else |
163 | |
{ |
164 | 0 | for (int i = 0; i < this.when.length; i++) |
165 | |
{ |
166 | |
|
167 | 0 | fcc.startComponentUniqueIdSection(); |
168 | |
try |
169 | |
{ |
170 | 0 | if (!processed) |
171 | |
{ |
172 | 0 | if (restoredSavedOption.equals(i)) |
173 | |
{ |
174 | 0 | this.when[i].apply(ctx, parent); |
175 | 0 | processed = true; |
176 | 0 | savedOption = i; |
177 | |
|
178 | |
} |
179 | |
} |
180 | |
} |
181 | |
finally |
182 | |
{ |
183 | 0 | fcc.endComponentUniqueIdSection(); |
184 | 0 | } |
185 | |
} |
186 | |
} |
187 | |
} |
188 | |
else |
189 | |
{ |
190 | 0 | for (int i = 0; i < this.when.length; i++) |
191 | |
{ |
192 | |
|
193 | 0 | fcc.startComponentUniqueIdSection(); |
194 | |
try |
195 | |
{ |
196 | 0 | if (!processed) |
197 | |
{ |
198 | 0 | if (this.when[i].isTestTrue(ctx)) |
199 | |
{ |
200 | 0 | this.when[i].apply(ctx, parent); |
201 | 0 | processed = true; |
202 | 0 | savedOption = i; |
203 | |
|
204 | |
} |
205 | |
} |
206 | |
} |
207 | |
finally |
208 | |
{ |
209 | 0 | fcc.endComponentUniqueIdSection(); |
210 | 0 | } |
211 | |
} |
212 | |
} |
213 | 0 | if (this.otherwise != null) |
214 | |
{ |
215 | 0 | fcc.startComponentUniqueIdSection(); |
216 | |
try |
217 | |
{ |
218 | 0 | if (!processed) |
219 | |
{ |
220 | 0 | this.otherwise.apply(ctx, parent); |
221 | 0 | savedOption = -1; |
222 | |
} |
223 | |
} |
224 | |
finally |
225 | |
{ |
226 | 0 | fcc.endComponentUniqueIdSection(); |
227 | 0 | } |
228 | |
} |
229 | |
} |
230 | |
finally |
231 | |
{ |
232 | 0 | fcc.endComponentUniqueIdSection(); |
233 | 0 | } |
234 | |
|
235 | 0 | ComponentSupport.saveInitialTagState(ctx, fcc, parent, uniqueId, savedOption); |
236 | 0 | if (fcc.isUsingPSSOnThisView() && fcc.isRefreshTransientBuildOnPSS() && !fcc.isRefreshingTransientBuild()) |
237 | |
{ |
238 | |
|
239 | 0 | ComponentSupport.markComponentToRestoreFully(ctx.getFacesContext(), parent); |
240 | |
} |
241 | 0 | if (fcc.isDynamicComponentSection()) |
242 | |
{ |
243 | 0 | ComponentSupport.markComponentToRefreshDynamically(ctx.getFacesContext(), parent); |
244 | |
} |
245 | 0 | } |
246 | |
|
247 | |
private Integer getSavedOption(FaceletContext ctx, FaceletCompositionContext fcc, |
248 | |
UIComponent parent, String uniqueId) |
249 | |
{ |
250 | 0 | return (Integer) ComponentSupport.restoreInitialTagState(ctx, fcc, parent, uniqueId); |
251 | |
} |
252 | |
} |