1 18 19 package swingwtx.swing.tree; 20 21 import java.util.ArrayList ; 22 import java.util.Enumeration ; 23 import java.util.NoSuchElementException ; 24 import java.util.Vector ; 25 26 public class DefaultMutableTreeNode implements MutableTreeNode { 27 28 29 public org.eclipse.swt.widgets.TreeItem peer = null; 30 31 32 static public final Enumeration EMPTY_ENUMERATION = new Enumeration () { 33 public Object nextElement() { 34 throw new NoSuchElementException ("No elements"); 35 } 36 public boolean hasMoreElements() { return false; } 37 }; 38 39 protected MutableTreeNode parent; 40 protected boolean allowsChildren; 41 protected Vector children; 42 protected Object userObject; 43 44 public DefaultMutableTreeNode() { 45 this(null); 46 } 47 48 public DefaultMutableTreeNode(Object userObject) { 49 this(userObject, true); 50 } 51 52 public DefaultMutableTreeNode(Object userObject, boolean allowsChildren) { 53 super(); 54 this.allowsChildren = allowsChildren; 55 this.userObject = userObject; 56 parent = null; 57 } 58 59 public void insert(MutableTreeNode newChild, int childIndex) { 60 MutableTreeNode oldParent = (MutableTreeNode) newChild.getParent(); 61 if (oldParent != null) 62 oldParent.remove(newChild); 63 newChild.setParent(this); 64 if (children == null) 65 children = new Vector (); 66 children.insertElementAt(newChild, childIndex); 67 } 68 69 public void remove(int childIndex) { 70 MutableTreeNode child = (MutableTreeNode) getChildAt(childIndex); 71 children.removeElementAt(childIndex); 72 child.setParent(null); 73 } 74 75 public void add(MutableTreeNode newChild) { 76 if(newChild.getParent() == this && newChild != null) 77 insert(newChild, getChildCount() - 1); 78 else 79 insert(newChild, getChildCount()); 80 } 81 82 public void setParent(MutableTreeNode newParent) { 83 parent = newParent; 84 } 85 86 public TreeNode getParent() { 87 return parent; 88 } 89 90 public void removeFromParent() { 91 MutableTreeNode parent = (MutableTreeNode) getParent(); 92 parent.remove(this); 93 } 94 95 public TreeNode getChildAt(int index) { 96 return (TreeNode) children.elementAt(index); 97 } 98 99 public int getChildCount() { 100 if (children == null) 101 return 0; 102 else 103 return children.size(); 104 } 105 106 public int getIndex(TreeNode aChild) { 107 return children.indexOf(aChild); 108 } 109 110 public Enumeration children() { 111 if (children != null) 112 return children.elements(); 113 else 114 return EMPTY_ENUMERATION; 115 } 116 117 public boolean getAllowsChildren() { 118 return allowsChildren; 119 } 120 121 public void setAllowsChildren(boolean allows) { 122 if (allows != allowsChildren) { 123 allowsChildren = allows; 124 if (!allowsChildren) { 125 removeAllChildren(); 126 } 127 } 128 } 129 130 public Object getUserObject() { 131 return userObject; 132 } 133 134 public void setUserObject(Object userObject) { 135 this.userObject = userObject; 136 } 137 138 public void remove(MutableTreeNode aChild) { 139 remove(getIndex(aChild)); 140 } 141 142 public void removeAllChildren() { 143 for (int i = getChildCount() - 1; i >= 0; i--) { 144 remove(i); 145 } 146 } 147 148 public boolean isLeaf() { 149 return getChildCount() > 0; 150 } 151 152 public TreeNode[] getPath() { 153 ArrayList path = new ArrayList (); 154 TreeNode current = this; 155 while (current!=null) { 156 path.add(0,current); 157 current = current.getParent(); 158 } 159 return (TreeNode[])path.toArray(new TreeNode[0]); 160 } 161 162 163 public boolean isNodeAncestor(TreeNode anotherNode) { 164 TreeNode ancestor = this; 165 while (ancestor != null) { 166 if (ancestor == anotherNode) { 167 return true; 168 } 169 ancestor = ancestor.getParent(); 170 } 171 return false; 172 } 173 174 public boolean isNodeDescendant(DefaultMutableTreeNode anotherNode) { 175 return anotherNode.isNodeAncestor(this); 176 } 177 178 public TreeNode getRoot() { 179 TreeNode anc = this; 180 TreeNode previous; 181 previous = anc; 182 while (anc != null) { 183 anc = anc.getParent(); 184 } 185 return previous; 186 } 187 188 public boolean isRoot() { 189 return getParent() == null; 190 } 191 192 public String toString() { 193 if (userObject == null) { 194 return null; 195 } else { 196 return userObject.toString(); 197 } 198 } 199 } 200 | Popular Tags |