1 11 package org.eclipse.team.core.subscribers; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.resources.mapping.ResourceMapping; 18 import org.eclipse.core.resources.mapping.ResourceTraversal; 19 import org.eclipse.core.runtime.*; 20 import org.eclipse.osgi.util.NLS; 21 import org.eclipse.team.core.*; 22 import org.eclipse.team.core.diff.*; 23 import org.eclipse.team.core.synchronize.SyncInfo; 24 import org.eclipse.team.core.synchronize.SyncInfoSet; 25 import org.eclipse.team.core.variants.IResourceVariantComparator; 26 import org.eclipse.team.internal.core.*; 27 import org.eclipse.team.internal.core.mapping.SyncInfoToDiffConverter; 28 29 66 abstract public class Subscriber { 67 68 private List listeners = new ArrayList (1); 69 70 76 abstract public String getName(); 77 78 91 abstract public boolean isSupervised(IResource resource) throws TeamException; 92 93 106 abstract public IResource[] members(IResource resource) throws TeamException; 107 108 116 abstract public IResource[] roots(); 117 118 143 abstract public SyncInfo getSyncInfo(IResource resource) throws TeamException; 144 145 152 abstract public IResourceVariantComparator getResourceComparator(); 153 154 201 abstract public void refresh(IResource[] resources, int depth, IProgressMonitor monitor) throws TeamException; 202 203 212 public void addListener(ISubscriberChangeListener listener) { 213 synchronized (listeners) { 214 if (!listeners.contains(listener)) { 215 listeners.add(listener); 216 } 217 } 218 } 219 220 226 public void removeListener(ISubscriberChangeListener listener) { 227 synchronized (listeners) { 228 listeners.remove(listener); 229 } 230 } 231 232 251 public void collectOutOfSync(IResource[] resources, int depth, SyncInfoSet set, IProgressMonitor monitor) { 252 try { 253 monitor.beginTask(null, 100 * resources.length); 254 for (int i = 0; i < resources.length; i++) { 255 IResource resource = resources[i]; 256 IProgressMonitor subMonitor = Policy.subMonitorFor(monitor, 100); 257 subMonitor.beginTask(null, IProgressMonitor.UNKNOWN); 258 collect(resource, depth, set, subMonitor); 259 subMonitor.done(); 260 } 261 } finally { 262 monitor.done(); 263 } 264 } 265 266 272 protected void fireTeamResourceChange(final ISubscriberChangeEvent[] deltas) { 273 ISubscriberChangeListener[] allListeners; 274 synchronized (listeners) { 276 allListeners = (ISubscriberChangeListener[]) listeners.toArray(new ISubscriberChangeListener[listeners.size()]); 277 } 278 for (int i = 0; i < allListeners.length; i++) { 280 final ISubscriberChangeListener listener = allListeners[i]; 281 SafeRunner.run(new ISafeRunnable() { 282 public void handleException(Throwable exception) { 283 } 286 public void run() throws Exception { 287 listener.subscriberResourceChanged(deltas); 288 } 289 }); 290 } 291 } 292 293 297 private void collect( 298 IResource resource, 299 int depth, 300 SyncInfoSet set, 301 IProgressMonitor monitor) { 302 303 Policy.checkCanceled(monitor); 304 305 if (resource.getType() != IResource.FILE 306 && depth != IResource.DEPTH_ZERO) { 307 try { 308 IResource[] members = members(resource); 309 for (int i = 0; i < members.length; i++) { 310 collect( 311 members[i], 312 depth == IResource.DEPTH_INFINITE 313 ? IResource.DEPTH_INFINITE 314 : IResource.DEPTH_ZERO, 315 set, 316 monitor); 317 } 318 } catch (TeamException e) { 319 set.addError(new TeamStatus(IStatus.ERROR, TeamPlugin.ID, ITeamStatus.SYNC_INFO_SET_ERROR, NLS.bind(Messages.SubscriberEventHandler_8, new String [] { resource.getFullPath().toString(), e.getMessage() }), e, resource)); 320 } 321 } 322 323 monitor.subTask(NLS.bind(Messages.SubscriberEventHandler_2, new String [] { resource.getFullPath().toString() })); 324 try { 325 SyncInfo info = getSyncInfo(resource); 326 if (info == null || info.getKind() == SyncInfo.IN_SYNC) { 327 set.remove(resource); 330 } else { 331 set.add(info); 332 } 333 } catch (TeamException e) { 334 set.addError(new TeamStatus( 335 IStatus.ERROR, TeamPlugin.ID, ITeamStatus.RESOURCE_SYNC_INFO_ERROR, 336 NLS.bind(Messages.SubscriberEventHandler_9, new String [] { resource.getFullPath().toString(), e.getMessage() }), 337 e, resource)); 338 } 339 monitor.worked(1); 341 } 342 343 369 public IDiff getDiff(IResource resource) throws CoreException { 370 SyncInfo info = getSyncInfo(resource); 371 if (info == null || info.getKind() == SyncInfo.IN_SYNC) 372 return null; 373 return SyncInfoToDiffConverter.getDefault().getDeltaFor(info); 374 } 375 376 392 public void accept(ResourceTraversal[] traversals, IDiffVisitor visitor) throws CoreException { 393 for (int i = 0; i < traversals.length; i++) { 394 ResourceTraversal traversal = traversals[i]; 395 accept(traversal.getResources(), traversal.getDepth(), visitor); 396 } 397 } 398 399 420 public void accept(IResource[] resources, int depth, IDiffVisitor visitor) throws CoreException { 421 for (int i = 0; i < resources.length; i++) { 422 IResource resource = resources[i]; 423 accept(resource, depth, visitor); 424 } 425 } 426 427 private void accept(IResource resource, int depth, IDiffVisitor visitor) throws CoreException { 428 IDiff node = getDiff(resource); 429 if (node != null && node.getKind() != IDiff.NO_CHANGE) { 430 if (!visitor.visit(node)) 431 return; 432 } 433 if (depth != IResource.DEPTH_ZERO) { 434 IResource[] members = members(resource); 435 int newDepth = depth == IResource.DEPTH_INFINITE ? IResource.DEPTH_INFINITE : IResource.DEPTH_ZERO; 436 for (int i = 0; i < members.length; i++) { 437 IResource member = members[i]; 438 accept(member, newDepth, visitor); 439 } 440 } 441 } 442 443 461 public void refresh(ResourceTraversal[] traversals, IProgressMonitor monitor) throws TeamException { 462 monitor.beginTask(null, 100 * traversals.length); 463 for (int i = 0; i < traversals.length; i++) { 464 ResourceTraversal traversal = traversals[i]; 465 refresh(traversal.getResources(), traversal.getDepth(), Policy.subMonitorFor(monitor, 100)); 466 } 467 monitor.done(); 468 } 469 470 497 public int getState(ResourceMapping mapping, int stateMask, IProgressMonitor monitor) throws CoreException { 498 ResourceTraversal[] traversals = mapping.getTraversals(new SubscriberResourceMappingContext(this, true), monitor); 499 final int[] direction = new int[] { 0 }; 500 final int[] kind = new int[] { 0 }; 501 accept(traversals, new IDiffVisitor() { 502 public boolean visit(IDiff diff) { 503 if (diff instanceof IThreeWayDiff) { 504 IThreeWayDiff twd = (IThreeWayDiff) diff; 505 direction[0] |= twd.getDirection(); 506 } 507 int diffKind = diff.getKind(); 509 if (kind[0] == 0) 510 kind[0] = diffKind; 511 if (kind[0] != diffKind) { 512 kind[0] = IDiff.CHANGE; 513 } 514 return diffKind == IDiff.CHANGE; 516 } 517 }); 518 return (direction[0] | kind[0]) & stateMask; 519 } 520 } 521 | Popular Tags |