1 11 package org.eclipse.jdt.internal.corext.refactoring.reorg; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Set ; 19 20 import org.eclipse.core.runtime.Assert; 21 import org.eclipse.core.runtime.CoreException; 22 23 import org.eclipse.core.resources.IContainer; 24 import org.eclipse.core.resources.IFile; 25 import org.eclipse.core.resources.IResource; 26 import org.eclipse.core.resources.mapping.IResourceChangeDescriptionFactory; 27 28 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 29 import org.eclipse.ltk.core.refactoring.participants.IParticipantDescriptorFilter; 30 import org.eclipse.ltk.core.refactoring.participants.MoveArguments; 31 import org.eclipse.ltk.core.refactoring.participants.ParticipantManager; 32 import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments; 33 import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant; 34 import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor; 35 import org.eclipse.ltk.core.refactoring.participants.SharableParticipants; 36 import org.eclipse.ltk.core.refactoring.participants.ValidateEditChecker; 37 38 import org.eclipse.jdt.core.ICompilationUnit; 39 import org.eclipse.jdt.core.IJavaElement; 40 import org.eclipse.jdt.core.IPackageFragment; 41 import org.eclipse.jdt.core.IPackageFragmentRoot; 42 import org.eclipse.jdt.core.IType; 43 44 import org.eclipse.jdt.internal.corext.refactoring.participants.ResourceModifications; 45 46 public class MoveModifications extends RefactoringModifications { 47 48 private List fMoves; 49 private List fMoveArguments; 50 private List fParticipantDescriptorFilter; 51 52 public MoveModifications() { 53 fMoves= new ArrayList (); 54 fMoveArguments= new ArrayList (); 55 fParticipantDescriptorFilter= new ArrayList (); 56 } 57 58 public void move(IResource resource, MoveArguments args) { 59 add(resource, args, null); 60 } 61 62 public void move(IPackageFragmentRoot sourceFolder, MoveArguments arguments) { 63 add(sourceFolder, arguments, null); 64 IResource sourceResource= sourceFolder.getResource(); 65 if (sourceResource != null) { 66 getResourceModifications().addMove(sourceResource, 67 new MoveArguments(getResourceDestination(arguments), arguments.getUpdateReferences())); 68 IFile classpath= getClasspathFile(sourceResource); 69 if (classpath != null) { 70 getResourceModifications().addChanged(classpath); 71 } 72 classpath= getClasspathFile(getResourceDestination(arguments)); 73 if (classpath != null) { 74 getResourceModifications().addChanged(classpath); 75 } 76 } 77 } 78 79 public void move(IPackageFragment pack, MoveArguments args) throws CoreException { 80 add(pack, args, null); 81 if (pack.getResource() == null) 82 return; 83 IPackageFragmentRoot javaDestination= (IPackageFragmentRoot) args.getDestination(); 84 if (javaDestination.getResource() == null) 85 return; 86 IPackageFragment newPack= javaDestination.getPackageFragment(pack.getElementName()); 87 if (!pack.hasSubpackages() && !newPack.exists()) { 88 IContainer resourceDestination= newPack.getResource().getParent(); 90 createIncludingParents(resourceDestination); 91 getResourceModifications().addMove( 92 pack.getResource(), 93 new MoveArguments(resourceDestination, args.getUpdateReferences())); 94 } else { 95 IContainer resourceSource= (IContainer)pack.getResource(); 96 IContainer resourceDestination= (IContainer) newPack.getResource(); 97 createIncludingParents(resourceDestination); 98 MoveArguments arguments= new MoveArguments(resourceDestination, args.getUpdateReferences()); 99 IResource[] resourcesToMove= collectResourcesOfInterest(pack); 100 Set allMembers= new HashSet (Arrays.asList(resourceSource.members())); 101 for (int i= 0; i < resourcesToMove.length; i++) { 102 IResource toMove= resourcesToMove[i]; 103 getResourceModifications().addMove(toMove, arguments); 104 allMembers.remove(toMove); 105 } 106 for (Iterator iter= allMembers.iterator(); iter.hasNext();) { 107 IResource element= (IResource) iter.next(); 108 if (element instanceof IFile) { 109 getResourceModifications().addDelete(element); 110 iter.remove(); 111 } 112 } 113 if (allMembers.isEmpty()) { 114 getResourceModifications().addDelete(resourceSource); 115 } 116 } 117 } 118 119 public void move(ICompilationUnit unit, MoveArguments args) throws CoreException { 120 add(unit, args, null); 121 IType[] types= unit.getTypes(); 122 for (int tt= 0; tt < types.length; tt++) { 123 add(types[tt], args, null); 124 } 125 IResource resourceDestination= getResourceDestination(args); 126 if (resourceDestination != null && unit.getResource() != null) { 127 getResourceModifications().addMove(unit.getResource(), new MoveArguments(resourceDestination, args.getUpdateReferences())); 128 } 129 } 130 131 public void buildDelta(IResourceChangeDescriptionFactory builder) { 132 for (int i= 0; i < fMoves.size(); i++) { 133 Object element= fMoves.get(i); 134 if (element instanceof IResource) { 135 ResourceModifications.buildMoveDelta(builder, (IResource) element, (MoveArguments) fMoveArguments.get(i)); 136 } 137 } 138 getResourceModifications().buildDelta(builder); 139 } 140 141 public void buildValidateEdits(ValidateEditChecker checker) { 142 for (Iterator iter= fMoves.iterator(); iter.hasNext();) { 143 Object element= iter.next(); 144 if (element instanceof ICompilationUnit) { 145 ICompilationUnit unit= (ICompilationUnit)element; 146 IResource resource= unit.getResource(); 147 if (resource != null && resource.getType() == IResource.FILE) { 148 checker.addFile((IFile)resource); 149 } 150 } 151 } 152 } 153 154 public RefactoringParticipant[] loadParticipants(RefactoringStatus status, RefactoringProcessor owner, String [] natures, SharableParticipants shared) { 155 List result= new ArrayList (); 156 for (int i= 0; i < fMoves.size(); i++) { 157 result.addAll(Arrays.asList(ParticipantManager.loadMoveParticipants(status, 158 owner, fMoves.get(i), 159 (MoveArguments) fMoveArguments.get(i), 160 (IParticipantDescriptorFilter) fParticipantDescriptorFilter.get(i), 161 natures, shared))); 162 } 163 result.addAll(Arrays.asList(getResourceModifications().getParticipants(status, owner, natures, shared))); 164 return (RefactoringParticipant[]) result.toArray(new RefactoringParticipant[result.size()]); 165 } 166 167 private void add(Object element, RefactoringArguments args, IParticipantDescriptorFilter filter) { 168 Assert.isNotNull(element); 169 Assert.isNotNull(args); 170 fMoves.add(element); 171 fMoveArguments.add(args); 172 fParticipantDescriptorFilter.add(filter); 173 } 174 175 private IResource getResourceDestination(MoveArguments args) { 176 Object genericDestination= args.getDestination(); 177 IResource resourceDestination= null; 178 if (genericDestination instanceof IJavaElement) { 179 resourceDestination= ((IJavaElement)genericDestination).getResource(); 180 } else if (genericDestination instanceof IResource) { 181 resourceDestination= (IResource)genericDestination; 182 } 183 return resourceDestination; 184 } 185 } 186 | Popular Tags |