KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > util > ViewIterator


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.util;
17
18 import javax.faces.component.UIComponent;
19 import java.util.Iterator JavaDoc;
20 import java.util.NoSuchElementException JavaDoc;
21 import java.util.Stack JavaDoc;
22
23 /**
24  * Iterates over a view structure, i.e. a component tree, in a
25  * depth first manner.
26  *
27  * @author Manfred Geiler (latest modification by $Author: matze $)
28  * @version $Revision: 1.5 $ $Date: 2004/10/13 11:51:01 $
29  */

30 public class ViewIterator
31         implements Iterator JavaDoc
32 {
33     //private static final Log log = LogFactory.getLog(ViewIterator.class);
34
private UIComponent _next = null;
35     private boolean _mayHaveNext = true;
36     private UIComponent _current = null;
37     private Stack JavaDoc _stack = new Stack JavaDoc();
38
39     /**
40      * @param root the root of the view structure to iterate over
41      */

42     public ViewIterator(UIComponent root)
43     {
44         _next = root;
45         _current = null;
46     }
47
48     public boolean hasNext()
49     {
50         return getNext() != null;
51     }
52
53     /**
54      * @return the next component in the view. The first element is always the given root
55      * component.
56      */

57     public Object JavaDoc next()
58     {
59         if (!hasNext())
60         {
61             throw new NoSuchElementException JavaDoc();
62         }
63         _current = _next;
64         _next = null;
65         return _current;
66     }
67
68     private UIComponent getNext()
69     {
70         if (_next == null && _mayHaveNext)
71         {
72             //has child?
73
Iterator JavaDoc children = _current.getFacetsAndChildren();
74             if (children.hasNext())
75             {
76                 _next = (UIComponent)children.next();
77                 //push siblings
78
_stack.push(children);
79             }
80             else
81             {
82                 //has next sibling?
83
for (;;)
84                 {
85                     if (_stack.empty())
86                     {
87                         _next = null;
88                         _mayHaveNext = false;
89                         break;
90                     }
91
92                     Iterator JavaDoc currentSiblings = (Iterator JavaDoc)_stack.peek();
93                     if (currentSiblings.hasNext())
94                     {
95                         _next = (UIComponent)currentSiblings.next();
96                         break;
97                     }
98                     else
99                     {
100                         _stack.pop();
101                     }
102                 }
103             }
104         }
105         return _next;
106     }
107
108     public void remove()
109     {
110         throw new UnsupportedOperationException JavaDoc(this.getClass().getName() + " UnsupportedOperationException");
111     }
112 }
113
Popular Tags