1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.refactoring.changes;
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;
21 import org.eclipse.core.runtime.Assert;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.NullProgressMonitor;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.core.runtime.SubProgressMonitor;
30 import org.eclipse.core.resources.IFile;
31 import org.eclipse.core.resources.IResource;
33 import org.eclipse.core.filebuffers.FileBuffers;
34 import org.eclipse.core.filebuffers.ITextFileBufferManager;
35 import org.eclipse.core.filebuffers.LocationKind;
37 import org.eclipse.text.edits.ReplaceEdit;
39 import org.eclipse.ui.ide.undo.ResourceDescription;
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.TextFileChange;
46 import org.eclipse.jdt.core.IJavaProject;
47 import org.eclipse.jdt.core.IPackageFragmentRoot;
48 import org.eclipse.jdt.core.JavaCore;
49 import org.eclipse.jdt.core.JavaModelException;
51 import org.eclipse.jdt.internal.corext.Corext;
52 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
53 import org.eclipse.jdt.internal.corext.refactoring.reorg.IPackageFragmentRootManipulationQuery;
54 import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil;
55 import org.eclipse.jdt.internal.corext.util.Messages;
57 import org.eclipse.jdt.ui.JavaElementLabels;
59 import org.eclipse.jdt.internal.ui.JavaPlugin;
60 import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
62 public class DeletePackageFragmentRootChange extends AbstractDeleteChange {
64 private final String fHandle;
65 private final IPackageFragmentRootManipulationQuery fUpdateClasspathQuery;
67 public DeletePackageFragmentRootChange(IPackageFragmentRoot root, boolean isExecuteChange,
68 IPackageFragmentRootManipulationQuery updateClasspathQuery) {
69 Assert.isNotNull(root);
70 Assert.isTrue(! root.isExternal());
71 fHandle= root.getHandleIdentifier();
72 fUpdateClasspathQuery= updateClasspathQuery;
74 if (isExecuteChange) {
75 // don't check for read-only resources since we already
76 // prompt the user via a dialog to confirm deletion of
77 // read only resource. The change is currently not used
79 setValidationMethod(VALIDATE_NOT_DIRTY);
81 setValidationMethod(VALIDATE_NOT_DIRTY | VALIDATE_NOT_READ_ONLY);
86 public String getName() {
87 String rootName= JavaElementLabels.getElementLabel(getRoot(), JavaElementLabels.ALL_DEFAULT);
88 return Messages.format(RefactoringCoreMessages.DeletePackageFragmentRootChange_delete, rootName);
92 public Object getModifiedElement() {
97 * @see org.eclipse.jdt.internal.corext.refactoring.base.JDTChange#getModifiedResource()
100 protected IResource getModifiedResource() {
101 return getRoot().getResource();
104 private IPackageFragmentRoot getRoot(){
105 return (IPackageFragmentRoot)JavaCore.create(fHandle);
109 protected Change doDelete(IProgressMonitor pm) throws CoreException {
110 if (! confirmDeleteIfReferenced())
111 return new NullChange();
112 int resourceUpdateFlags= IResource.KEEP_HISTORY;
113 int jCoreUpdateFlags= IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH | IPackageFragmentRoot.OTHER_REFERRING_PROJECTS_CLASSPATH;
115 pm.beginTask("", 2); //$NON-NLS-1$
116 IPackageFragmentRoot root= getRoot();
117 IResource rootResource= root.getResource();
118 CompositeChange result= new CompositeChange(getName());
120 ResourceDescription rootDescription = ResourceDescription.fromResource(rootResource);
121 IJavaProject[] referencingProjects= JavaElementUtil.getReferencingProjects(root);
122 HashMap<IFile, String> classpathFilesContents= new HashMap<IFile, String>();
123 for (int i= 0; i < referencingProjects.length; i++) {
124 IJavaProject javaProject= referencingProjects[i];
125 IFile classpathFile= javaProject.getProject().getFile(".classpath"); //$NON-NLS-1$
126 if (classpathFile.exists()) {
127 classpathFilesContents.put(classpathFile, getFileContents(classpathFile));
131 root.delete(resourceUpdateFlags, jCoreUpdateFlags, new SubProgressMonitor(pm, 1));
133 rootDescription.recordStateFromHistory(rootResource, new SubProgressMonitor(pm, 1));
134 for (Iterator<Entry<IFile, String>> iterator= classpathFilesContents.entrySet().iterator(); iterator.hasNext();) {
135 Entry<IFile, String> entry= iterator.next();
136 IFile file= entry.getKey();
137 String contents= entry.getValue();
138 //Restore time stamps? This should probably be some sort of UndoTextFileChange.
139 TextFileChange classpathUndo= new TextFileChange(Messages.format(RefactoringCoreMessages.DeletePackageFragmentRootChange_restore_file, BasicElementLabels.getPathLabel(file.getFullPath(), true)), file);
140 classpathUndo.setEdit(new ReplaceEdit(0, getFileLength(file), contents));
141 result.add(classpathUndo);
143 result.add(new UndoDeleteResourceChange(rootDescription));
149 private static String getFileContents(IFile file) throws CoreException {
150 ITextFileBufferManager manager= FileBuffers.getTextFileBufferManager();
151 IPath path= file.getFullPath();
152 manager.connect(path, LocationKind.IFILE, new NullProgressMonitor());
154 return manager.getTextFileBuffer(path, LocationKind.IFILE).getDocument().get();
156 manager.disconnect(path, LocationKind.IFILE, new NullProgressMonitor());
160 private static int getFileLength(IFile file) throws CoreException {
161 // Cannot use file buffers here, since they are not yet in sync at this point.
162 InputStream contents= file.getContents();
163 InputStreamReader reader;
165 reader= new InputStreamReader(contents, file.getCharset());
166 } catch (UnsupportedEncodingException e) {
168 reader= new InputStreamReader(contents);
171 return (int) reader.skip(Integer.MAX_VALUE);
172 } catch (IOException e) {
173 throw new CoreException(new Status(IStatus.ERROR, Corext.getPluginId(), e.getMessage(), e));
177 } catch (IOException e) {
182 private boolean confirmDeleteIfReferenced() throws JavaModelException {
183 IPackageFragmentRoot root= getRoot();
184 if (!root.isArchive() && !root.isExternal()) //for source folders, you don't ask, just do it
186 if (fUpdateClasspathQuery == null)
188 IJavaProject[] referencingProjects= JavaElementUtil.getReferencingProjects(getRoot());
189 if (referencingProjects.length <= 1)
191 return fUpdateClasspathQuery.confirmManipulation(getRoot(), referencingProjects);