1 11 package org.eclipse.team.core.synchronize; 12 13 import org.eclipse.core.resources.IResource; 14 import org.eclipse.core.runtime.Assert; 15 import org.eclipse.core.runtime.IAdaptable; 16 import org.eclipse.osgi.util.NLS; 17 import org.eclipse.team.core.TeamException; 18 import org.eclipse.team.core.variants.IResourceVariant; 19 import org.eclipse.team.core.variants.IResourceVariantComparator; 20 import org.eclipse.team.internal.core.Messages; 21 22 62 public class SyncInfo implements IAdaptable { 63 64 67 68 71 public static final int IN_SYNC = 0; 72 73 76 public static final int ADDITION = 1; 77 78 81 public static final int DELETION = 2; 82 83 86 public static final int CHANGE = 3; 87 88 91 public static final int CHANGE_MASK = CHANGE; 92 93 96 97 100 public static final int OUTGOING = 4; 101 102 105 public static final int INCOMING = 8; 106 107 110 public static final int CONFLICTING = 12; 111 112 115 public static final int DIRECTION_MASK = CONFLICTING; 116 117 120 121 125 public static final int PSEUDO_CONFLICT = 16; 126 127 132 public static final int AUTOMERGE_CONFLICT = 32; 133 134 139 public static final int MANUAL_CONFLICT = 64; 140 141 144 private IResource local; 145 private IResourceVariant base; 146 private IResourceVariant remote; 147 private IResourceVariantComparator comparator; 148 149 private int syncKind; 150 151 158 public SyncInfo(IResource local, IResourceVariant base, IResourceVariant remote, IResourceVariantComparator comparator) { 159 Assert.isNotNull(local); 160 Assert.isNotNull(comparator); 161 this.local = local; 162 this.base = base; 163 this.remote = remote; 164 this.comparator = comparator; 165 } 166 167 173 public IResource getLocal() { 174 return local; 175 } 176 177 183 public String getLocalContentIdentifier() { 184 return null; 185 } 186 187 197 public IResourceVariant getBase() { 198 return base; 199 } 200 201 211 public IResourceVariant getRemote() { 212 return remote; 213 } 214 215 222 public IResourceVariantComparator getComparator() { 223 return comparator; 224 } 225 226 231 public int getKind() { 232 return syncKind; 233 } 234 235 242 static public boolean isInSync(int kind) { 243 return kind == IN_SYNC; 244 } 245 246 254 static public int getDirection(int kind) { 255 return kind & DIRECTION_MASK; 256 } 257 258 267 static public int getChange(int kind) { 268 return kind & CHANGE_MASK; 269 } 270 271 274 public boolean equals(Object other) { 275 if(other == this) return true; 276 if(other instanceof SyncInfo) { 277 return equalNodes(this, (SyncInfo)other); 278 } 279 return false; 280 } 281 282 285 public int hashCode() { 286 return getLocal().hashCode(); 287 } 288 289 private boolean equalNodes(SyncInfo node1, SyncInfo node2) { 290 if(node1 == null || node2 == null) { 291 return false; 292 } 293 294 IResource local1 = null; 296 if (node1.getLocal() != null) 297 local1 = node1.getLocal(); 298 IResource local2 = null; 299 if (node2.getLocal() != null) 300 local2 = node2.getLocal(); 301 if (!equalObjects(local1, local2)) return false; 302 303 IResourceVariant base1 = null; 305 if (node1.getBase() != null) 306 base1 = node1.getBase(); 307 IResourceVariant base2 = null; 308 if (node2.getBase() != null) 309 base2 = node2.getBase(); 310 if (!equalObjects(base1, base2)) return false; 311 312 IResourceVariant remote1 = null; 314 if (node1.getRemote() != null) 315 remote1 = node1.getRemote(); 316 IResourceVariant remote2 = null; 317 if (node2.getRemote() != null) 318 remote2 = node2.getRemote(); 319 if (!equalObjects(remote1, remote2)) return false; 320 321 return true; 322 } 323 324 private boolean equalObjects(Object o1, Object o2) { 325 if (o1 == null && o2 == null) return true; 326 if (o1 == null || o2 == null) return false; 327 return o1.equals(o2); 328 } 329 330 333 public Object getAdapter(Class adapter) { 334 if (adapter == IResource.class) { 335 return getLocal(); 336 } 337 return null; 338 } 339 340 343 public String toString() { 344 return getLocal().getName() + " " + kindToString(getKind()); } 346 347 354 public static String kindToString(int kind) { 355 String label = ""; if(kind==IN_SYNC) { 357 label = Messages.RemoteSyncElement_insync; 358 } else { 359 switch(kind & DIRECTION_MASK) { 360 case CONFLICTING: label = Messages.RemoteSyncElement_conflicting; break; 361 case OUTGOING: label = Messages.RemoteSyncElement_outgoing; break; 362 case INCOMING: label = Messages.RemoteSyncElement_incoming; break; 363 } 364 switch(kind & CHANGE_MASK) { 365 case CHANGE: label = NLS.bind(Messages.concatStrings, new String [] { label, Messages.RemoteSyncElement_change }); break; case ADDITION: label = NLS.bind(Messages.concatStrings, new String [] { label, Messages.RemoteSyncElement_addition }); break; case DELETION: label = NLS.bind(Messages.concatStrings, new String [] { label, Messages.RemoteSyncElement_deletion }); break; } 369 if((kind & MANUAL_CONFLICT) != 0) { 370 label = NLS.bind(Messages.concatStrings, new String [] { label, Messages.RemoteSyncElement_manual }); } 372 if((kind & AUTOMERGE_CONFLICT) != 0) { 373 label = NLS.bind(Messages.concatStrings, new String [] { label, Messages.RemoteSyncElement_auto }); } 375 } 376 return NLS.bind(Messages.RemoteSyncElement_delimit, new String [] { label }); 377 } 378 379 387 public final void init() throws TeamException { 388 syncKind = calculateKind(); 389 } 390 391 401 protected int calculateKind() throws TeamException { 402 int description = IN_SYNC; 403 404 boolean localExists = local.exists(); 405 406 if (comparator.isThreeWay()) { 407 if (base == null) { 408 if (remote == null) { 409 if (!localExists) { 410 description = IN_SYNC; 411 } else { 412 description = OUTGOING | ADDITION; 413 } 414 } else { 415 if (!localExists) { 416 description = INCOMING | ADDITION; 417 } else { 418 description = CONFLICTING | ADDITION; 419 if (comparator.compare(local, remote)) { 420 description |= PSEUDO_CONFLICT; 421 } 422 } 423 } 424 } else { 425 if (!localExists) { 426 if (remote == null) { 427 description = CONFLICTING | DELETION | PSEUDO_CONFLICT; 428 } else { 429 if (comparator.compare(base, remote)) 430 description = OUTGOING | DELETION; 431 else 432 description = CONFLICTING | CHANGE; 433 } 434 } else { 435 if (remote == null) { 436 if (comparator.compare(local, base)) 437 description = INCOMING | DELETION; 438 else 439 description = CONFLICTING | CHANGE; 440 } else { 441 boolean ay = comparator.compare(local, base); 442 boolean am = comparator.compare(base, remote); 443 if (ay && am) { 444 } else if (ay && !am) { 446 description = INCOMING | CHANGE; 447 } else if (!ay && am) { 448 description = OUTGOING | CHANGE; 449 } else { 450 if(! comparator.compare(local, remote)) { 451 description = CONFLICTING | CHANGE; 452 } 453 } 454 } 455 } 456 } 457 } else { if (remote == null) { 459 if (!localExists) { 460 Assert.isTrue(false); 461 } else { 463 description= DELETION; 464 } 465 } else { 466 if (!localExists) { 467 description= ADDITION; 468 } else { 469 if (! comparator.compare(local, remote)) 470 description= CHANGE; 471 } 472 } 473 } 474 return description; 475 } 476 } 477 | Popular Tags |