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.custom.focus2;
20  
21  import java.util.Iterator;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.context.FacesContext;
25  
26  public class TreeVisitor
27  {
28  
29      public static interface TreeTraversalListener
30      {
31  
32          /**
33           * @param context
34           * @param level
35           * @param component
36           * @return false to stop traversal
37           */
38          boolean traverse(FacesContext context, int level, UIComponent component);
39      }
40  
41      public static void traverseTree(FacesContext context,
42              TreeTraversalListener listener, UIComponent startingFrom)
43      {
44  
45          int level = 0;
46  
47          if (!listener.traverse(context, level, startingFrom))
48              return;
49  
50          if (!recurseDown(context, listener, startingFrom, level + 1))
51              return;
52  
53          recurseUp(context, listener, startingFrom, level - 1);
54      }
55  
56      private static boolean recurseDown(FacesContext context,
57              TreeTraversalListener listener, UIComponent comp, int level)
58      {
59          Iterator it = comp.getFacetsAndChildren();
60  
61          while (it.hasNext())
62          {
63              UIComponent child = (UIComponent) it.next();
64              if (!listener.traverse(context, level, child))
65                  return false;
66              if (!recurseDown(context, listener, child, level + 1))
67                  return false;
68          }
69  
70          return true;
71      }
72  
73      private static boolean recurseUp(FacesContext context,
74              TreeTraversalListener listener, UIComponent comp, int level)
75      {
76          UIComponent parent = comp.getParent();
77          if (parent != null)
78          {
79              Iterator siblingIt = parent.getFacetsAndChildren();
80  
81              boolean traverseNow = false;
82  
83              while (siblingIt.hasNext())
84              {
85                  if (traverseNow)
86                  {
87                      UIComponent nextSibling = (UIComponent) siblingIt.next();
88  
89                      if (!listener.traverse(context, level, nextSibling))
90                          return false;
91  
92                      if (!recurseDown(context, listener, nextSibling, level + 1))
93                          return false;
94                  }
95                  else
96                  {
97                      UIComponent potSibling = (UIComponent) siblingIt.next();
98  
99                      if (potSibling == comp)
100                     {
101                         traverseNow = true;
102                     }
103                 }
104             }
105 
106             return recurseUp(context, listener, parent, level - 1);
107         }
108         return false;
109     }
110 }