1 11 package org.eclipse.core.internal.dtree; 12 13 import org.eclipse.core.runtime.IPath; 14 15 20 public class DataTree extends AbstractDataTree { 21 22 25 private DataTreeNode rootNode; 26 27 30 public DataTree() { 31 this.empty(); 32 } 33 34 40 public AbstractDataTreeNode copyCompleteSubtree(IPath key) { 41 DataTreeNode node = findNodeAt(key); 42 if (node == null) { 43 handleNotFound(key); 44 } 45 return copyHierarchy(node); 46 } 47 48 54 DataTreeNode copyHierarchy(DataTreeNode node) { 55 DataTreeNode newNode; 56 int size = node.size(); 57 if (size == 0) { 58 newNode = new DataTreeNode(node.getName(), node.getData()); 59 } else { 60 AbstractDataTreeNode[] children = node.getChildren(); 61 DataTreeNode[] newChildren = new DataTreeNode[size]; 62 for (int i = size; --i >= 0;) { 63 newChildren[i] = this.copyHierarchy((DataTreeNode) children[i]); 64 } 65 newNode = new DataTreeNode(node.getName(), node.getData(), newChildren); 66 } 67 68 return newNode; 69 } 70 71 75 public void createChild(IPath parentKey, String localName) { 76 createChild(parentKey, localName, null); 77 } 78 79 83 public void createChild(IPath parentKey, String localName, Object data) { 84 DataTreeNode node = findNodeAt(parentKey); 85 if (node == null) 86 handleNotFound(parentKey); 87 if (this.isImmutable()) 88 handleImmutableTree(); 89 90 if (node.includesChild(localName)) { 91 node.replaceChild(localName, new DataTreeNode(localName, data)); 92 } else { 93 this.replaceNode(parentKey, node.copyWithNewChild(localName, new DataTreeNode(localName, data))); 94 } 95 } 96 97 102 protected AbstractDataTree createInstance() { 103 return new DataTree(); 104 } 105 106 114 public void createSubtree(IPath key, AbstractDataTreeNode subtree) { 115 116 DataTreeNode newNode = copyHierarchy((DataTreeNode) subtree); 119 120 if (this.isImmutable()) { 121 handleImmutableTree(); 122 } 123 124 if (key.isRoot()) { 125 setRootNode(newNode); 126 } else { 127 String localName = key.lastSegment(); 128 newNode.setName(localName); 130 IPath parentKey = key.removeLastSegments(1); 131 132 DataTreeNode node = findNodeAt(parentKey); 133 if (node == null) { 134 handleNotFound(parentKey); 135 } 136 137 138 if (node.includesChild(localName)) { 139 node.replaceChild(localName, newNode); 140 } 141 142 this.replaceNode(parentKey, node.copyWithNewChild(localName, newNode)); 143 } 144 } 145 146 150 public void deleteChild(IPath parentKey, String localName) { 151 if (this.isImmutable()) 152 handleImmutableTree(); 153 DataTreeNode node = findNodeAt(parentKey); 154 if (node == null || (!node.includesChild(localName))) { 155 handleNotFound(node == null ? parentKey : parentKey.append(localName)); 156 } else { 157 this.replaceNode(parentKey, node.copyWithoutChild(localName)); 158 } 159 } 160 161 165 public void empty() { 166 this.setRootNode(new DataTreeNode(null, null)); 167 } 168 169 175 public DataTreeNode findNodeAt(IPath key) { 176 AbstractDataTreeNode node = this.getRootNode(); 177 int keyLength = key.segmentCount(); 178 for (int i = 0; i < keyLength; i++) { 179 try { 180 node = node.childAt(key.segment(i)); 181 } catch (ObjectNotFoundException notFound) { 182 return null; 183 } 184 } 185 return (DataTreeNode) node; 186 } 187 188 194 public Object getData(IPath key) { 195 DataTreeNode node = findNodeAt(key); 196 if (node == null) { 197 handleNotFound(key); 198 return null; 199 } 200 return node.getData(); 201 } 202 203 207 public String [] getNamesOfChildren(IPath parentKey) { 208 DataTreeNode parentNode; 209 parentNode = findNodeAt(parentKey); 210 if (parentNode == null) { 211 handleNotFound(parentKey); 212 return null; 213 } 214 return parentNode.namesOfChildren(); 215 } 216 217 220 AbstractDataTreeNode getRootNode() { 221 return rootNode; 222 } 223 224 228 public boolean includes(IPath key) { 229 return (findNodeAt(key) != null); 230 } 231 232 239 public DataTreeLookup lookup(IPath key) { 240 DataTreeNode node = this.findNodeAt(key); 241 if (node == null) 242 return DataTreeLookup.newLookup(key, false, null); 243 return DataTreeLookup.newLookup(key, true, node.getData()); 244 } 245 246 249 protected void replaceNode(IPath key, DataTreeNode node) { 250 DataTreeNode found; 251 if (key.isRoot()) { 252 this.setRootNode(node); 253 } else { 254 found = this.findNodeAt(key.removeLastSegments(1)); 255 found.replaceChild(key.lastSegment(), node); 256 } 257 } 258 259 263 public void setData(IPath key, Object data) { 264 DataTreeNode node = this.findNodeAt(key); 265 if (this.isImmutable()) 266 handleImmutableTree(); 267 if (node == null) { 268 handleNotFound(key); 269 } else { 270 node.setData(data); 271 } 272 } 273 274 278 void setRootNode(DataTreeNode aNode) { 279 rootNode = aNode; 280 } 281 } 282 | Popular Tags |