1 11 package org.eclipse.core.internal.watson; 12 13 import org.eclipse.core.internal.dtree.*; 14 import org.eclipse.core.internal.utils.Assert; 15 import org.eclipse.core.runtime.IPath; 16 import org.eclipse.core.runtime.Path; 17 18 22 public class DeltaIterator { 23 24 public static final int PRE_ORDER = 0; 25 26 27 public static final int POST_ORDER = 1; 28 29 30 private final int order; 31 32 private ElementTreeDelta elementTreeDelta; 33 private DeltaDataTree deltaTree; 34 private ElementTree oldTree; 35 private ElementTree newTree; 36 37 40 public DeltaIterator() { 41 order = PRE_ORDER; 42 } 43 44 47 public DeltaIterator(int order) { 48 this.order = order; 49 } 50 51 54 private void initialize(ElementTreeDelta tree) { 55 elementTreeDelta = tree; 56 deltaTree = tree.getDeltaTree(); 57 oldTree = tree.getParent(); 58 newTree = tree.getElementTree(); 59 } 60 61 65 public void iterate(ElementTreeDelta tree, IDeltaVisitor visitor) { 66 iterate(tree, visitor, Path.ROOT); 67 } 68 69 74 public void iterate(ElementTreeDelta tree, IDeltaVisitor visitor, IPath path) { 75 initialize(tree); 76 77 78 if (path.isRoot()) { 79 IPath[] children = deltaTree.getChildren(path); 80 for (int i = 0; i < children.length; i++) { 81 iterate(visitor, children[i], NodeComparison.K_CHANGED); 82 } 83 } else { 84 iterate(visitor, path); 85 } 86 } 87 88 92 private void iterate(IDeltaVisitor visitor, IPath path) { 93 94 DataTreeNode node = (DataTreeNode) deltaTree.findNodeAt(path); 95 96 if (node == null) { 97 98 if (oldTree.includes(path)) { 99 iterate(visitor, path, NodeComparison.K_ADDED); 100 } else { 101 102 if (newTree.includes(path)) { 103 iterate(visitor, path, NodeComparison.K_REMOVED); 104 } 105 } 106 } else { 107 108 iterate(visitor, path, NodeComparison.K_CHANGED); 109 } 110 } 111 112 115 private void iterate(IDeltaVisitor visitor, IPath path, int parentChange) { 116 int comparison = 0, realChange = 0; 117 Object oldData = null; 118 Object newData = null; 119 IPath[] children = null; 120 121 switch (parentChange) { 122 case NodeComparison.K_ADDED : 123 124 comparison = realChange = parentChange; 125 newData = newTree.getElementData(path); 126 children = newTree.getChildren(path); 127 break; 128 case NodeComparison.K_REMOVED : 129 130 comparison = realChange = parentChange; 131 oldData = oldTree.getElementData(path); 132 children = oldTree.getChildren(path); 133 break; 134 case NodeComparison.K_CHANGED : 135 136 NodeComparison info = (NodeComparison) deltaTree.getData(path); 137 comparison = info.getUserComparison(); 138 139 realChange = info.getComparison(); 140 children = deltaTree.getChildren(path); 141 oldData = info.getOldData(); 142 newData = info.getNewData(); 143 } 144 145 Assert.isNotNull(children); 146 147 boolean visitChildren = true; 148 if (order == PRE_ORDER) { 149 visitChildren = visitor.visitElement(elementTreeDelta, path, oldData, newData, comparison); 150 } 151 152 if (visitChildren) { 153 for (int i = 0; i < children.length; i++) { 154 iterate(visitor, children[i], realChange); 155 } 156 } 157 158 if (order == POST_ORDER) { 159 visitor.visitElement(elementTreeDelta, path, oldData, newData, comparison); 160 } 161 } 162 } | Popular Tags |