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      public void apply(FaceletContext ctx, UIComponent parent) throws IOException, FacesException, FaceletException,
88              ELException
89      {
90          FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(ctx);
91          boolean processed = false;
92          //assign an unique id for this section
93          AbstractFaceletContext actx = (AbstractFaceletContext) ctx;
94          String uniqueId = actx.generateUniqueFaceletTagId(
95              fcc.startComponentUniqueIdSection(), tagId);
96          Integer savedOption = null;
97          try
98          {
99              Integer restoredSavedOption = getSavedOption(ctx, fcc, parent, uniqueId);
100 
101             if (restoredSavedOption != null)
102             {
103                 if (!PhaseId.RESTORE_VIEW.equals(ctx.getFacesContext().getCurrentPhaseId()))
104                 {
105                     for (int i = 0; i < this.when.length; i++)
106                     {
107                         //Ensure each option has its unique section
108                         fcc.startComponentUniqueIdSection();
109                         try
110                         {
111                             if (!processed)
112                             {
113                                 if (this.when[i].isTestTrue(ctx))
114                                 {
115                                     boolean markInitialState = !restoredSavedOption.equals(i);
116                                     boolean oldMarkInitialState = false;
117                                     Boolean isBuildingInitialState = null;
118                                     try
119                                     {
120                                         if (markInitialState)
121                                         {
122                                             //set markInitialState flag
123                                             oldMarkInitialState = fcc.isMarkInitialState();
124                                             fcc.setMarkInitialState(true);
125                                             isBuildingInitialState = (Boolean) ctx.getFacesContext().
126                                                     getAttributes().put(
127                                                     StateManager.IS_BUILDING_INITIAL_STATE, Boolean.TRUE);
128                                         }
129                                         this.when[i].apply(ctx, parent);
130                                     }
131                                     finally
132                                     {
133                                         if (markInitialState)
134                                         {
135                                             //unset markInitialState flag
136                                             if (isBuildingInitialState == null)
137                                             {
138                                                 ctx.getFacesContext().getAttributes().remove(
139                                                         StateManager.IS_BUILDING_INITIAL_STATE);
140                                             }
141                                             else
142                                             {
143                                                 ctx.getFacesContext().getAttributes().put(
144                                                         StateManager.IS_BUILDING_INITIAL_STATE, isBuildingInitialState);
145                                             }
146                                             fcc.setMarkInitialState(oldMarkInitialState);
147                                         }
148                                     }
149                                     processed = true;
150                                     savedOption = i;
151                                     //return;
152                                 }
153                             }
154                         }
155                         finally
156                         {
157                             fcc.endComponentUniqueIdSection();
158                         }
159                     }
160 
161                 }
162                 else
163                 {
164                     for (int i = 0; i < this.when.length; i++)
165                     {
166                         //Ensure each option has its unique section
167                         fcc.startComponentUniqueIdSection();
168                         try
169                         {
170                             if (!processed)
171                             {
172                                 if (restoredSavedOption.equals(i))
173                                 {
174                                     this.when[i].apply(ctx, parent);
175                                     processed = true;
176                                     savedOption = i;
177                                     //return;
178                                 }
179                             }
180                         }
181                         finally
182                         {
183                             fcc.endComponentUniqueIdSection();
184                         }
185                     }
186                 }
187             }
188             else
189             {
190                 for (int i = 0; i < this.when.length; i++)
191                 {
192                     //Ensure each option has its unique section
193                     fcc.startComponentUniqueIdSection();
194                     try
195                     {
196                         if (!processed)
197                         {
198                             if (this.when[i].isTestTrue(ctx))
199                             {
200                                 this.when[i].apply(ctx, parent);
201                                 processed = true;
202                                 savedOption = i;
203                                 //return;
204                             }
205                         }
206                     }
207                     finally
208                     {
209                         fcc.endComponentUniqueIdSection();
210                     }
211                 }
212             }
213             if (this.otherwise != null)
214             {
215                 fcc.startComponentUniqueIdSection();
216                 try
217                 {
218                     if (!processed)
219                     {
220                         this.otherwise.apply(ctx, parent);
221                         savedOption = -1;
222                     }
223                 }
224                 finally
225                 {
226                     fcc.endComponentUniqueIdSection();
227                 }
228             }
229         }
230         finally
231         {
232             fcc.endComponentUniqueIdSection();
233         }
234 
235         ComponentSupport.saveInitialTagState(ctx, fcc, parent, uniqueId, savedOption);
236         if (fcc.isUsingPSSOnThisView() && fcc.isRefreshTransientBuildOnPSS() && !fcc.isRefreshingTransientBuild())
237         {
238             //Mark the parent component to be saved and restored fully.
239             ComponentSupport.markComponentToRestoreFully(ctx.getFacesContext(), parent);
240         }
241         if (fcc.isDynamicComponentSection())
242         {
243             ComponentSupport.markComponentToRefreshDynamically(ctx.getFacesContext(), parent);
244         }
245     }
246     
247     private Integer getSavedOption(FaceletContext ctx, FaceletCompositionContext fcc,
248                                    UIComponent parent, String uniqueId)
249     {
250         return (Integer) ComponentSupport.restoreInitialTagState(ctx, fcc, parent, uniqueId);
251     }
252 }