1 11 package org.eclipse.team.core.mapping.provider; 12 13 import java.util.*; 14 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.resources.ResourcesPlugin; 17 import org.eclipse.core.resources.mapping.ResourceTraversal; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.team.core.diff.*; 20 import org.eclipse.team.core.diff.provider.DiffTree; 21 import org.eclipse.team.core.mapping.IResourceDiff; 22 import org.eclipse.team.core.mapping.IResourceDiffTree; 23 24 31 public class ResourceDiffTree extends DiffTree implements IResourceDiffTree { 32 33 39 public static IResource getResourceFor(IDiff node) { 40 if (node instanceof IResourceDiff) { 41 IResourceDiff rd = (IResourceDiff) node; 42 return rd.getResource(); 43 } 44 if (node instanceof IThreeWayDiff) { 45 IThreeWayDiff twd = (IThreeWayDiff) node; 46 IDiff child = twd.getLocalChange(); 47 if (child != null) 48 return getResourceFor(child); 49 child = twd.getRemoteChange(); 50 if (child != null) 51 return getResourceFor(child); 52 } 53 Assert.isLegal(false); 54 return null; 55 } 56 57 60 public IDiff getDiff(IResource resource) { 61 return getDiff(resource.getFullPath()); 62 } 63 64 67 public IResource getResource(IDiff diff) { 68 if (diff instanceof IThreeWayDiff) { 69 IThreeWayDiff twd = (IThreeWayDiff) diff; 70 IResourceDiff localChange = ((IResourceDiff)twd.getLocalChange()); 71 if (localChange != null) 72 return localChange.getResource(); 73 return ((IResourceDiff)twd.getRemoteChange()).getResource(); 74 } else { 75 return ((IResourceDiff)diff).getResource(); 76 } 77 } 78 79 82 public void accept(ResourceTraversal[] traversals, IDiffVisitor visitor) { 83 for (int i = 0; i < traversals.length; i++) { 84 ResourceTraversal traversal = traversals[i]; 85 IResource[] resources = traversal.getResources(); 86 for (int j = 0; j < resources.length; j++) { 87 IResource resource = resources[j]; 88 accept(resource.getFullPath(), visitor, traversal.getDepth()); 89 } 90 } 91 } 92 93 96 public IDiff[] getDiffs(final ResourceTraversal[] traversals) { 97 final Set result = new HashSet(); 98 for (int i = 0; i < traversals.length; i++) { 99 ResourceTraversal traversal = traversals[i]; 100 IResource[] resources = traversal.getResources(); 101 for (int j = 0; j < resources.length; j++) { 102 IResource resource = resources[j]; 103 internalGetDiffs(resource, traversal.getDepth(), result); 104 } 105 } 106 return (IDiff[]) result.toArray(new IDiff[result.size()]); 107 } 108 109 112 public IDiff[] getDiffs(IResource resource, int depth) { 113 final Set result = new HashSet(); 114 internalGetDiffs(resource, depth, result); 115 return (IDiff[]) result.toArray(new IDiff[result.size()]); 116 } 117 118 private void internalGetDiffs(IResource resource, int depth, final Set result) { 119 accept(resource.getFullPath(), new IDiffVisitor() { 120 public boolean visit(IDiff diff) { 121 return result.add(diff); 122 } 123 }, depth); 124 } 125 126 private IResource internalGetResource(IPath fullPath, boolean container) { 127 if (container) { 128 if (fullPath.segmentCount() == 1) 129 return ResourcesPlugin.getWorkspace().getRoot().getProject(fullPath.segment(0)); 130 return ResourcesPlugin.getWorkspace().getRoot().getFolder(fullPath); 131 } 132 return ResourcesPlugin.getWorkspace().getRoot().getFile(fullPath); 133 } 134 135 138 public IResource[] members(IResource resource) { 139 List result = new ArrayList(); 140 IPath[] paths = getChildren(resource.getFullPath()); 141 for (int i = 0; i < paths.length; i++) { 142 IPath path = paths[i]; 143 IDiff node = getDiff(path); 144 if (node == null) { 145 result.add(internalGetResource(path, true)); 146 } else { 147 result.add(getResource(node)); 148 } 149 } 150 return (IResource[]) result.toArray(new IResource[result.size()]); 151 } 152 153 156 public IResource[] getAffectedResources() { 157 List result = new ArrayList(); 158 IDiff[] nodes = getDiffs(); 159 for (int i = 0; i < nodes.length; i++) { 160 IDiff node = nodes[i]; 161 result.add(getResource(node)); 162 } 163 return (IResource[]) result.toArray(new IResource[result.size()]); 164 } 165 166 169 public void add(IDiff delta) { 170 Assert.isTrue(delta instanceof IResourceDiff || delta instanceof IThreeWayDiff); 171 super.add(delta); 172 } 173 174 179 public void remove(IResource resource) { 180 remove(resource.getFullPath()); 181 } 182 183 public boolean hasMatchingDiffs(ResourceTraversal[] traversals, final FastDiffFilter filter) { 184 final RuntimeException found = new RuntimeException (); 185 try { 186 accept(traversals, new IDiffVisitor() { 187 public boolean visit(IDiff delta) { 188 if (filter.select(delta)) { 189 throw found; 190 } 191 return false; 192 } 193 194 }); 195 } catch (RuntimeException e) { 196 if (e == found) 197 return true; 198 throw e; 199 } 200 return false; 201 } 202 } 203 | Popular Tags |