KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > rename > RenameSourceFolderProcessor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.rename;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.Path;
18
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.ResourcesPlugin;
23
24 import org.eclipse.ltk.core.refactoring.Change;
25 import org.eclipse.ltk.core.refactoring.RefactoringDescriptor;
26 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
27 import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
28 import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;
29 import org.eclipse.ltk.core.refactoring.participants.RenameArguments;
30
31 import org.eclipse.jdt.core.IJavaProject;
32 import org.eclipse.jdt.core.IPackageFragmentRoot;
33 import org.eclipse.jdt.core.JavaCore;
34 import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
35 import org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor;
36
37 import org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment;
38 import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
39 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
40 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
41 import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange;
42 import org.eclipse.jdt.internal.corext.refactoring.changes.RenameSourceFolderChange;
43 import org.eclipse.jdt.internal.corext.refactoring.code.ScriptableRefactoring;
44 import org.eclipse.jdt.internal.corext.refactoring.participants.JavaProcessors;
45 import org.eclipse.jdt.internal.corext.util.Messages;
46
47 import org.eclipse.jdt.internal.ui.refactoring.RefactoringSaveHelper;
48
49 public final class RenameSourceFolderProcessor extends JavaRenameProcessor {
50     
51     private static final String JavaDoc ATTRIBUTE_PATH= "path"; //$NON-NLS-1$
52
private static final String JavaDoc ATTRIBUTE_NAME= "name"; //$NON-NLS-1$
53

54     private IPackageFragmentRoot fSourceFolder;
55
56     public static final String JavaDoc IDENTIFIER= "org.eclipse.jdt.ui.renameSourceFolderProcessor"; //$NON-NLS-1$
57

58     /**
59      * Creates a new rename source folder processor.
60      * @param root the package fragment root, or <code>null</code> if invoked by scripting
61      */

62     public RenameSourceFolderProcessor(IPackageFragmentRoot root) {
63         fSourceFolder= root;
64         if (root != null)
65             setNewElementName(root.getElementName());
66     }
67
68     public String JavaDoc getIdentifier() {
69         return IDENTIFIER;
70     }
71     
72     public boolean isApplicable() throws CoreException {
73         return RefactoringAvailabilityTester.isRenameAvailable(fSourceFolder);
74     }
75     
76     public String JavaDoc getProcessorName() {
77         return RefactoringCoreMessages.RenameSourceFolderRefactoring_rename;
78     }
79     
80     protected String JavaDoc[] getAffectedProjectNatures() throws CoreException {
81         return JavaProcessors.computeAffectedNatures(fSourceFolder);
82     }
83     
84     public Object JavaDoc[] getElements() {
85         return new Object JavaDoc[] {fSourceFolder};
86     }
87
88     public Object JavaDoc getNewElement() throws CoreException {
89         IPackageFragmentRoot[] roots= fSourceFolder.getJavaProject().getPackageFragmentRoots();
90         for (int i= 0; i < roots.length; i++) {
91             if (roots[i].getElementName().equals(getNewElementName()))
92                 return roots[i];
93         }
94         return null;
95     }
96     
97     public int getSaveMode() {
98         return RefactoringSaveHelper.SAVE_ALL;
99     }
100     
101     protected RenameModifications computeRenameModifications() throws CoreException {
102         RenameModifications result= new RenameModifications();
103         result.rename(fSourceFolder, new RenameArguments(getNewElementName(), getUpdateReferences()));
104         return result;
105     }
106     
107     protected IFile[] getChangedFiles() throws CoreException {
108         return new IFile[0];
109     }
110     
111     //---- IRenameProcessor ----------------------------------------------
112

113     public String JavaDoc getCurrentElementName() {
114         return fSourceFolder.getElementName();
115     }
116             
117     public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
118         return new RefactoringStatus();
119     }
120
121     public RefactoringStatus checkNewElementName(String JavaDoc newName) throws CoreException {
122         Assert.isNotNull(newName, "new name"); //$NON-NLS-1$
123
if (! newName.trim().equals(newName))
124             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameSourceFolderRefactoring_blank);
125         
126         IContainer c= fSourceFolder.getResource().getParent();
127         if (! c.getFullPath().isValidSegment(newName))
128             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameSourceFolderRefactoring_invalid_name);
129         
130         RefactoringStatus result= RefactoringStatus.create(c.getWorkspace().validateName(newName, IResource.FOLDER));
131         if (result.hasFatalError())
132             return result;
133                 
134         result.merge(RefactoringStatus.create(c.getWorkspace().validatePath(createNewPath(newName), IResource.FOLDER)));
135         if (result.hasFatalError())
136             return result;
137             
138         IJavaProject project= fSourceFolder.getJavaProject();
139         IPath p= project.getProject().getFullPath().append(newName);
140         if (project.findPackageFragmentRoot(p) != null)
141             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameSourceFolderRefactoring_already_exists);
142         
143         if (project.getProject().findMember(new Path(newName)) != null)
144             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameSourceFolderRefactoring_alread_exists);
145         return result;
146     }
147     
148     private String JavaDoc createNewPath(String JavaDoc newName) {
149         return fSourceFolder.getPath().removeLastSegments(1).append(newName).toString();
150     }
151
152     protected RefactoringStatus doCheckFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException {
153         pm.beginTask("", 1); //$NON-NLS-1$
154
try{
155             return new RefactoringStatus();
156         } finally{
157             pm.done();
158         }
159     }
160     
161     public boolean getUpdateReferences() {
162         return true;
163     }
164
165     public Change createChange(IProgressMonitor monitor) throws CoreException {
166         monitor.beginTask(RefactoringCoreMessages.RenameTypeRefactoring_creating_change, 1);
167         try {
168             final IResource resource= fSourceFolder.getResource();
169             final String JavaDoc project= resource.getProject().getName();
170             final String JavaDoc newName= getNewElementName();
171             final String JavaDoc description= Messages.format(RefactoringCoreMessages.RenameSourceFolderChange_descriptor_description_short, fSourceFolder.getElementName());
172             final String JavaDoc header= Messages.format(RefactoringCoreMessages.RenameSourceFolderChange_descriptor_description, new String JavaDoc[] { resource.getFullPath().toString(), newName});
173             final String JavaDoc comment= new JDTRefactoringDescriptorComment(project, this, header).asString();
174             final RenameJavaElementDescriptor descriptor= new RenameJavaElementDescriptor(IJavaRefactorings.RENAME_SOURCE_FOLDER);
175             descriptor.setProject(project);
176             descriptor.setDescription(description);
177             descriptor.setComment(comment);
178             descriptor.setFlags(RefactoringDescriptor.NONE);
179             descriptor.setJavaElement(fSourceFolder);
180             descriptor.setNewName(newName);
181             return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.RenameSourceFolderRefactoring_rename, new Change[] { new RenameSourceFolderChange(fSourceFolder, newName)});
182         } finally {
183             monitor.done();
184         }
185     }
186
187     public RefactoringStatus initialize(RefactoringArguments arguments) {
188         if (arguments instanceof JavaRefactoringArguments) {
189             final JavaRefactoringArguments generic= (JavaRefactoringArguments) arguments;
190             final String JavaDoc path= generic.getAttribute(ATTRIBUTE_PATH);
191             if (path != null) {
192                 final IResource resource= ResourcesPlugin.getWorkspace().getRoot().findMember(new Path(path));
193                 if (resource == null || !resource.exists())
194                     return ScriptableRefactoring.createInputFatalStatus(resource, getRefactoring().getName(), IJavaRefactorings.RENAME_SOURCE_FOLDER);
195                 else
196                     fSourceFolder= (IPackageFragmentRoot) JavaCore.create(resource);
197             } else
198                 return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_PATH));
199             final String JavaDoc name= generic.getAttribute(ATTRIBUTE_NAME);
200             if (name != null && !"".equals(name)) //$NON-NLS-1$
201
setNewElementName(name);
202             else
203                 return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_NAME));
204         } else
205             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InitializableRefactoring_inacceptable_arguments);
206         return new RefactoringStatus();
207     }
208 }
209
Popular Tags