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 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   * Handles a path in a tree from the root node to the position inside this tree.
33   * The position of the root node is dropped in the list, because it would always be zero.
34   * The path of the root node as length 0.
35   * <p/>
36   * Example:
37   * <pre>
38   *  + Root               Path: []
39   *  |
40   *  +-+ Node             Path: [0]
41   *  | |
42   *  | +-+ Sub-Node       Path: [0, 0]
43   *  | |
44   *  | +-+ Sub-Node       Path: [0, 1]
45   *  |
46   *  +-+ Node             Path: [1]
47   *    |
48   *    +-+ Sub-Node       Path: [1, 0]
49   *    |
50   *    +-+ Sub-Node       Path: [1, 1]
51   *    |
52   *    +-+ Sub-Node       Path: [1, 2]
53   * </pre>
54   *
55   * @since 1.5.0
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     * @deprecated since 2.0.0
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     * @deprecated since 2.0.0
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) { // == is okay in this case
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    * @deprecated since 2.0.0
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    * @deprecated since 1.5.0
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    * Returns the node at the position of this NodePath applied to the parameter node.
150    *
151    * @param tree The start node.
152    * @return The node applied to the given path.
153    * @deprecated since 2.0.0
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++) { // i = 1: first entry must be 0 and means the root
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 }