1 11 package org.eclipse.jdt.internal.corext.refactoring.changes; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.InputStreamReader ; 16 import java.io.UnsupportedEncodingException ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.Map.Entry; 20 21 import org.eclipse.text.edits.ReplaceEdit; 22 23 import org.eclipse.core.runtime.Assert; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IPath; 26 import org.eclipse.core.runtime.IProgressMonitor; 27 import org.eclipse.core.runtime.IStatus; 28 import org.eclipse.core.runtime.NullProgressMonitor; 29 import org.eclipse.core.runtime.Status; 30 import org.eclipse.core.runtime.SubProgressMonitor; 31 32 import org.eclipse.core.filebuffers.FileBuffers; 33 import org.eclipse.core.filebuffers.ITextFileBufferManager; 34 import org.eclipse.core.filebuffers.LocationKind; 35 36 import org.eclipse.core.resources.IFile; 37 import org.eclipse.core.resources.IResource; 38 39 import org.eclipse.ui.ide.undo.ResourceDescription; 40 41 import org.eclipse.ltk.core.refactoring.Change; 42 import org.eclipse.ltk.core.refactoring.CompositeChange; 43 import org.eclipse.ltk.core.refactoring.NullChange; 44 import org.eclipse.ltk.core.refactoring.RefactoringStatus; 45 import org.eclipse.ltk.core.refactoring.TextFileChange; 46 47 import org.eclipse.jdt.core.IJavaProject; 48 import org.eclipse.jdt.core.IPackageFragmentRoot; 49 import org.eclipse.jdt.core.JavaCore; 50 import org.eclipse.jdt.core.JavaModelException; 51 52 import org.eclipse.jdt.internal.corext.Corext; 53 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages; 54 import org.eclipse.jdt.internal.corext.refactoring.reorg.IPackageFragmentRootManipulationQuery; 55 import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil; 56 import org.eclipse.jdt.internal.corext.util.Messages; 57 58 import org.eclipse.jdt.internal.ui.JavaPlugin; 59 60 public class DeletePackageFragmentRootChange extends AbstractDeleteChange { 61 62 private final String fHandle; 63 private final boolean fIsExecuteChange; 64 private final IPackageFragmentRootManipulationQuery fUpdateClasspathQuery; 65 66 public DeletePackageFragmentRootChange(IPackageFragmentRoot root, boolean isExecuteChange, 67 IPackageFragmentRootManipulationQuery updateClasspathQuery) { 68 Assert.isNotNull(root); 69 Assert.isTrue(! root.isExternal()); 70 fHandle= root.getHandleIdentifier(); 71 fIsExecuteChange= isExecuteChange; 72 fUpdateClasspathQuery= updateClasspathQuery; 73 } 74 75 public String getName() { 76 String [] keys= {getRoot().getElementName()}; 77 return Messages.format(RefactoringCoreMessages.DeletePackageFragmentRootChange_delete, keys); 78 } 79 80 public Object getModifiedElement() { 81 return getRoot(); 82 } 83 84 private IPackageFragmentRoot getRoot(){ 85 return (IPackageFragmentRoot)JavaCore.create(fHandle); 86 } 87 88 public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException { 89 if (fIsExecuteChange) { 90 return super.isValid(pm, DIRTY); 95 } else { 96 return super.isValid(pm, READ_ONLY | DIRTY); 97 } 98 } 99 100 protected Change doDelete(IProgressMonitor pm) throws CoreException { 101 if (! confirmDeleteIfReferenced()) 102 return new NullChange(); 103 int resourceUpdateFlags= IResource.KEEP_HISTORY; 104 int jCoreUpdateFlags= IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH | IPackageFragmentRoot.OTHER_REFERRING_PROJECTS_CLASSPATH; 105 106 pm.beginTask("", 2); IPackageFragmentRoot root= getRoot(); 108 IResource rootResource= root.getResource(); 109 CompositeChange result= new CompositeChange(getName()); 110 111 ResourceDescription rootDescription = ResourceDescription.fromResource(rootResource); 112 IJavaProject[] referencingProjects= JavaElementUtil.getReferencingProjects(root); 113 HashMap classpathFilesContents= new HashMap (); 114 for (int i= 0; i < referencingProjects.length; i++) { 115 IJavaProject javaProject= referencingProjects[i]; 116 IFile classpathFile= javaProject.getProject().getFile(".classpath"); if (classpathFile.exists()) { 118 classpathFilesContents.put(classpathFile, getFileContents(classpathFile)); 119 } 120 } 121 122 root.delete(resourceUpdateFlags, jCoreUpdateFlags, new SubProgressMonitor(pm, 1)); 123 124 rootDescription.recordStateFromHistory(rootResource, new SubProgressMonitor(pm, 1)); 125 for (Iterator iterator= classpathFilesContents.entrySet().iterator(); iterator.hasNext();) { 126 Entry entry= (Entry) iterator.next(); 127 IFile file= (IFile) entry.getKey(); 128 String contents= (String ) entry.getValue(); 129 TextFileChange classpathUndo= new TextFileChange(Messages.format(RefactoringCoreMessages.DeletePackageFragmentRootChange_restore_file, file.getFullPath().toOSString()), file); 131 classpathUndo.setEdit(new ReplaceEdit(0, getFileLength(file), contents)); 132 result.add(classpathUndo); 133 } 134 result.add(new UndoDeleteResourceChange(rootDescription)); 135 136 pm.done(); 137 return result; 138 } 139 140 private static String getFileContents(IFile file) throws CoreException { 141 ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager(); 142 IPath path= file.getFullPath(); 143 manager.connect(path, LocationKind.IFILE, new NullProgressMonitor()); 144 try { 145 return manager.getTextFileBuffer(path, LocationKind.IFILE).getDocument().get(); 146 } finally { 147 manager.disconnect(path, LocationKind.IFILE, new NullProgressMonitor()); 148 } 149 } 150 151 private static int getFileLength(IFile file) throws CoreException { 152 InputStream contents= file.getContents(); 154 InputStreamReader reader; 155 try { 156 reader= new InputStreamReader (contents, file.getCharset()); 157 } catch (UnsupportedEncodingException e) { 158 JavaPlugin.log(e); 159 reader= new InputStreamReader (contents); 160 } 161 try { 162 return (int) reader.skip(Integer.MAX_VALUE); 163 } catch (IOException e) { 164 throw new CoreException(new Status(IStatus.ERROR, Corext.getPluginId(), e.getMessage(), e)); 165 } 166 } 167 168 private boolean confirmDeleteIfReferenced() throws JavaModelException { 169 if (! getRoot().isArchive()) return true; 171 if (fUpdateClasspathQuery == null) 172 return true; 173 IJavaProject[] referencingProjects= JavaElementUtil.getReferencingProjects(getRoot()); 174 if (referencingProjects.length <= 1) 175 return true; 176 return fUpdateClasspathQuery.confirmManipulation(getRoot(), referencingProjects); 177 } 178 } 179 | Popular Tags |