KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > swingwtx > swing > tree > TreePath


1 /*
2    SwingWT
3    Copyright(c)2003-2004, R. Rawson-Tetley
4  
5    For more information on distributing and using this program, please
6    see the accompanying "COPYING" file.
7  
8    Contact me by electronic mail: bobintetley@users.sourceforge.net
9  
10    $Log: TreePath.java,v $
11    Revision 1.5 2004/01/10 11:46:27 bobintetley
12    JTree/TreePath fixes by Sachin (broken path, rootVisible support) and Rob
13      (Missing root node from path, couldn't represent TreePath as string)
14
15    Revision 1.4 2003/12/14 09:13:39 bobintetley
16    Added CVS log to source headers
17  
18  */

19
20 package swingwtx.swing.tree;
21
22
23 public class TreePath {
24     
25     private TreePath parentPath;
26     private Object JavaDoc lastPathComponent;
27     
28     protected TreePath() {
29     }
30     
31     public TreePath(Object JavaDoc singlePath) {
32         lastPathComponent = singlePath;
33         parentPath = null;
34     }
35     
36     protected TreePath(TreePath parent, Object JavaDoc lastElement) {
37         parentPath = parent;
38         lastPathComponent = lastElement;
39     }
40     
41     protected TreePath(Object JavaDoc[] path, int length) {
42         lastPathComponent = path[path.length - length];
43         if(length > 1)
44             parentPath = new TreePath(path, length - 1);
45     }
46     
47     public TreePath(Object JavaDoc[] path) {
48         lastPathComponent = path[0];
49         if (path.length > 1) {
50             parentPath = new TreePath(path, path.length - 1);
51         }
52     }
53     
54     public Object JavaDoc[] getPath() {
55         int i = getPathCount();
56         Object JavaDoc[] result = new Object JavaDoc[i--];
57         for(TreePath path = this; path != null; path = path.parentPath) {
58             result[i--] = path.lastPathComponent;
59         }
60         return result;
61     }
62     
63     public Object JavaDoc getLastPathComponent() {
64         return lastPathComponent;
65     }
66     
67     public int getPathCount() {
68         int result = 0;
69         for (TreePath path = this; path != null; path = path.parentPath) {
70             result++;
71         }
72         return result;
73     }
74     
75     public Object JavaDoc getPathComponent(int element) {
76         TreePath path = this;
77         int pathLength = getPathCount();
78         for(int i = pathLength-1; i != element; i--) {
79             path = path.parentPath;
80         }
81         return path.lastPathComponent;
82     }
83     
84     public TreePath pathByAddingChild(Object JavaDoc child) {
85         return new TreePath(this, child);
86     }
87     
88     public TreePath getParentPath() {
89         return parentPath;
90     }
91     
92     public String JavaDoc toString() {
93         StringBuffer JavaDoc s = new StringBuffer JavaDoc("[");
94         for(int c = 0, m = getPathCount(); c<m;
95         c++) {
96             if(c> 0)
97                 s.append(", ");
98             s.append(getPathComponent(c));
99         }
100         s.append("]");
101         return s.toString();
102     }
103     
104 }
105
Popular Tags