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(final TreeNode node) {
70      if (node == null) {
71        throw new IllegalArgumentException();
72      }
73  
74      TreeNode p = node;
75      final List<TreeNode> list = new ArrayList<TreeNode>();
76      int n = 0;
77      while (p != null) {
78        list.add(p);
79        p = p.getParent();
80        n++;
81      }
82      path = new int[n - 1];
83      for (int i = n - 2; i >= 0; i--) {
84        final TreeNode parent = list.get(i + 1);
85        final TreeNode child = list.get(i);
86        for (int j = 0; j < parent.getChildCount(); j++) {
87          if (parent.getChildAt(j) == child) { // == is okay in this case
88            path[n - 2 - i] = j;
89            break;
90          }
91        }
92      }
93    }
94  
95    public int[] getPath() {
96      return path;
97    }
98  
99    public TreePath getParent() {
100     return new TreePath(Arrays.copyOf(path, path.length - 1));
101   }
102 
103   public boolean isRoot() {
104     return path.length == 0;
105   }
106 
107   public int getLength() {
108     return path.length;
109   }
110 
111   @Override
112   public boolean equals(final Object o) {
113     if (this == o) {
114       return true;
115     }
116     if (o == null || getClass() != o.getClass()) {
117       return false;
118     }
119     final TreePath nodeIndex = (TreePath) o;
120     return Arrays.equals(path, nodeIndex.path);
121 
122   }
123 
124   @Override
125   public int hashCode() {
126     return path != null ? Arrays.hashCode(path) : 0;
127   }
128 
129   @Override
130   public String toString() {
131     return Arrays.toString(path);
132   }
133 }