1 11 package org.eclipse.team.core.subscribers; 12 13 import java.util.*; 14 15 import org.eclipse.core.resources.*; 16 import org.eclipse.core.resources.mapping.RemoteResourceMappingContext; 17 import org.eclipse.core.resources.mapping.ResourceTraversal; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.team.core.TeamException; 20 import org.eclipse.team.core.synchronize.SyncInfo; 21 import org.eclipse.team.core.variants.IResourceVariant; 22 import org.eclipse.team.internal.core.*; 23 24 32 public class SubscriberResourceMappingContext extends RemoteResourceMappingContext { 33 34 private final Subscriber subscriber; 35 36 private Set shallowRefresh = new HashSet(); 38 private Set deepRefresh = new HashSet(); 39 private boolean autoRefresh; 40 41 48 public static RemoteResourceMappingContext createContext(Subscriber subscriber) { 49 return new SubscriberResourceMappingContext(subscriber, true); 50 } 51 52 58 public SubscriberResourceMappingContext(Subscriber subscriber, boolean autoRefresh) { 59 this.subscriber = subscriber; 60 this.autoRefresh = autoRefresh; 61 } 62 63 66 public final boolean hasRemoteChange(IResource resource, IProgressMonitor monitor) throws CoreException { 67 try { 68 monitor.beginTask(null, 100); 69 ensureRefreshed(resource, IResource.DEPTH_ONE, NONE, monitor); 70 SyncInfo syncInfo = subscriber.getSyncInfo(resource); 71 validateRemote(resource, syncInfo); 72 if (syncInfo == null) return false; 73 int direction = SyncInfo.getDirection(syncInfo.getKind()); 74 return direction == SyncInfo.OUTGOING || direction == SyncInfo.CONFLICTING; 75 } finally { 76 monitor.done(); 77 } 78 } 79 80 83 public boolean hasLocalChange(IResource resource, IProgressMonitor monitor) throws CoreException { 84 SyncInfo syncInfo = subscriber.getSyncInfo(resource); 85 if (syncInfo == null) return false; 86 int direction = SyncInfo.getDirection(syncInfo.getKind()); 87 return direction == SyncInfo.OUTGOING || direction == SyncInfo.CONFLICTING; 88 } 89 90 93 public final IStorage fetchRemoteContents(IFile file, IProgressMonitor monitor) throws CoreException { 94 try { 95 monitor.beginTask(null, 100); 96 ensureRefreshed(file, IResource.DEPTH_ZERO, FILE_CONTENTS_REQUIRED, Policy.subMonitorFor(monitor, 10)); 97 SyncInfo syncInfo = subscriber.getSyncInfo(file); 98 IResourceVariant remote = validateRemote(file, syncInfo); 99 if (remote == null) { 100 return null; 101 } 102 return remote.getStorage(Policy.subMonitorFor(monitor, 90)); 103 } finally { 104 monitor.done(); 105 } 106 } 107 108 111 public final IStorage fetchBaseContents(IFile file, IProgressMonitor monitor) throws CoreException { 112 try { 113 monitor.beginTask(null, 100); 114 ensureRefreshed(file, IResource.DEPTH_ZERO, FILE_CONTENTS_REQUIRED, Policy.subMonitorFor(monitor, 10)); 115 SyncInfo syncInfo = subscriber.getSyncInfo(file); 116 IResourceVariant base = validateBase(file, syncInfo); 117 if (base == null) { 118 return null; 119 } 120 return base.getStorage(Policy.subMonitorFor(monitor, 90)); 121 } finally { 122 monitor.done(); 123 } 124 } 125 126 129 public final IResource[] fetchMembers(IContainer container, IProgressMonitor monitor) throws CoreException { 130 try { 131 monitor.beginTask(null, 100); 132 ensureRefreshed(container, IResource.DEPTH_ONE, NONE, Policy.subMonitorFor(monitor, 100)); 133 SyncInfo syncInfo = subscriber.getSyncInfo(container); 134 if (validateRemote(container, syncInfo) == null) { 135 return new IResource[0]; 137 } 138 return subscriber.members(container); 139 } finally { 140 monitor.done(); 141 } 142 } 143 144 147 public final void refresh(ResourceTraversal[] traversals, int flags, IProgressMonitor monitor) throws CoreException { 148 subscriber.refresh(traversals, monitor); 149 for (int i = 0; i < traversals.length; i++) { 150 ResourceTraversal traversal = traversals[i]; 151 refreshed(traversal.getResources(), traversal.getDepth()); 152 } 153 } 154 155 166 protected void refresh(IResource[] resources, int depth, int flags, IProgressMonitor monitor) throws TeamException { 167 subscriber.refresh(resources, depth, monitor); 168 refreshed(resources, depth); 169 } 170 171 178 protected final void refreshed(IResource[] resources, int depth) { 179 for (int i = 0; i < resources.length; i++) { 180 IResource resource = resources[i]; 181 if (depth == IResource.DEPTH_ONE || resource.getType() == IResource.FILE) { 183 shallowRefresh.add(resource); 184 } else if (depth == IResource.DEPTH_INFINITE) { 185 deepRefresh.add(resource); 186 } 187 } 188 } 189 190 194 private void ensureRefreshed(IResource resource, int depth, int flags, IProgressMonitor monitor) throws TeamException { 195 if (autoRefresh) { 196 if (depth == IResource.DEPTH_INFINITE) { 197 if (wasRefreshedDeeply(resource)) 199 return; 200 if (resource.getType() == IResource.FILE && wasRefreshedShallow(resource)) 202 return; 203 } else { 204 if (wasRefreshedShallow(resource)) 205 return; 206 } 207 refresh(new IResource[] { resource }, depth, flags, monitor); 208 } 209 } 210 211 216 private boolean wasRefreshedShallow(IResource resource) { 217 if (shallowRefresh.contains(resource)) 218 return true; 219 if (resource.getType() == IResource.FILE && shallowRefresh.contains(resource.getParent())) 220 return true; 221 if (wasRefreshedDeeply(resource)) 222 return true; 223 return false; 224 } 225 226 229 private boolean wasRefreshedDeeply(IResource resource) { 230 if (resource.getType() == IResource.ROOT) 231 return false; 232 if (deepRefresh.contains(resource)) 233 return true; 234 return wasRefreshedDeeply(resource.getParent()); 235 } 236 237 241 private IResourceVariant validateRemote(IResource resource, SyncInfo syncInfo) throws CoreException { 242 if (syncInfo == null) return null; 243 IResourceVariant remote = syncInfo.getRemote(); 244 if (remote == null) return null; 245 return validateRemote(resource, remote); 246 } 247 248 private IResourceVariant validateRemote(IResource resource, IResourceVariant remote) throws CoreException { 249 boolean containerExpected = resource.getType() != IResource.FILE; 250 if (remote.isContainer() && !containerExpected) { 251 throw new CoreException(new Status(IStatus.ERROR, TeamPlugin.ID, IResourceStatus.RESOURCE_WRONG_TYPE, Messages.SubscriberResourceMappingContext_0 + resource.getFullPath().toString(), null)); 252 } else if (!remote.isContainer() && containerExpected) { 253 throw new CoreException(new Status(IStatus.ERROR, TeamPlugin.ID, IResourceStatus.RESOURCE_WRONG_TYPE, Messages.SubscriberResourceMappingContext_1 + resource.getFullPath().toString(), null)); 254 } 255 return remote; 256 } 257 258 262 private IResourceVariant validateBase(IResource resource, SyncInfo syncInfo) throws CoreException { 263 if (syncInfo == null) return null; 264 IResourceVariant base = syncInfo.getBase(); 265 if (base == null) return null; 266 return validateRemote(resource, base); 267 } 268 269 277 public void setAutoRefresh(boolean autoRefresh) { 278 this.autoRefresh = autoRefresh; 279 } 280 281 284 public boolean isThreeWay() { 285 return subscriber.getResourceComparator().isThreeWay(); 286 } 287 288 291 public boolean contentDiffers(IFile file, IProgressMonitor monitor) throws CoreException { 292 return hasRemoteChange(file, monitor) || hasLocalChange(file, monitor); 293 } 294 295 public IProject[] getProjects() { 296 Set projects = new HashSet(); 297 IResource[] roots = subscriber.roots(); 298 for (int i = 0; i < roots.length; i++) { 299 IResource resource = roots[i]; 300 projects.add(resource.getProject()); 301 } 302 return (IProject[]) projects.toArray(new IProject[projects.size()]); 303 } 304 } 305 | Popular Tags |