KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > changes > DeletePackageFragmentRootChange


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.corext.refactoring.changes;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.InputStreamReader JavaDoc;
16 import java.io.UnsupportedEncodingException JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
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 JavaDoc 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 JavaDoc getName() {
76         String JavaDoc[] keys= {getRoot().getElementName()};
77         return Messages.format(RefactoringCoreMessages.DeletePackageFragmentRootChange_delete, keys);
78     }
79
80     public Object JavaDoc 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             // don't check for read-only resources since we already
91
// prompt the user via a dialog to confirm deletion of
92
// read only resource. The change is currently not used
93
// as
94
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); //$NON-NLS-1$
107
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 JavaDoc/*<IFile, String>*/ classpathFilesContents= new HashMap JavaDoc();
114         for (int i= 0; i < referencingProjects.length; i++) {
115             IJavaProject javaProject= referencingProjects[i];
116             IFile classpathFile= javaProject.getProject().getFile(".classpath"); //$NON-NLS-1$
117
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 JavaDoc iterator= classpathFilesContents.entrySet().iterator(); iterator.hasNext();) {
126             Entry entry= (Entry) iterator.next();
127             IFile file= (IFile) entry.getKey();
128             String JavaDoc contents= (String JavaDoc) entry.getValue();
129             //Restore time stamps? This should probably be some sort of UndoTextFileChange.
130
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 JavaDoc 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         // Cannot use file buffers here, since they are not yet in sync at this point.
153
InputStream JavaDoc contents= file.getContents();
154         InputStreamReader JavaDoc reader;
155         try {
156             reader= new InputStreamReader JavaDoc(contents, file.getCharset());
157         } catch (UnsupportedEncodingException JavaDoc e) {
158             JavaPlugin.log(e);
159             reader= new InputStreamReader JavaDoc(contents);
160         }
161         try {
162             return (int) reader.skip(Integer.MAX_VALUE);
163         } catch (IOException JavaDoc 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()) //for source folders, you don't ask, just do it
170
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