1 11 package org.eclipse.ltk.internal.ui.refactoring.model; 12 13 import java.util.HashSet ; 14 import java.util.Iterator ; 15 import java.util.Set ; 16 17 import org.eclipse.team.core.diff.IDiff; 18 import org.eclipse.team.core.mapping.IMergeContext; 19 20 import org.eclipse.core.runtime.Assert; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IProgressMonitor; 23 import org.eclipse.core.runtime.NullProgressMonitor; 24 25 import org.eclipse.core.resources.IResource; 26 import org.eclipse.core.resources.IResourceChangeEvent; 27 import org.eclipse.core.resources.IResourceChangeListener; 28 import org.eclipse.core.resources.IResourceDelta; 29 import org.eclipse.core.resources.IResourceDeltaVisitor; 30 import org.eclipse.core.resources.ResourcesPlugin; 31 32 import org.eclipse.ltk.core.refactoring.Refactoring; 33 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor; 34 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 35 36 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryService; 37 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIPlugin; 38 39 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryWizard; 40 41 56 public abstract class RefactoringHistoryMergeWizard extends RefactoringHistoryWizard { 57 58 59 private class WorkspaceChangeListener implements IResourceChangeListener { 60 61 64 public void resourceChanged(final IResourceChangeEvent event) { 65 final IResourceDelta delta= event.getDelta(); 66 if (delta != null) { 67 try { 68 delta.accept(new IResourceDeltaVisitor() { 69 70 public final boolean visit(final IResourceDelta current) throws CoreException { 71 final IResource resource= current.getResource(); 72 if (!resource.isDerived()) { 73 if (resource.getType() == IResource.FILE) { 74 switch (delta.getKind()) { 75 case IResourceDelta.ADDED: 76 fAddedFiles.add(resource); 77 break; 78 case IResourceDelta.REMOVED: 79 fRemovedFiles.add(resource); 80 break; 81 case IResourceDelta.CHANGED: 82 fChangedFiles.add(resource); 83 break; 84 } 85 } 86 } 87 return true; 88 } 89 }); 90 } catch (CoreException exception) { 91 RefactoringUIPlugin.log(exception); 92 } 93 } 94 } 95 } 96 97 98 private final Set fAddedFiles= new HashSet (); 99 100 101 private final Set fChangedFiles= new HashSet (); 102 103 104 private final IResourceChangeListener fListener= new WorkspaceChangeListener(); 105 106 107 private final Set fRemovedFiles= new HashSet (); 108 109 119 protected RefactoringHistoryMergeWizard(final String caption, final String title, final String description) { 120 super(caption, title, description); 121 } 122 123 126 protected RefactoringStatus aboutToPerformHistory(final IProgressMonitor monitor) { 127 fAddedFiles.clear(); 128 fRemovedFiles.clear(); 129 fChangedFiles.clear(); 130 ResourcesPlugin.getWorkspace().addResourceChangeListener(fListener, IResourceChangeEvent.POST_CHANGE); 131 return super.aboutToPerformHistory(monitor); 132 } 133 134 137 protected RefactoringStatus aboutToPerformRefactoring(final Refactoring refactoring, final RefactoringDescriptor descriptor, final IProgressMonitor monitor) { 138 Assert.isNotNull(descriptor); 139 final long stamp= descriptor.getTimeStamp(); 140 if (stamp >= 0) 141 RefactoringHistoryService.getInstance().setOverrideTimeStamp(stamp); 142 return super.aboutToPerformRefactoring(refactoring, descriptor, monitor); 143 } 144 145 150 public Set getAddedFiles() { 151 return fAddedFiles; 152 } 153 154 159 public Set getChangedFiles() { 160 return fChangedFiles; 161 } 162 163 168 public Set getRemovedFiles() { 169 return fRemovedFiles; 170 } 171 172 175 protected RefactoringStatus historyPerformed(final IProgressMonitor monitor) { 176 ResourcesPlugin.getWorkspace().removeResourceChangeListener(fListener); 177 RefactoringHistoryService.getInstance().setOverrideTimeStamp(-1); 178 return super.historyPerformed(monitor); 179 } 180 181 184 protected RefactoringStatus refactoringPerformed(final Refactoring refactoring, final IProgressMonitor monitor) { 185 RefactoringHistoryService.getInstance().setOverrideTimeStamp(-1); 186 return super.refactoringPerformed(refactoring, monitor); 187 } 188 189 196 public void resolveConflicts(final IMergeContext context) { 197 Assert.isNotNull(context); 198 for (final Iterator iterator= fChangedFiles.iterator(); iterator.hasNext();) { 199 final IResource resource= (IResource) iterator.next(); 200 final IDiff diff= context.getDiffTree().getDiff(resource); 201 if (diff != null) { 202 try { 203 context.markAsMerged(diff, true, new NullProgressMonitor()); 204 } catch (CoreException exception) { 205 RefactoringUIPlugin.log(exception); 206 } 207 } 208 } 209 for (final Iterator iterator= fAddedFiles.iterator(); iterator.hasNext();) { 210 final IResource resource= (IResource) iterator.next(); 211 final IDiff diff= context.getDiffTree().getDiff(resource); 212 if (diff != null) { 213 try { 214 context.markAsMerged(diff, true, new NullProgressMonitor()); 215 } catch (CoreException exception) { 216 RefactoringUIPlugin.log(exception); 217 } 218 } 219 } 220 for (final Iterator iterator= fRemovedFiles.iterator(); iterator.hasNext();) { 221 final IResource resource= (IResource) iterator.next(); 222 final IDiff diff= context.getDiffTree().getDiff(resource); 223 if (diff != null) { 224 try { 225 context.markAsMerged(diff, true, new NullProgressMonitor()); 226 } catch (CoreException exception) { 227 RefactoringUIPlugin.log(exception); 228 } 229 } 230 } 231 } 232 } | Popular Tags |