1 11 package org.eclipse.team.core.variants; 12 13 import java.util.*; 14 15 import org.eclipse.core.resources.IContainer; 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.osgi.util.NLS; 19 import org.eclipse.team.core.TeamException; 20 import org.eclipse.team.internal.core.*; 21 22 34 public abstract class AbstractResourceVariantTree implements IResourceVariantTree { 35 36 49 public IResource[] refresh(IResource[] resources, int depth, IProgressMonitor monitor) throws TeamException { 50 List changedResources = new ArrayList(); 51 monitor.beginTask(null, 100 * resources.length); 52 for (int i = 0; i < resources.length; i++) { 53 IResource resource = resources[i]; 54 IResource[] changed = refresh(resource, depth, Policy.subMonitorFor(monitor, 100)); 55 changedResources.addAll(Arrays.asList(changed)); 56 } 57 monitor.done(); 58 return (IResource[]) changedResources.toArray(new IResource[changedResources.size()]); 59 } 60 61 77 protected IResource[] refresh(IResource resource, int depth, IProgressMonitor monitor) throws TeamException { 78 IResource[] changedResources = null; 79 monitor.beginTask(null, 100); 80 try { 81 monitor.setTaskName(NLS.bind(Messages.SynchronizationCacheRefreshOperation_0, new String [] { resource.getFullPath().makeRelative().toString() })); 82 83 IResourceVariant tree = fetchVariant(resource, depth, Policy.subMonitorFor(monitor, 70)); 85 86 IProgressMonitor sub = Policy.infiniteSubMonitorFor(monitor, 30); 88 try { 89 sub.beginTask(null, 64); 90 changedResources = collectChanges(resource, tree, depth, Policy.subMonitorFor(sub, 64)); 91 } finally { 92 sub.done(); 93 } 94 } finally { 95 monitor.done(); 96 } 97 if (changedResources == null) return new IResource[0]; 98 return changedResources; 99 } 100 101 111 protected IResource[] collectChanges(IResource local, IResourceVariant remote, int depth, IProgressMonitor monitor) throws TeamException { 112 List changedResources = new ArrayList(); 113 collectChanges(local, remote, changedResources, depth, monitor); 114 return (IResource[]) changedResources.toArray(new IResource[changedResources.size()]); 115 } 116 117 125 protected abstract IResourceVariant[] fetchMembers(IResourceVariant variant, IProgressMonitor progress) throws TeamException; 126 127 141 protected abstract IResourceVariant fetchVariant(IResource resource, int depth, IProgressMonitor monitor) throws TeamException; 142 143 152 protected IResource[] collectedMembers(IResource local, IResource[] members) throws TeamException { 153 return new IResource[0]; 154 } 155 156 165 protected abstract boolean setVariant(IResource local, IResourceVariant remote) throws TeamException; 166 167 private void collectChanges(IResource local, IResourceVariant remote, Collection changedResources, int depth, IProgressMonitor monitor) throws TeamException { 168 boolean changed = setVariant(local, remote); 169 if (changed) { 170 changedResources.add(local); 171 } 172 if (depth == IResource.DEPTH_ZERO) return; 173 Map children = mergedMembers(local, remote, monitor); 174 for (Iterator it = children.keySet().iterator(); it.hasNext();) { 175 IResource localChild = (IResource) it.next(); 176 IResourceVariant remoteChild = (IResourceVariant)children.get(localChild); 177 collectChanges(localChild, remoteChild, changedResources, 178 depth == IResource.DEPTH_INFINITE ? IResource.DEPTH_INFINITE : IResource.DEPTH_ZERO, 179 monitor); 180 } 181 182 IResource[] cleared = collectedMembers(local, (IResource[]) children.keySet().toArray(new IResource[children.keySet().size()])); 183 changedResources.addAll(Arrays.asList(cleared)); 184 monitor.worked(1); 185 } 186 187 private Map mergedMembers(IResource local, IResourceVariant remote, IProgressMonitor progress) throws TeamException { 188 189 Map mergedResources = new HashMap(); 191 192 IResourceVariant[] remoteChildren; 193 if (remote == null) { 194 remoteChildren = new IResourceVariant[0]; 195 } else { 196 remoteChildren = fetchMembers(remote, progress); 197 } 198 199 200 IResource[] localChildren = members(local); 201 202 if (remoteChildren.length > 0 || localChildren.length > 0) { 203 Set allSet = new HashSet(20); 204 Map localSet = null; 205 Map remoteSet = null; 206 207 if (localChildren.length > 0) { 208 localSet = new HashMap(10); 209 for (int i = 0; i < localChildren.length; i++) { 210 IResource localChild = localChildren[i]; 211 String name = localChild.getName(); 212 localSet.put(name, localChild); 213 allSet.add(name); 214 } 215 } 216 217 if (remoteChildren.length > 0) { 218 remoteSet = new HashMap(10); 219 for (int i = 0; i < remoteChildren.length; i++) { 220 IResourceVariant remoteChild = remoteChildren[i]; 221 String name = remoteChild.getName(); 222 remoteSet.put(name, remoteChild); 223 allSet.add(name); 224 } 225 } 226 227 Iterator e = allSet.iterator(); 228 while (e.hasNext()) { 229 String keyChildName = (String ) e.next(); 230 231 Policy.checkCanceled(progress); 232 233 IResource localChild = 234 localSet != null ? (IResource) localSet.get(keyChildName) : null; 235 236 IResourceVariant remoteChild = 237 remoteSet != null ? (IResourceVariant) remoteSet.get(keyChildName) : null; 238 239 if (localChild == null) { 240 Assert.isTrue(remoteChild != null); 242 boolean isContainer = remoteChild.isContainer(); 243 localChild = getResourceChild(local , keyChildName, isContainer); 244 } 245 if (localChild == null) { 246 TeamPlugin.log(IStatus.ERROR, NLS.bind("File {0} cannot be the parent of remote resource {1}", new Object [] { local.getFullPath(), keyChildName }), null); 248 } else { 249 mergedResources.put(localChild, remoteChild); 250 } 251 } 252 } 253 return mergedResources; 254 } 255 256 private IResource getResourceChild(IResource parent, String childName, boolean isContainer) { 257 if (parent.getType() == IResource.FILE) { 258 return null; 259 } 260 if (isContainer) { 261 return ((IContainer) parent).getFolder(new Path(null, childName)); 262 } else { 263 return ((IContainer) parent).getFile(new Path(null, childName)); 264 } 265 } 266 267 } 268 | Popular Tags |