1 11 package org.eclipse.team.internal.core.mapping; 12 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.core.resources.IResource; 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.diff.*; 19 import org.eclipse.team.core.diff.provider.Diff; 20 import org.eclipse.team.core.diff.provider.ThreeWayDiff; 21 import org.eclipse.team.core.history.IFileRevision; 22 import org.eclipse.team.core.history.provider.FileRevision; 23 import org.eclipse.team.core.mapping.IResourceDiff; 24 import org.eclipse.team.core.mapping.provider.ResourceDiff; 25 import org.eclipse.team.core.synchronize.SyncInfo; 26 import org.eclipse.team.core.variants.IResourceVariant; 27 import org.eclipse.team.core.variants.IResourceVariantComparator; 28 import org.eclipse.team.internal.core.Messages; 29 30 33 public class SyncInfoToDiffConverter { 34 35 private static class PrecalculatedSyncInfo extends SyncInfo { 36 public int kind; 37 public PrecalculatedSyncInfo(int kind, IResource local, IResourceVariant base, IResourceVariant remote, IResourceVariantComparator comparator) { 38 super(local, base, remote, comparator); 39 this.kind = kind; 40 } 41 42 protected int calculateKind() throws TeamException { 43 return kind; 44 } 45 } 46 47 private static SyncInfoToDiffConverter instance; 48 49 50 public static String diffKindToString(int kind) { 51 String label = ""; if(kind==IDiff.NO_CHANGE) { 53 label = Messages.RemoteSyncElement_insync; 54 } else { 55 switch(kind) { 56 case IDiff.CHANGE: label = Messages.RemoteSyncElement_change ; break; 57 case IDiff.ADD: label = Messages.RemoteSyncElement_addition; break; 58 case IDiff.REMOVE: label = Messages.RemoteSyncElement_deletion; break; 59 } 60 } 61 return label; 62 } 63 64 public static String diffDirectionToString(int direction) { 65 switch(direction) { 66 case IThreeWayDiff.CONFLICTING: return Messages.RemoteSyncElement_conflicting; 67 case IThreeWayDiff.OUTGOING: return Messages.RemoteSyncElement_outgoing; 68 case IThreeWayDiff.INCOMING: return Messages.RemoteSyncElement_incoming; 69 } 70 return ""; } 72 73 public static String diffStatusToString(int status) { 74 int kind = status & Diff.KIND_MASK; 75 String label = diffKindToString(kind); 76 int direction = status & ThreeWayDiff.DIRECTION_MASK; 77 if (direction != 0) 78 label = NLS.bind(Messages.concatStrings, new String [] { diffDirectionToString(direction), label }); 79 return label; 80 } 81 82 public static int asDiffFlags(int syncInfoFlags) { 83 if (syncInfoFlags == SyncInfo.IN_SYNC) 84 return IDiff.NO_CHANGE; 85 int kind = SyncInfo.getChange(syncInfoFlags); 86 int diffFlags = 0; 87 switch (kind) { 88 case SyncInfo.ADDITION: 89 diffFlags = IDiff.ADD; 90 break; 91 case SyncInfo.DELETION: 92 diffFlags = IDiff.REMOVE; 93 break; 94 case SyncInfo.CHANGE: 95 diffFlags = IDiff.CHANGE; 96 break; 97 } 98 int direction = SyncInfo.getDirection(syncInfoFlags); 99 switch (direction) { 100 case SyncInfo.INCOMING: 101 diffFlags |= IThreeWayDiff.INCOMING; 102 break; 103 case SyncInfo.OUTGOING: 104 diffFlags |= IThreeWayDiff.OUTGOING; 105 break; 106 case SyncInfo.CONFLICTING: 107 diffFlags |= IThreeWayDiff.CONFLICTING; 108 break; 109 } 110 return diffFlags; 111 } 112 113 private static int asSyncInfoKind(IThreeWayDiff diff) { 114 int kind = diff.getKind(); 115 if (diff.getKind() == IDiff.NO_CHANGE) 116 return SyncInfo.IN_SYNC; 117 int syncKind = 0; 118 switch (kind) { 119 case IDiff.ADD: 120 syncKind = SyncInfo.ADDITION; 121 break; 122 case IDiff.REMOVE: 123 syncKind = SyncInfo.DELETION; 124 break; 125 case IDiff.CHANGE: 126 syncKind = SyncInfo.CHANGE; 127 break; 128 } 129 int direction = diff.getDirection(); 130 switch (direction) { 131 case IThreeWayDiff.INCOMING: 132 syncKind |= SyncInfo.INCOMING; 133 break; 134 case IThreeWayDiff.OUTGOING: 135 syncKind |= SyncInfo.OUTGOING; 136 break; 137 case IThreeWayDiff.CONFLICTING: 138 syncKind |= SyncInfo.CONFLICTING; 139 break; 140 } 141 return syncKind; 142 } 143 144 public IDiff getDeltaFor(SyncInfo info) { 145 if (info.getComparator().isThreeWay()) { 146 ITwoWayDiff local = getLocalDelta(info); 147 ITwoWayDiff remote = getRemoteDelta(info); 148 return new ThreeWayDiff(local, remote); 149 } else { 150 if (info.getKind() != SyncInfo.IN_SYNC) { 151 IResourceVariant remote = info.getRemote(); 152 IResource local = info.getLocal(); 153 int kind; 154 if (remote == null) { 155 kind = IDiff.REMOVE; 156 } else if (!local.exists()) { 157 kind = IDiff.ADD; 158 } else { 159 kind = IDiff.CHANGE; 160 } 161 if (local.getType() == IResource.FILE) { 162 IFileRevision after = asFileState(remote); 163 IFileRevision before = FileRevision.getFileRevisionFor((IFile)local); 164 return new ResourceDiff(info.getLocal(), kind, 0, before, after); 165 } 166 return new ResourceDiff(info.getLocal(), kind); 168 } 169 return null; 170 } 171 } 172 173 private ITwoWayDiff getRemoteDelta(SyncInfo info) { 174 int direction = SyncInfo.getDirection(info.getKind()); 175 if (direction == SyncInfo.INCOMING || direction == SyncInfo.CONFLICTING) { 176 IResourceVariant ancestor = info.getBase(); 177 IResourceVariant remote = info.getRemote(); 178 int kind; 179 if (ancestor == null) { 180 kind = IDiff.ADD; 181 } else if (remote == null) { 182 kind = IDiff.REMOVE; 183 } else { 184 kind = IDiff.CHANGE; 185 } 186 if (info.getLocal().getType() == IResource.FILE) { 188 IFileRevision before = asFileState(ancestor); 189 IFileRevision after = asFileState(remote); 190 return new ResourceDiff(info.getLocal(), kind, 0, before, after); 191 } 192 193 return new ResourceDiff(info.getLocal(), kind); 194 } 195 return null; 196 } 197 198 private IFileRevision asFileState(final IResourceVariant variant) { 199 if (variant == null) 200 return null; 201 return asFileRevision(variant); 202 } 203 204 protected ResourceVariantFileRevision asFileRevision(final IResourceVariant variant) { 205 return new ResourceVariantFileRevision(variant); 206 } 207 208 private ITwoWayDiff getLocalDelta(SyncInfo info) { 209 int direction = SyncInfo.getDirection(info.getKind()); 210 if (direction == SyncInfo.OUTGOING || direction == SyncInfo.CONFLICTING) { 211 IResourceVariant ancestor = info.getBase(); 212 IResource local = info.getLocal(); 213 int kind; 214 if (ancestor == null) { 215 kind = IDiff.ADD; 216 } else if (!local.exists()) { 217 kind = IDiff.REMOVE; 218 } else { 219 kind = IDiff.CHANGE; 220 } 221 if (local.getType() == IResource.FILE) { 222 IFileRevision before = asFileState(ancestor); 223 IFileRevision after = FileRevision.getFileRevisionFor((IFile)local); 224 return new ResourceDiff(info.getLocal(), kind, 0, before, after); 225 } 226 return new ResourceDiff(info.getLocal(), kind); 228 229 } 230 return null; 231 } 232 233 public static IResourceVariant getRemoteVariant(IThreeWayDiff twd) { 234 IFileRevision revision = getRemote(twd); 235 if (revision != null) 236 return asResourceVariant(revision); 237 return null; 238 } 239 240 public static IResourceVariant getBaseVariant(IThreeWayDiff twd) { 241 IResourceDiff diff = (IResourceDiff)twd.getRemoteChange(); 242 if (diff != null) 243 return asResourceVariant(diff.getBeforeState()); 244 diff = (IResourceDiff)twd.getLocalChange(); 245 if (diff != null) 246 return asResourceVariant(diff.getBeforeState()); 247 return null; 248 } 249 250 public SyncInfo asSyncInfo(IDiff diff, IResourceVariantComparator comparator) { 251 if (diff instanceof ResourceDiff) { 252 ResourceDiff rd = (ResourceDiff) diff; 253 IResource local = rd.getResource(); 254 IFileRevision afterState = rd.getAfterState(); 255 IResourceVariant remote = asResourceVariant(afterState); 256 int kind; 257 if (remote == null) { 258 kind = SyncInfo.DELETION; 259 } else if (!local.exists()) { 260 kind = SyncInfo.ADDITION; 261 } else { 262 kind = SyncInfo.CHANGE; 263 } 264 SyncInfo info = createSyncInfo(comparator, kind, local, null, remote); 265 return info; 266 } else if (diff instanceof IThreeWayDiff) { 267 IThreeWayDiff twd = (IThreeWayDiff) diff; 268 IResource local = getLocal(twd); 269 if (local != null) { 270 IResourceVariant remote = getRemoteVariant(twd); 271 IResourceVariant base = getBaseVariant(twd); 272 int kind = asSyncInfoKind(twd); 273 SyncInfo info = createSyncInfo(comparator, kind, local, base, remote); 274 return info; 275 } 276 } 277 return null; 278 } 279 280 protected SyncInfo createSyncInfo(IResourceVariantComparator comparator, int kind, IResource local, IResourceVariant base, IResourceVariant remote) { 281 PrecalculatedSyncInfo info = new PrecalculatedSyncInfo(kind, local, base, remote, comparator); 282 try { 283 info.init(); 284 } catch (TeamException e) { 285 } 287 return info; 288 } 289 290 private static IResource getLocal(IThreeWayDiff twd) { 291 IResourceDiff diff = (IResourceDiff)twd.getRemoteChange(); 292 if (diff != null) 293 return diff.getResource(); 294 diff = (IResourceDiff)twd.getLocalChange(); 295 if (diff != null) 296 return diff.getResource(); 297 return null; 298 } 299 300 public static IResourceVariant asResourceVariant(IFileRevision revision) { 301 if (revision == null) 302 return null; 303 if (revision instanceof ResourceVariantFileRevision) { 304 ResourceVariantFileRevision rvfr = (ResourceVariantFileRevision) revision; 305 return rvfr.getVariant(); 306 } 307 if (revision instanceof IAdaptable) { 308 IAdaptable adaptable = (IAdaptable) revision; 309 Object o = adaptable.getAdapter(IResourceVariant.class); 310 if (o instanceof IResourceVariant) { 311 return (IResourceVariant) o; 312 } 313 } 314 return null; 315 } 316 317 public static IFileRevision getRemote(IDiff diff) { 318 if (diff instanceof IResourceDiff) { 319 IResourceDiff rd = (IResourceDiff) diff; 320 return rd.getAfterState(); 321 } 322 if (diff instanceof IThreeWayDiff) { 323 IThreeWayDiff twd = (IThreeWayDiff) diff; 324 return getRemote(twd); 325 } 326 return null; 327 } 328 329 public static IFileRevision getRemote(IThreeWayDiff twd) { 330 IResourceDiff rd = (IResourceDiff)twd.getRemoteChange(); 331 if (rd != null) 332 return rd.getAfterState(); 333 rd = (IResourceDiff)twd.getLocalChange(); 334 if (rd != null) 335 return rd.getBeforeState(); 336 return null; 337 } 338 339 public static SyncInfoToDiffConverter getDefault() { 340 if (instance == null) 341 instance = new SyncInfoToDiffConverter(); 342 return instance; 343 } 344 } 345 | Popular Tags |