1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.model;
21
22 import org.apache.myfaces.tobago.internal.util.StringUtils;
23
24 import javax.swing.tree.DefaultMutableTreeNode;
25 import javax.swing.tree.TreeNode;
26 import java.io.Serializable;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public class TreePath implements Serializable {
58
59 private final int[] path;
60
61 public TreePath(int... path) {
62 this.path = path;
63 }
64
65 public TreePath(List<Integer> pathList) {
66 path = new int[pathList.size()];
67 for (int i = 0; i < path.length; i++) {
68 path[i] = pathList.get(i);
69 }
70 }
71
72
73
74
75 @Deprecated
76 public TreePath(TreePath position, int addendum) {
77 this.path = new int[position.path.length + 1];
78 System.arraycopy(position.path, 0, path, 0, position.path.length);
79 path[position.path.length] = addendum;
80 }
81
82
83
84
85 @Deprecated
86 public TreePath(String string) throws NumberFormatException {
87 this(StringUtils.parseIntegerList(string, "_"));
88 }
89
90 public TreePath(TreeNode node) {
91 if (node == null) {
92 throw new IllegalArgumentException();
93 }
94
95 final List<TreeNode> list = new ArrayList<TreeNode>();
96 int n = 0;
97 while (node != null) {
98 list.add(node);
99 node = node.getParent();
100 n++;
101 }
102 path = new int[n - 1];
103 for (int i = n - 2; i >= 0; i--) {
104 TreeNode parent = list.get(i + 1);
105 TreeNode child = list.get(i);
106 for (int j = 0; j < parent.getChildCount(); j++) {
107 if (parent.getChildAt(j) == child) {
108 path[n - 2 - i] = j;
109 break;
110 }
111 }
112 }
113 }
114
115 public int[] getPath() {
116 return path;
117 }
118
119 public int getLength() {
120 return path.length;
121 }
122
123
124
125
126 public String getPathString() {
127 StringBuilder builder = new StringBuilder();
128 for (int item : path) {
129 builder.append("_");
130 builder.append(item);
131 }
132 return builder.toString();
133 }
134
135
136
137
138 @Deprecated
139 public String getParentPathString() {
140 StringBuilder builder = new StringBuilder();
141 for (int i = 0; i < path.length - 1; i++) {
142 builder.append("_");
143 builder.append(path[i]);
144 }
145 return builder.toString();
146 }
147
148
149
150
151
152
153
154
155 @Deprecated
156 public DefaultMutableTreeNode getNode(DefaultMutableTreeNode tree) {
157 if (tree == null) {
158 return null;
159 }
160 for (int i = 1; i < path.length; i++) {
161 int pos = path[i];
162 if (pos >= tree.getChildCount()) {
163 return null;
164 }
165 tree = (DefaultMutableTreeNode) tree.getChildAt(pos);
166 }
167 return tree;
168 }
169
170 @Override
171 public boolean equals(Object o) {
172 if (this == o) {
173 return true;
174 }
175 if (o == null || getClass() != o.getClass()) {
176 return false;
177 }
178 TreePath nodeIndex = (TreePath) o;
179 return Arrays.equals(path, nodeIndex.path);
180
181 }
182
183 @Override
184 public int hashCode() {
185 return path != null ? Arrays.hashCode(path) : 0;
186 }
187
188 @Override
189 public String toString() {
190 return Arrays.toString(path);
191 }
192 }