1 11 package org.eclipse.jdt.internal.corext.refactoring.participants; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.IPath; 20 21 import org.eclipse.core.resources.IFile; 22 import org.eclipse.core.resources.IResource; 23 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory; 24 25 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 26 import org.eclipse.ltk.core.refactoring.participants.CopyArguments; 27 import org.eclipse.ltk.core.refactoring.participants.CopyParticipant; 28 import org.eclipse.ltk.core.refactoring.participants.CreateArguments; 29 import org.eclipse.ltk.core.refactoring.participants.CreateParticipant; 30 import org.eclipse.ltk.core.refactoring.participants.DeleteArguments; 31 import org.eclipse.ltk.core.refactoring.participants.DeleteParticipant; 32 import org.eclipse.ltk.core.refactoring.participants.MoveArguments; 33 import org.eclipse.ltk.core.refactoring.participants.MoveParticipant; 34 import org.eclipse.ltk.core.refactoring.participants.ParticipantManager; 35 import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant; 36 import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor; 37 import org.eclipse.ltk.core.refactoring.participants.RenameArguments; 38 import org.eclipse.ltk.core.refactoring.participants.RenameParticipant; 39 import org.eclipse.ltk.core.refactoring.participants.SharableParticipants; 40 41 42 47 public class ResourceModifications { 48 49 private List fCreate; 50 private List fDelete; 51 52 private List fMove; 53 private List fMoveArguments; 54 55 private List fRename; 56 private List fRenameArguments; 57 58 private List fCopy; 59 private List fCopyArguments; 60 61 private int fIgnoreCount; 62 private List fDeltaDescriptions; 63 64 public static abstract class DeltaDescription { 65 protected IResource fResource; 66 public DeltaDescription(IResource resource) { 67 fResource= resource; 68 } 69 public abstract void buildDelta(IResourceChangeDescriptionFactory builder); 70 public abstract IPath getDestinationPath(); 71 72 } 73 public static class DeleteDescription extends DeltaDescription { 74 public DeleteDescription(IResource resource) { 75 super(resource); 76 } 77 public void buildDelta(IResourceChangeDescriptionFactory builder) { 78 builder.delete(fResource); 79 } 80 public IPath getDestinationPath() { 81 return null; 82 } 83 } 84 public static class ChangedDescription extends DeltaDescription { 85 public ChangedDescription(IFile resource) { 86 super(resource); 87 } 88 public void buildDelta(IResourceChangeDescriptionFactory builder) { 89 builder.change((IFile)fResource); 90 } 91 public IPath getDestinationPath() { 92 return null; 93 } 94 } 95 public static class CreateDescription extends DeltaDescription { 96 public CreateDescription(IResource resource) { 97 super(resource); 98 } 99 public void buildDelta(IResourceChangeDescriptionFactory builder) { 100 builder.create(fResource); 101 } 102 public IPath getDestinationPath() { 103 return fResource.getFullPath(); 104 } 105 } 106 public static class MoveDescription extends DeltaDescription { 107 private IPath fDestination; 108 public MoveDescription(IResource resource, IPath destination) { 109 super(resource); 110 fDestination= destination; 111 } 112 public void buildDelta(IResourceChangeDescriptionFactory builder) { 113 builder.move(fResource, fDestination); 114 } 115 public IPath getDestinationPath() { 116 return fDestination; 117 } 118 } 119 public static class CopyDescription extends DeltaDescription { 120 private IPath fDestination; 121 public CopyDescription(IResource resource, IPath destination) { 122 super(resource); 123 fDestination= destination; 124 } 125 public void buildDelta(IResourceChangeDescriptionFactory builder) { 126 builder.copy(fResource, fDestination); 127 } 128 public IPath getDestinationPath() { 129 return fDestination; 130 } 131 } 132 133 138 public void addChanged(IFile file) { 139 if (fIgnoreCount == 0) { 140 internalAdd(new ChangedDescription(file)); 141 } 142 } 143 144 151 public void addCreate(IResource create) { 152 if (fCreate == null) 153 fCreate= new ArrayList (2); 154 fCreate.add(create); 155 if (fIgnoreCount == 0) { 156 internalAdd(new CreateDescription(create)); 157 } 158 } 159 160 166 public void addDelete(IResource delete) { 167 if (fDelete == null) 168 fDelete= new ArrayList (2); 169 fDelete.add(delete); 170 if (fIgnoreCount == 0) { 171 internalAdd(new DeleteDescription(delete)); 172 } 173 } 174 175 181 public void addMove(IResource move, MoveArguments arguments) { 182 if (fMove == null) { 183 fMove= new ArrayList (2); 184 fMoveArguments= new ArrayList (2); 185 } 186 fMove.add(move); 187 fMoveArguments.add(arguments); 188 if (fIgnoreCount == 0) { 189 IPath destination= ((IResource)arguments.getDestination()).getFullPath().append(move.getName()); 190 internalAdd(new MoveDescription(move, destination)); 191 } 192 } 193 194 200 public void addCopy(IResource copy, CopyArguments arguments) { 201 if (fCopy == null) { 202 fCopy= new ArrayList (2); 203 fCopyArguments= new ArrayList (2); 204 } 205 fCopy.add(copy); 206 fCopyArguments.add(arguments); 207 addCopyDelta(copy, arguments); 208 } 209 210 217 public void addRename(IResource rename, RenameArguments arguments) { 218 Assert.isNotNull(rename); 219 Assert.isNotNull(arguments); 220 if (fRename == null) { 221 fRename= new ArrayList (2); 222 fRenameArguments= new ArrayList (2); 223 } 224 fRename.add(rename); 225 fRenameArguments.add(arguments); 226 if (fIgnoreCount == 0) { 227 IPath newPath= rename.getFullPath().removeLastSegments(1).append(arguments.getNewName()); 228 internalAdd(new MoveDescription(rename, newPath)); 229 } 230 } 231 232 public RefactoringParticipant[] getParticipants(RefactoringStatus status, RefactoringProcessor processor, String [] natures, SharableParticipants shared) { 233 List result= new ArrayList (5); 234 if (fDelete != null) { 235 DeleteArguments arguments= new DeleteArguments(); 236 for (Iterator iter= fDelete.iterator(); iter.hasNext();) { 237 DeleteParticipant[] deletes= ParticipantManager.loadDeleteParticipants(status, 238 processor, iter.next(), 239 arguments, natures, shared); 240 result.addAll(Arrays.asList(deletes)); 241 } 242 } 243 if (fCreate != null) { 244 CreateArguments arguments= new CreateArguments(); 245 for (Iterator iter= fCreate.iterator(); iter.hasNext();) { 246 CreateParticipant[] creates= ParticipantManager.loadCreateParticipants(status, 247 processor, iter.next(), 248 arguments, natures, shared); 249 result.addAll(Arrays.asList(creates)); 250 } 251 } 252 if (fMove != null) { 253 for (int i= 0; i < fMove.size(); i++) { 254 Object element= fMove.get(i); 255 MoveArguments arguments= (MoveArguments)fMoveArguments.get(i); 256 MoveParticipant[] moves= ParticipantManager.loadMoveParticipants(status, 257 processor, element, 258 arguments, natures, shared); 259 result.addAll(Arrays.asList(moves)); 260 261 } 262 } 263 if (fCopy != null) { 264 for (int i= 0; i < fCopy.size(); i++) { 265 Object element= fCopy.get(i); 266 CopyArguments arguments= (CopyArguments)fCopyArguments.get(i); 267 CopyParticipant[] copies= ParticipantManager.loadCopyParticipants(status, 268 processor, element, 269 arguments, natures, shared); 270 result.addAll(Arrays.asList(copies)); 271 } 272 } 273 if (fRename != null) { 274 for (int i= 0; i < fRename.size(); i++) { 275 Object resource= fRename.get(i); 276 RenameArguments arguments= (RenameArguments) fRenameArguments.get(i); 277 RenameParticipant[] renames= ParticipantManager.loadRenameParticipants(status, 278 processor, resource, 279 arguments, natures, shared); 280 result.addAll(Arrays.asList(renames)); 281 } 282 } 283 return (RefactoringParticipant[])result.toArray(new RefactoringParticipant[result.size()]); 284 } 285 286 public void ignoreForDelta() { 287 fIgnoreCount++; 288 } 289 290 public void trackForDelta() { 291 fIgnoreCount--; 292 } 293 294 public void addDelta(DeltaDescription description) { 295 if (fIgnoreCount > 0) 296 return; 297 internalAdd(description); 298 } 299 300 public void addCopyDelta(IResource copy, CopyArguments arguments) { 301 if (fIgnoreCount == 0) { 302 IPath destination= ((IResource)arguments.getDestination()).getFullPath().append(copy.getName()); 303 internalAdd(new CopyDescription(copy, destination)); 304 } 305 } 306 307 314 public boolean willExist(IResource resource) { 315 if (fDeltaDescriptions == null) 316 return false; 317 IPath fullPath= resource.getFullPath(); 318 for (Iterator iter= fDeltaDescriptions.iterator(); iter.hasNext();) { 319 DeltaDescription delta= (DeltaDescription) iter.next(); 320 if (fullPath.equals(delta.getDestinationPath())) 321 return true; 322 } 323 return false; 324 } 325 326 public void buildDelta(IResourceChangeDescriptionFactory builder) { 327 if (fDeltaDescriptions == null) 328 return; 329 for (Iterator iter= fDeltaDescriptions.iterator(); iter.hasNext();) { 330 ((DeltaDescription) iter.next()).buildDelta(builder); 331 } 332 } 333 334 public static void buildMoveDelta(IResourceChangeDescriptionFactory builder, IResource resource, RenameArguments args) { 335 IPath newPath= resource.getFullPath().removeLastSegments(1).append(args.getNewName()); 336 builder.move(resource, newPath); 337 } 338 339 public static void buildMoveDelta(IResourceChangeDescriptionFactory builder, IResource resource, MoveArguments args) { 340 IPath destination= ((IResource)args.getDestination()).getFullPath().append(resource.getName()); 341 builder.move(resource, destination); 342 } 343 344 public static void buildCopyDelta(IResourceChangeDescriptionFactory builder, IResource resource, CopyArguments args) { 345 IPath destination= ((IResource)args.getDestination()).getFullPath().append(resource.getName()); 346 builder.copy(resource, destination); 347 } 348 349 private void internalAdd(DeltaDescription description) { 350 if (fDeltaDescriptions == null) 351 fDeltaDescriptions= new ArrayList (); 352 fDeltaDescriptions.add(description); 353 } 354 } 355 | Popular Tags |