View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Simple conditional tag that establishes a context for
46   * mutually exclusive conditional operations, marked by
47   * <when> and <otherwise>
48   * 
49   * @author Jacob Hookom
50   * @version $Id$
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          //assign an unique id for this section
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                         //Ensure each option has its unique section
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                                             //set markInitialState flag
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                                             //unset markInitialState flag
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                                     //return;
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                         //Ensure each option has its unique section
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                                     //return;
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                     //Ensure each option has its unique section
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                                 //return;
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             //Mark the parent component to be saved and restored fully.
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 }