1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.custom.tree2;
21
22 import org.apache.commons.lang.StringUtils;
23 import java.util.Stack;
24
25
26
27
28
29 public class TreeWalkerBase implements TreeWalker
30 {
31 private String ROOT_NODE_ID = "0";
32 private String TREE_NODE_SEPARATOR = ":";
33
34 private Tree tree;
35 private Stack nodeStack = new Stack();
36 private Stack idStack = new Stack();
37 private boolean checkState = true;
38 private boolean startedWalking = false;
39
40
41 public void setTree(Tree tree)
42 {
43 this.tree = tree;
44 }
45
46
47 public boolean isCheckState()
48 {
49 return checkState;
50 }
51
52
53 public void setCheckState(boolean checkState)
54 {
55 this.checkState = checkState;
56 }
57
58
59 public boolean next()
60 {
61 if (!startedWalking)
62 {
63
64 idStack.push(ROOT_NODE_ID);
65 tree.setNodeId(ROOT_NODE_ID);
66 nodeStack.push(tree.getNode());
67
68 startedWalking = true;
69 return true;
70 }
71
72 if (nodeStack.isEmpty())
73 {
74 return false;
75 }
76
77 TreeNode prevNode = (TreeNode)nodeStack.peek();
78 String prevNodeId = (String)idStack.peek();
79
80 if (prevNode.isLeaf())
81 {
82 nodeStack.pop();
83 idStack.pop();
84
85 return next();
86 }
87 else
88 {
89 TreeNode nextNode = null;
90 String nextNodeId = null;
91
92 if (prevNodeId.equals(tree.getNodeId()))
93 {
94
95
96
97
98
99
100 nextNodeId = prevNodeId + TREE_NODE_SEPARATOR + "0";
101
102
103 if (checkState)
104 {
105 if (!tree.getDataModel().getTreeState().isNodeExpanded(prevNodeId))
106 {
107 nodeStack.pop();
108 idStack.pop();
109
110 return next();
111 }
112 }
113 }
114 else
115 {
116
117 String currentNodeId = tree.getNodeId();
118 String parentNodeId = StringUtils.substringBeforeLast(currentNodeId, TREE_NODE_SEPARATOR);
119 tree.setNodeId(parentNodeId);
120 TreeNode parentNode = tree.getNode();
121
122 int siblingCount = Integer.parseInt(currentNodeId.substring(parentNodeId.length()+1));
123 siblingCount++;
124
125 if (siblingCount == parentNode.getChildCount())
126 {
127
128 nodeStack.pop();
129 idStack.pop();
130
131 return next();
132 }
133
134 nextNodeId = parentNodeId + TREE_NODE_SEPARATOR + siblingCount;
135 }
136
137 tree.setNodeId(nextNodeId);
138 nextNode = tree.getNode();
139
140 nodeStack.push(nextNode);
141 idStack.push(nextNodeId);
142
143 return true;
144 }
145 }
146
147
148 public String getRootNodeId()
149 {
150 return ROOT_NODE_ID;
151 }
152
153
154 public void reset()
155 {
156 nodeStack.empty();
157 idStack.empty();
158 startedWalking = false;
159 }
160 }