001    package org.apache.myfaces.tobago.model;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import javax.swing.tree.DefaultMutableTreeNode;
021    import java.io.Serializable;
022    import java.util.Arrays;
023    import java.util.List;
024    
025    /**
026     * Handles a path in a tree from the root node to the position inside this tree.
027     *
028     * Date: 02.12.2008
029     */
030    public class TreePath implements Serializable {
031    
032      private int[] path;
033    
034      public TreePath(int... path) {
035        assert path[0] == 0;
036        this.path = path;
037      }
038    
039      public TreePath(List<Integer> pathList) {
040        assert pathList.get(0) == 0;
041        path = new int[pathList.size()];
042        for (int i = 0; i < path.length; i++) {
043          path[i] = pathList.get(i);
044        }
045      }
046    
047      public TreePath(TreePath position, int addendum) {
048        this.path = new int[position.path.length + 1];
049        System.arraycopy(position.path, 0, path, 0, position.path.length);
050        path[position.path.length] = addendum;
051      }
052    
053      public int[] getPath() {
054        return path;
055      }
056    
057      public int getLength() {
058        return path.length;
059      }
060    
061      public String getPathString() {
062        StringBuffer buffer = new StringBuffer();
063        for (int item : path) {
064          buffer.append("_");
065          buffer.append(item);
066        }
067        return buffer.toString();
068      }
069    
070      public String getParentPathString() {
071        StringBuffer buffer = new StringBuffer();
072        for (int i = 0; i < path.length - 1; i++) {
073          buffer.append("_");
074          buffer.append(path[i]);
075        }
076        return buffer.toString();
077      }
078    
079      /**
080       * Returns the node at the position of this NodePath applied to the parameter node.
081       * @param tree The start node.
082       * @return The node applied to the given path.
083       */
084      public DefaultMutableTreeNode getNode(DefaultMutableTreeNode tree) {
085        for (int i = 1; i < path.length; i++) { // i = 1: first entry must be 0 and means the root
086          int pos = path[i];
087          tree = (DefaultMutableTreeNode) tree.getChildAt(pos);
088        }
089        return tree;
090      }
091    
092      @Override
093      public boolean equals(Object o) {
094        if (this == o) {
095          return true;
096        }
097        if (o == null || getClass() != o.getClass()) {
098          return false;
099        }
100        TreePath nodeIndex = (TreePath) o;
101        return Arrays.equals(path, nodeIndex.path);
102    
103      }
104    
105      @Override
106      public int hashCode() {
107        return path != null ? Arrays.hashCode(path) : 0;
108      }
109    
110      @Override
111      public String toString() {
112        return getPathString();
113      }
114    }