1 19 20 package swingwtx.swing.tree; 21 22 23 public class TreePath { 24 25 private TreePath parentPath; 26 private Object lastPathComponent; 27 28 protected TreePath() { 29 } 30 31 public TreePath(Object singlePath) { 32 lastPathComponent = singlePath; 33 parentPath = null; 34 } 35 36 protected TreePath(TreePath parent, Object lastElement) { 37 parentPath = parent; 38 lastPathComponent = lastElement; 39 } 40 41 protected TreePath(Object [] 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 [] path) { 48 lastPathComponent = path[0]; 49 if (path.length > 1) { 50 parentPath = new TreePath(path, path.length - 1); 51 } 52 } 53 54 public Object [] getPath() { 55 int i = getPathCount(); 56 Object [] result = new Object [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 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 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 child) { 85 return new TreePath(this, child); 86 } 87 88 public TreePath getParentPath() { 89 return parentPath; 90 } 91 92 public String toString() { 93 StringBuffer s = new StringBuffer ("["); 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 |