1 11 package org.eclipse.core.internal.events; 12 13 import java.util.Map ; 14 import org.eclipse.core.internal.dtree.DeltaDataTree; 15 import org.eclipse.core.internal.dtree.NodeComparison; 16 import org.eclipse.core.internal.resources.*; 17 import org.eclipse.core.internal.watson.ElementTree; 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.resources.IResourceDelta; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.Path; 22 23 27 public class ResourceDeltaFactory { 28 31 protected static final ResourceDelta[] NO_CHILDREN = new ResourceDelta[0]; 32 33 39 public static ResourceDelta computeDelta(Workspace workspace, ElementTree oldTree, ElementTree newTree, IPath root, long markerGeneration) { 40 ResourceComparator comparator = markerGeneration >= 0 ? ResourceComparator.getNotificationComparator() : ResourceComparator.getBuildComparator(); 42 newTree.immutable(); 43 DeltaDataTree delta = null; 44 if (Path.ROOT.equals(root)) 45 delta = newTree.getDataTree().compareWith(oldTree.getDataTree(), comparator); 46 else 47 delta = newTree.getDataTree().compareWith(oldTree.getDataTree(), comparator, root); 48 49 delta = delta.asReverseComparisonTree(comparator); 50 IPath pathInTree = root.isRoot() ? Path.ROOT : root; 51 IPath pathInDelta = Path.ROOT; 52 53 Map allMarkerDeltas = null; 55 if (markerGeneration >= 0) 56 allMarkerDeltas = workspace.getMarkerManager().getMarkerDeltas(markerGeneration); 57 58 ResourceDeltaInfo deltaInfo = new ResourceDeltaInfo(workspace, allMarkerDeltas, comparator); 60 ResourceDelta result = createDelta(workspace, delta, deltaInfo, pathInTree, pathInDelta); 61 62 deltaInfo.setNodeIDMap(computeNodeIDMap(result, new NodeIDMap())); 64 result.fixMovesAndMarkers(oldTree); 65 66 int segmentCount = result.getFullPath().segmentCount(); 69 if (segmentCount <= 1) 70 checkForOpen(result, segmentCount); 71 return result; 72 } 73 74 79 protected static void checkForOpen(ResourceDelta delta, int segmentCount) { 80 if (delta.getKind() == IResourceDelta.ADDED) 81 if (delta.newInfo.isSet(ICoreConstants.M_OPEN)) 82 delta.status |= IResourceDelta.OPEN; 83 if (segmentCount == 1) 85 return; 86 IResourceDelta[] children = delta.children; 88 for (int i = 0; i < children.length; i++) 89 checkForOpen((ResourceDelta) children[i], 1); 90 } 91 92 96 protected static NodeIDMap computeNodeIDMap(ResourceDelta delta, NodeIDMap nodeIDMap) { 97 IResourceDelta[] children = delta.children; 98 for (int i = 0; i < children.length; i++) { 99 ResourceDelta child = (ResourceDelta) children[i]; 100 IPath path = child.getFullPath(); 101 switch (child.getKind()) { 102 case IResourceDelta.ADDED : 103 nodeIDMap.putNewPath(child.newInfo.getNodeId(), path); 104 break; 105 case IResourceDelta.REMOVED : 106 nodeIDMap.putOldPath(child.oldInfo.getNodeId(), path); 107 break; 108 case IResourceDelta.CHANGED : 109 long oldID = child.oldInfo.getNodeId(); 110 long newID = child.newInfo.getNodeId(); 111 if (oldID != newID) { 113 nodeIDMap.putOldPath(oldID, path); 114 nodeIDMap.putNewPath(newID, path); 115 } 116 break; 117 } 118 computeNodeIDMap(child, nodeIDMap); 120 } 121 return nodeIDMap; 122 } 123 124 128 protected static ResourceDelta createDelta(Workspace workspace, DeltaDataTree delta, ResourceDeltaInfo deltaInfo, IPath pathInTree, IPath pathInDelta) { 129 ResourceDelta result = new ResourceDelta(pathInTree, deltaInfo); 131 132 NodeComparison compare = (NodeComparison) delta.getData(pathInDelta); 134 int comparison = compare.getUserComparison(); 135 result.setStatus(comparison); 136 if (comparison == IResourceDelta.NO_CHANGE || Path.ROOT.equals(pathInTree)) { 137 ResourceInfo info = workspace.getResourceInfo(pathInTree, true, false); 138 result.setOldInfo(info); 139 result.setNewInfo(info); 140 } else { 141 result.setOldInfo((ResourceInfo) compare.getOldData()); 142 result.setNewInfo((ResourceInfo) compare.getNewData()); 143 } 144 IPath[] childKeys = delta.getChildren(pathInDelta); 146 int numChildren = childKeys.length; 147 if (numChildren == 0) { 148 result.setChildren(NO_CHILDREN); 149 } else { 150 ResourceDelta[] children = new ResourceDelta[numChildren]; 151 for (int i = 0; i < numChildren; i++) { 152 IPath newTreePath = pathInTree == pathInDelta ? childKeys[i] : pathInTree.append(childKeys[i].lastSegment()); 154 children[i] = createDelta(workspace, delta, deltaInfo, newTreePath, childKeys[i]); 155 } 156 result.setChildren(children); 157 } 158 159 int status = result.status; 161 if ((status & IResourceDelta.ALL_WITH_PHANTOMS) == 0 && numChildren != 0) 162 result.setStatus(status |= IResourceDelta.CHANGED); 163 164 return result; 166 } 167 168 174 public static IResourceDelta newEmptyDelta(IProject project) { 175 ResourceDelta result = new ResourceDelta(project.getFullPath(), new ResourceDeltaInfo(((Workspace) project.getWorkspace()), null, ResourceComparator.getBuildComparator())); 176 result.setStatus(0); 177 result.setChildren(NO_CHILDREN); 178 ResourceInfo info = ((Project) project).getResourceInfo(true, false); 179 result.setOldInfo(info); 180 result.setNewInfo(info); 181 return result; 182 } 183 } 184 | Popular Tags |