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