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  
20  package org.apache.myfaces.tobago.model;
21  
22  import javax.swing.tree.TreeNode;
23  import java.io.Serializable;
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  /**
29   * Handles a path in a tree from the root node to the position inside this tree.
30   * The position of the root node is dropped in the list, because it would always be zero.
31   * The path of the root node as length 0.
32   * <br>
33   * Example:
34   * <pre>
35   *  + Root               Path: []
36   *  |
37   *  +-+ Node             Path: [0]
38   *  | |
39   *  | +-+ Sub-Node       Path: [0, 0]
40   *  | |
41   *  | +-+ Sub-Node       Path: [0, 1]
42   *  |
43   *  +-+ Node             Path: [1]
44   *    |
45   *    +-+ Sub-Node       Path: [1, 0]
46   *    |
47   *    +-+ Sub-Node       Path: [1, 1]
48   *    |
49   *    +-+ Sub-Node       Path: [1, 2]
50   * </pre>
51   *
52   * @since 1.5.0
53   */
54  public class TreePath implements Serializable {
55  
56    private final int[] path;
57  
58    public TreePath(final int... path) {
59      this.path = path;
60    }
61  
62    public TreePath(final List<Integer> pathList) {
63      path = new int[pathList.size()];
64      for (int i = 0; i < path.length; i++) {
65        path[i] = pathList.get(i);
66      }
67    }
68  
69    public TreePath(TreeNode node) {
70      if (node == null) {
71        throw new IllegalArgumentException();
72      }
73  
74      final List<TreeNode> list = new ArrayList<TreeNode>();
75      int n = 0;
76      while (node != null) {
77        list.add(node);
78        node = node.getParent();
79        n++;
80      }
81      path = new int[n - 1];
82      for (int i = n - 2; i >= 0; i--) {
83        final TreeNode parent = list.get(i + 1);
84        final TreeNode child = list.get(i);
85        for (int j = 0; j < parent.getChildCount(); j++) {
86          if (parent.getChildAt(j) == child) { // == is okay in this case
87            path[n - 2 - i] = j;
88            break;
89          }
90        }
91      }
92    }
93  
94    public int[] getPath() {
95      return path;
96    }
97  
98    public TreePath getParent() {
99      return new TreePath(Arrays.copyOf(path, path.length - 1));
100   }
101 
102   public boolean isRoot() {
103     return path.length == 0;
104   }
105 
106   public int getLength() {
107     return path.length;
108   }
109 
110   @Override
111   public boolean equals(final Object o) {
112     if (this == o) {
113       return true;
114     }
115     if (o == null || getClass() != o.getClass()) {
116       return false;
117     }
118     final TreePath nodeIndex = (TreePath) o;
119     return Arrays.equals(path, nodeIndex.path);
120 
121   }
122 
123   @Override
124   public int hashCode() {
125     return path != null ? Arrays.hashCode(path) : 0;
126   }
127 
128   @Override
129   public String toString() {
130     return Arrays.toString(path);
131   }
132 }