1 12 package org.eclipse.team.internal.core.subscribers; 13 14 import java.util.*; 15 16 import org.eclipse.core.resources.*; 17 import org.eclipse.core.resources.mapping.ResourceTraversal; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.osgi.util.NLS; 20 import org.eclipse.team.core.diff.*; 21 import org.eclipse.team.core.mapping.IChangeGroupingRequestor; 22 import org.eclipse.team.core.mapping.IResourceDiffTree; 23 import org.eclipse.team.internal.core.*; 24 import org.eclipse.team.internal.core.mapping.CompoundResourceTraversal; 25 import org.osgi.service.prefs.BackingStoreException; 26 import org.osgi.service.prefs.Preferences; 27 28 32 public abstract class ActiveChangeSetManager extends ChangeSetManager implements IDiffChangeListener, IChangeGroupingRequestor { 33 34 private static final String CTX_DEFAULT_SET = "defaultSet"; 36 private ActiveChangeSet defaultSet; 37 38 44 protected ChangeSet getChangeSet(IResourceDiffTree tree) { 45 ChangeSet[] sets = getSets(); 46 for (int i = 0; i < sets.length; i++) { 47 ChangeSet changeSet = sets[i]; 48 if (((DiffChangeSet)changeSet).getDiffTree() == tree) { 49 return changeSet; 50 } 51 } 52 return null; 53 } 54 55 58 public void add(ChangeSet set) { 59 Assert.isTrue(set instanceof ActiveChangeSet); 60 super.add(set); 61 } 62 63 66 protected void handleSetAdded(ChangeSet set) { 67 Assert.isTrue(set instanceof ActiveChangeSet); 68 ((DiffChangeSet)set).getDiffTree().addDiffChangeListener(getDiffTreeListener()); 69 super.handleSetAdded(set); 70 handleAddedResources(set, ((ActiveChangeSet)set).internalGetDiffTree().getDiffs()); 71 } 72 73 76 protected void handleSetRemoved(ChangeSet set) { 77 ((DiffChangeSet)set).getDiffTree().removeDiffChangeListener(getDiffTreeListener()); 78 super.handleSetRemoved(set); 79 } 80 81 87 protected IDiffChangeListener getDiffTreeListener() { 88 return this; 89 } 90 91 94 public void diffsChanged(IDiffChangeEvent event, IProgressMonitor monitor) { 95 IResourceDiffTree tree = (IResourceDiffTree)event.getTree(); 96 handleSyncSetChange(tree, event.getAdditions(), getAllResources(event)); 97 } 98 99 102 public void propertyChanged(IDiffTree tree, int property, IPath[] paths) { 103 } 105 106 public boolean isModified(IFile file) throws CoreException { 107 IDiff diff = getDiff(file); 108 if (diff != null) 109 return isModified(diff); 110 return false; 111 } 112 113 118 public boolean isModified(IDiff diff) { 119 if (diff != null) { 120 if (diff instanceof IThreeWayDiff) { 121 IThreeWayDiff twd = (IThreeWayDiff) diff; 122 int dir = twd.getDirection(); 123 return dir == IThreeWayDiff.OUTGOING || dir == IThreeWayDiff.CONFLICTING; 124 } else { 125 return diff.getKind() != IDiff.NO_CHANGE; 126 } 127 } 128 return false; 129 } 130 131 136 public ActiveChangeSet getSet(String name) { 137 ChangeSet[] sets = getSets(); 138 for (int i = 0; i < sets.length; i++) { 139 ChangeSet set = sets[i]; 140 if (set.getName().equals(name) && set instanceof ActiveChangeSet) { 141 return (ActiveChangeSet)set; 142 } 143 } 144 return null; 145 } 146 147 155 public ActiveChangeSet createSet(String title, IFile[] files) throws CoreException { 156 List infos = new ArrayList(); 157 for (int i = 0; i < files.length; i++) { 158 IFile file = files[i]; 159 IDiff diff = getDiff(file); 160 if (diff != null) { 161 infos.add(diff); 162 } 163 } 164 return createSet(title, (IDiff[]) infos.toArray(new IDiff[infos.size()])); 165 } 166 167 176 public ActiveChangeSet createSet(String title, IDiff[] diffs) { 177 ActiveChangeSet commitSet = doCreateSet(title); 178 if (diffs != null && diffs.length > 0) { 179 commitSet.add(diffs); 180 } 181 return commitSet; 182 } 183 184 189 protected ActiveChangeSet doCreateSet(String name) { 190 return new ActiveChangeSet(this, name); 191 } 192 193 public abstract IDiff getDiff(IResource resource) throws CoreException; 194 195 202 protected boolean isSingleSetPerResource() { 203 return true; 204 } 205 206 private IPath[] getAllResources(IDiffChangeEvent event) { 207 Set allResources = new HashSet(); 208 IDiff[] addedResources = event.getAdditions(); 209 for (int i = 0; i < addedResources.length; i++) { 210 IDiff diff = addedResources[i]; 211 allResources.add(diff.getPath()); 212 } 213 IDiff[] changedResources = event.getChanges(); 214 for (int i = 0; i < changedResources.length; i++) { 215 IDiff diff = changedResources[i]; 216 allResources.add(diff.getPath()); 217 } 218 IPath[] removals = event.getRemovals(); 219 for (int i = 0; i < removals.length; i++) { 220 IPath path = removals[i]; 221 allResources.add(path); 222 } 223 return (IPath[]) allResources.toArray(new IPath[allResources.size()]); 224 } 225 226 231 protected void handleAddedResources(ChangeSet set, IDiff[] diffs) { 232 if (isSingleSetPerResource() && ((ActiveChangeSet)set).isUserCreated()) { 233 IResource[] resources = new IResource[diffs.length]; 234 for (int i = 0; i < resources.length; i++) { 235 resources[i] = ((DiffChangeSet)set).getDiffTree().getResource(diffs[i]); 236 } 237 ChangeSet[] sets = getSets(); 239 for (int i = 0; i < sets.length; i++) { 240 ChangeSet otherSet = sets[i]; 241 if (otherSet != set && ((ActiveChangeSet)otherSet).isUserCreated()) { 242 otherSet.remove(resources); 243 } 244 } 245 } 246 } 247 248 private void handleSyncSetChange(IResourceDiffTree tree, IDiff[] addedDiffs, IPath[] allAffectedResources) { 249 ChangeSet changeSet = getChangeSet(tree); 250 if (tree.isEmpty() && changeSet != null) { 251 remove(changeSet); 252 } 253 fireResourcesChangedEvent(changeSet, allAffectedResources); 254 handleAddedResources(changeSet, addedDiffs); 255 } 256 257 262 public void makeDefault(ActiveChangeSet set) { 263 if (!contains(set)) { 265 add(set); 266 } 267 ActiveChangeSet oldSet = defaultSet; 268 defaultSet = set; 269 fireDefaultChangedEvent(oldSet, defaultSet); 270 } 271 272 278 public boolean isDefault(ActiveChangeSet set) { 279 return set == defaultSet; 280 } 281 282 287 public ActiveChangeSet getDefaultSet() { 288 return defaultSet; 289 } 290 291 297 public ResourceTraversal[] adjustInputTraversals(ResourceTraversal[] traversals) { 298 CompoundResourceTraversal traversal = new CompoundResourceTraversal(); 299 traversal.addTraversals(traversals); 300 ChangeSet[] sets = getSets(); 301 for (int i = 0; i < sets.length; i++) { 302 ChangeSet set = sets[i]; 303 handleIntersect(traversal, set); 304 } 305 return traversal.asTraversals(); 306 } 307 308 private void handleIntersect(CompoundResourceTraversal traversal, ChangeSet set) { 309 IResource[] resources = set.getResources(); 310 for (int i = 0; i < resources.length; i++) { 311 IResource resource = resources[i]; 312 if (traversal.isCovered(resource, IResource.DEPTH_ZERO)) { 313 traversal.addResources(resources, IResource.DEPTH_ZERO); 314 return; 315 } 316 } 317 } 318 319 324 protected void save(Preferences prefs) { 325 if (!isInitialized()) 327 return; 328 try { 330 String [] oldSetNames = prefs.childrenNames(); 331 for (int i = 0; i < oldSetNames.length; i++) { 332 String string = oldSetNames[i]; 333 prefs.node(string).removeNode(); 334 } 335 } catch (BackingStoreException e) { 336 TeamPlugin.log(IStatus.ERROR, NLS.bind("An error occurred purging the commit set state for {0}", new String [] { getName() }), e); } 338 ChangeSet[] sets = getSets(); 339 for (int i = 0; i < sets.length; i++) { 340 ChangeSet set = sets[i]; 341 if (set instanceof ActiveChangeSet && !set.isEmpty()) { 342 String childPrefName = escapePrefName(((ActiveChangeSet)set).getTitle()); 346 Preferences child = prefs.node(childPrefName); 347 ((ActiveChangeSet)set).save(child); 348 } 349 } 350 if (getDefaultSet() != null) { 351 prefs.put(CTX_DEFAULT_SET, getDefaultSet().getTitle()); 352 } 353 try { 354 prefs.flush(); 355 } catch (BackingStoreException e) { 356 TeamPlugin.log(IStatus.ERROR, NLS.bind(Messages.SubscriberChangeSetCollector_3, new String [] { getName() }), e); 357 } 358 } 359 360 369 private static String escapePrefName(String string) { 370 StringBuffer out = new StringBuffer (); 371 for (int i = 0; i < string.length(); i++) { 372 char c = string.charAt(i); 373 switch (c) { 374 case '/': 375 out.append("\\s"); break; 377 case '\\': 378 out.append("\\\\"); break; 380 default: 381 out.append(c); 382 } 383 } 384 return out.toString(); 385 } 386 387 393 protected void load(Preferences prefs) { 394 String defaultSetTitle = prefs.get(CTX_DEFAULT_SET, null); 395 try { 396 String [] childNames = prefs.childrenNames(); 397 for (int i = 0; i < childNames.length; i++) { 398 String string = childNames[i]; 399 Preferences childPrefs = prefs.node(string); 400 ActiveChangeSet set = createSet(childPrefs); 401 if (!set.isEmpty()) { 402 if (getDefaultSet() == null && defaultSetTitle != null && set.getTitle().equals(defaultSetTitle)) { 403 makeDefault(set); 404 } 405 add(set); 406 } 407 } 408 } catch (BackingStoreException e) { 409 TeamPlugin.log(IStatus.ERROR, NLS.bind(Messages.SubscriberChangeSetCollector_4, new String [] { getName() }), e); 410 } 411 } 412 413 417 protected abstract String getName(); 418 419 425 protected ActiveChangeSet createSet(Preferences childPrefs) { 426 ActiveChangeSet changeSet = doCreateSet(null); 429 changeSet.init(childPrefs); 430 return changeSet; 431 } 432 433 436 public void ensureChangesGrouped(IProject project, IFile[] files, 437 String name) throws CoreException { 438 ActiveChangeSet set = getSet(name); 439 if (set == null) { 440 set = createSet(name, files); 441 set.setUserCreated(false); 442 add(set); 443 } else { 444 set.setUserCreated(false); 445 set.add(files); 446 } 447 } 448 } 449 | Popular Tags |