1 11 package org.eclipse.jdt.internal.corext.refactoring.changes; 12 13 import org.eclipse.core.resources.mapping.ResourceMapping; 14 15 import org.eclipse.core.runtime.Assert; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IPath; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.OperationCanceledException; 20 import org.eclipse.core.runtime.SubProgressMonitor; 21 22 import org.eclipse.core.resources.IProject; 23 import org.eclipse.core.resources.IResource; 24 25 import org.eclipse.ltk.core.refactoring.Change; 26 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 27 import org.eclipse.ltk.core.refactoring.participants.ReorgExecutionLog; 28 29 import org.eclipse.jdt.core.IJavaProject; 30 import org.eclipse.jdt.core.IPackageFragmentRoot; 31 import org.eclipse.jdt.core.JavaCore; 32 import org.eclipse.jdt.core.JavaModelException; 33 34 import org.eclipse.jdt.internal.corext.refactoring.base.JDTChange; 35 import org.eclipse.jdt.internal.corext.refactoring.reorg.INewNameQuery; 36 import org.eclipse.jdt.internal.corext.refactoring.reorg.IPackageFragmentRootManipulationQuery; 37 import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil; 38 import org.eclipse.jdt.internal.corext.util.JavaElementResourceMapping; 39 40 abstract class PackageFragmentRootReorgChange extends JDTChange { 41 42 private final String fRootHandle; 43 private final IPath fDestinationPath; 44 private final INewNameQuery fNewNameQuery; 45 private final IPackageFragmentRootManipulationQuery fUpdateClasspathQuery; 46 47 PackageFragmentRootReorgChange(IPackageFragmentRoot root, IProject destination, INewNameQuery newNameQuery, 48 IPackageFragmentRootManipulationQuery updateClasspathQuery) { 49 Assert.isTrue(! root.isExternal()); 50 fRootHandle= root.getHandleIdentifier(); 51 fDestinationPath= Utils.getResourcePath(destination); 52 fNewNameQuery= newNameQuery; 53 fUpdateClasspathQuery= updateClasspathQuery; 54 } 55 56 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException { 57 return isValid(pm, NONE); 62 } 63 64 public final Change perform(IProgressMonitor pm) throws CoreException, OperationCanceledException { 65 pm.beginTask(getName(), 2); 66 try { 67 String newName= getNewResourceName(); 68 IPackageFragmentRoot root= getRoot(); 69 ResourceMapping mapping= JavaElementResourceMapping.create(root); 70 final Change result= doPerformReorg(getDestinationProjectPath().append(newName), new SubProgressMonitor(pm, 1)); 71 markAsExecuted(root, mapping); 72 return result; 73 } finally { 74 pm.done(); 75 } 76 } 77 78 protected abstract Change doPerformReorg(IPath destinationPath, IProgressMonitor pm) throws JavaModelException; 79 80 public Object getModifiedElement() { 81 return getRoot(); 82 } 83 84 protected IPackageFragmentRoot getRoot(){ 85 return (IPackageFragmentRoot)JavaCore.create(fRootHandle); 86 } 87 88 protected IPath getDestinationProjectPath(){ 89 return fDestinationPath; 90 } 91 92 protected IProject getDestinationProject(){ 93 return Utils.getProject(getDestinationProjectPath()); 94 } 95 96 private String getNewResourceName() throws OperationCanceledException { 97 if (fNewNameQuery == null) 98 return getRoot().getElementName(); 99 String name= fNewNameQuery.getNewName(); 100 if (name == null) 101 return getRoot().getElementName(); 102 return name; 103 } 104 105 protected int getUpdateModelFlags(boolean isCopy) throws JavaModelException{ 106 final int destination= IPackageFragmentRoot.DESTINATION_PROJECT_CLASSPATH; 107 final int replace= IPackageFragmentRoot.REPLACE; 108 final int originating; 109 final int otherProjects; 110 if (isCopy){ 111 originating= 0; otherProjects= 0; } else{ 114 originating= IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH; 115 otherProjects= IPackageFragmentRoot.OTHER_REFERRING_PROJECTS_CLASSPATH; 116 } 117 118 if (! JavaCore.create(getDestinationProject()).exists()) 119 return replace | originating; 120 121 if (fUpdateClasspathQuery == null) 122 return replace | originating | destination; 123 124 IJavaProject[] referencingProjects= JavaElementUtil.getReferencingProjects(getRoot()); 125 if (referencingProjects.length <= 1) 126 return replace | originating | destination; 127 128 boolean updateOtherProjectsToo= fUpdateClasspathQuery.confirmManipulation(getRoot(), referencingProjects); 129 if (updateOtherProjectsToo) 130 return replace | originating | destination | otherProjects; 131 else 132 return replace | originating | destination; 133 } 134 135 protected int getResourceUpdateFlags(){ 136 return IResource.KEEP_HISTORY | IResource.SHALLOW; 137 } 138 139 private void markAsExecuted(IPackageFragmentRoot root, ResourceMapping mapping) { 140 ReorgExecutionLog log= (ReorgExecutionLog)getAdapter(ReorgExecutionLog.class); 141 if (log != null) { 142 log.markAsProcessed(root); 143 log.markAsProcessed(mapping); 144 } 145 } 146 } 147 | Popular Tags |