KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > tag > jstl > core > ChooseHandler


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14
15 package com.sun.facelets.tag.jstl.core;
16
17 import com.sun.facelets.tag.TagConfig;
18 import com.sun.facelets.tag.TagException;
19
20 import java.io.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.el.ELException;
26 import javax.faces.FacesException;
27
28 import javax.faces.component.UIComponent;
29
30 import com.sun.facelets.FaceletContext;
31 import com.sun.facelets.FaceletException;
32 import com.sun.facelets.tag.TagHandler;
33
34 /**
35  * @author Jacob Hookom
36  * @version $Id: ChooseHandler.java,v 1.2 2005/08/24 04:38:51 jhook Exp $
37  */

38 public final class ChooseHandler extends TagHandler {
39     
40     private final ChooseOtherwiseHandler otherwise;
41     private final ChooseWhenHandler[] when;
42     
43     public ChooseHandler(TagConfig config) {
44         super(config);
45         
46         List JavaDoc whenList = new ArrayList JavaDoc();
47         Iterator JavaDoc itr = this.findNextByType(ChooseWhenHandler.class);
48         while (itr.hasNext()) {
49             whenList.add(itr.next());
50         }
51         if (whenList.isEmpty()) {
52             throw new TagException(this.tag, "Choose Tag must have one or more When Tags");
53         }
54         this.when = (ChooseWhenHandler[]) whenList.toArray(new ChooseWhenHandler[whenList.size()]);
55         
56         itr = this.findNextByType(ChooseOtherwiseHandler.class);
57         if (itr.hasNext()) {
58             this.otherwise = (ChooseOtherwiseHandler) itr.next();
59         } else {
60             this.otherwise = null;
61         }
62     }
63
64     public void apply(FaceletContext ctx, UIComponent parent)
65             throws IOException JavaDoc, FacesException, FaceletException, ELException {
66         for (int i = 0; i < this.when.length; i++) {
67             if (this.when[i].isTestTrue(ctx)) {
68                 this.when[i].apply(ctx, parent);
69                 return;
70             }
71         }
72         if (this.otherwise != null) {
73             this.otherwise.apply(ctx, parent);
74         }
75     }
76
77 }
78
Popular Tags