1 11 package org.eclipse.team.internal.core.subscribers; 12 13 import java.util.*; 14 15 import org.eclipse.core.internal.resources.mapping.*; 16 import org.eclipse.core.resources.*; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.team.core.TeamException; 19 import org.eclipse.team.core.subscribers.Subscriber; 20 import org.eclipse.team.core.synchronize.SyncInfo; 21 import org.eclipse.team.core.synchronize.SyncInfoFilter; 22 import org.eclipse.team.core.variants.IResourceVariant; 23 import org.eclipse.team.internal.core.*; 24 25 33 public class SubscriberResourceMappingContext extends RemoteResourceMappingContext { 34 35 private final Subscriber subscriber; 36 private final SyncInfoFilter contentDiffFilter; 37 38 Set shallowRefresh = new HashSet(); 40 Set deepRefresh = new HashSet(); 41 42 46 public static ResourceMappingContext getReplaceContext(Subscriber subscriber) { 47 return new SubscriberResourceMappingContext(subscriber, new SyncInfoFilter() { 48 public boolean select(SyncInfo info, IProgressMonitor monitor) { 49 if (info != null) { 50 int direction = info.getKind() & SyncInfo.DIRECTION_MASK; 51 return direction != 0; 53 } 54 return false; 55 } 56 57 }); 58 } 59 60 66 public static ResourceMappingContext getUpdateContext(Subscriber subscriber) { 67 return new SubscriberResourceMappingContext(subscriber, new SyncInfoFilter() { 68 public boolean select(SyncInfo info, IProgressMonitor monitor) { 69 if (info != null) { 70 int direction = info.getKind() & SyncInfo.DIRECTION_MASK; 71 return direction == SyncInfo.INCOMING || direction == SyncInfo.CONFLICTING ; 73 } 74 return false; 75 } 76 77 }); 78 } 79 80 86 public static ResourceMappingContext getCheckInContext(Subscriber subscriber) { 87 return new SubscriberResourceMappingContext(subscriber, new SyncInfoFilter() { 88 public boolean select(SyncInfo info, IProgressMonitor monitor) { 89 if (info != null) { 90 int direction = info.getKind() & SyncInfo.DIRECTION_MASK; 91 return direction == SyncInfo.OUTGOING || direction == SyncInfo.CONFLICTING ; 93 } 94 return false; 95 } 96 97 }); 98 } 99 100 106 public static ResourceMappingContext getCompareContext(Subscriber subscriber) { 107 return new SubscriberResourceMappingContext(subscriber, new SyncInfoFilter() { 108 public boolean select(SyncInfo info, IProgressMonitor monitor) { 109 if (info != null) { 110 return info.getKind() != SyncInfo.IN_SYNC; 111 } 112 return false; 113 } 114 115 }); 116 } 117 118 124 public SubscriberResourceMappingContext(Subscriber subscriber, SyncInfoFilter contentDiffFilter) { 125 this.subscriber = subscriber; 126 this.contentDiffFilter = contentDiffFilter; 127 } 128 129 132 public final boolean contentDiffers(IFile file, IProgressMonitor monitor) throws CoreException { 133 try { 134 monitor.beginTask(null, 100); 135 ensureRefreshed(file, IResource.DEPTH_ZERO, NONE, Policy.subMonitorFor(monitor, 10)); 136 SyncInfo syncInfo = subscriber.getSyncInfo(file); 137 validateRemote(file, syncInfo); 138 return syncInfo != null && contentDiffFilter.select(syncInfo, Policy.subMonitorFor(monitor, 90)); 139 } finally { 140 monitor.done(); 141 } 142 } 143 144 147 public final IStorage fetchContents(IFile file, IProgressMonitor monitor) throws CoreException { 148 try { 149 monitor.beginTask(null, 100); 150 ensureRefreshed(file, IResource.DEPTH_ZERO, FILE_CONTENTS_REQUIRED, Policy.subMonitorFor(monitor, 10)); 151 SyncInfo syncInfo = subscriber.getSyncInfo(file); 152 IResourceVariant remote = validateRemote(file, syncInfo); 153 if (remote == null) { 154 return null; 155 } 156 return remote.getStorage(Policy.subMonitorFor(monitor, 90)); 157 } finally { 158 monitor.done(); 159 } 160 } 161 162 165 public final IResource[] fetchMembers(IContainer container, IProgressMonitor monitor) throws CoreException { 166 try { 167 monitor.beginTask(null, 100); 168 ensureRefreshed(container, IResource.DEPTH_ONE, NONE, Policy.subMonitorFor(monitor, 100)); 169 SyncInfo syncInfo = subscriber.getSyncInfo(container); 170 if (validateRemote(container, syncInfo) == null) { 171 return null; 173 } 174 return subscriber.members(container); 175 } finally { 176 monitor.done(); 177 } 178 } 179 180 183 public final void refresh(ResourceTraversal[] traversals, int flags, IProgressMonitor monitor) throws CoreException { 184 Set zero = new HashSet(); 185 Set one = new HashSet(); 186 Set infinite = new HashSet(); 187 for (int i = 0; i < traversals.length; i++) { 188 ResourceTraversal traversal = traversals[i]; 189 switch (traversal.getDepth()) { 190 case IResource.DEPTH_INFINITE: 191 infinite.addAll(Arrays.asList(traversal.getResources())); 192 break; 193 case IResource.DEPTH_ONE: 194 one.addAll(Arrays.asList(traversal.getResources())); 195 break; 196 case IResource.DEPTH_ZERO: 197 zero.addAll(Arrays.asList(traversal.getResources())); 198 break; 199 } 200 } 201 if (!zero.isEmpty()) 202 refresh((IResource[]) zero.toArray(new IResource[zero.size()]), IResource.DEPTH_ZERO, flags, monitor); 203 if (!one.isEmpty()) 204 refresh((IResource[]) one.toArray(new IResource[one.size()]), IResource.DEPTH_ONE, flags, monitor); 205 if (!infinite.isEmpty()) 206 refresh((IResource[]) infinite.toArray(new IResource[infinite.size()]), IResource.DEPTH_INFINITE, flags, monitor); 207 } 208 209 220 protected void refresh(IResource[] resources, int depth, int flags, IProgressMonitor monitor) throws TeamException { 221 subscriber.refresh(resources, depth, monitor); 222 refreshed(resources, depth); 223 } 224 225 232 protected final void refreshed(IResource[] resources, int depth) { 233 for (int i = 0; i < resources.length; i++) { 234 IResource resource = resources[i]; 235 if (depth == IResource.DEPTH_ONE || resource.getType() == IResource.FILE) { 237 shallowRefresh.add(resource); 238 } else if (depth == IResource.DEPTH_INFINITE) { 239 deepRefresh.add(resource); 240 } 241 } 242 } 243 244 248 private void ensureRefreshed(IResource resource, int depth, int flags, IProgressMonitor monitor) throws TeamException { 249 if (depth == IResource.DEPTH_INFINITE) { 250 if (wasRefreshedDeeply(resource)) 252 return; 253 if (resource.getType() == IResource.FILE && wasRefreshedShallow(resource)) 255 return; 256 } else { 257 if (wasRefreshedShallow(resource)) 258 return; 259 } 260 refresh(new IResource[] { resource }, depth, flags, monitor); 261 } 262 263 268 private boolean wasRefreshedShallow(IResource resource) { 269 if (shallowRefresh.contains(resource)) 270 return true; 271 if (resource.getType() == IResource.FILE && shallowRefresh.contains(resource.getParent())) 272 return true; 273 if (wasRefreshedDeeply(resource)) 274 return true; 275 return false; 276 } 277 278 281 private boolean wasRefreshedDeeply(IResource resource) { 282 if (resource.getType() == IResource.ROOT) 283 return false; 284 if (deepRefresh.contains(resource)) 285 return true; 286 return wasRefreshedDeeply(resource.getParent()); 287 } 288 289 293 private IResourceVariant validateRemote(IResource resource, SyncInfo syncInfo) throws CoreException { 294 if (syncInfo == null) return null; 295 IResourceVariant remote = syncInfo.getRemote(); 296 if (remote == null) return null; 297 boolean containerExpected = resource.getType() != IResource.FILE; 298 if (remote.isContainer() && !containerExpected) { 299 throw new CoreException(new Status(IStatus.ERROR, TeamPlugin.ID, IResourceStatus.RESOURCE_WRONG_TYPE, Messages.SubscriberResourceMappingContext_0 + resource.getFullPath().toString(), null)); 300 } else if (!remote.isContainer() && containerExpected) { 301 throw new CoreException(new Status(IStatus.ERROR, TeamPlugin.ID, IResourceStatus.RESOURCE_WRONG_TYPE, Messages.SubscriberResourceMappingContext_1 + resource.getFullPath().toString(), null)); 302 } 303 return remote; 304 } 305 } 306 | Popular Tags |