KickJava   Java API By Example, From Geeks To Geeks.

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


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.changes;
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.OperationCanceledException;
18 import org.eclipse.core.runtime.SubProgressMonitor;
19
20 import org.eclipse.core.resources.IContainer;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IFolder;
23 import org.eclipse.core.resources.IProject;
24 import org.eclipse.core.resources.IResource;
25
26 import org.eclipse.ltk.core.refactoring.Change;
27 import org.eclipse.ltk.core.refactoring.participants.ReorgExecutionLog;
28
29 import org.eclipse.jdt.internal.corext.refactoring.base.JDTChange;
30 import org.eclipse.jdt.internal.corext.refactoring.reorg.INewNameQuery;
31 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils;
32
33 abstract class ResourceReorgChange extends JDTChange {
34     
35     private final IPath fResourcePath;
36     private final boolean fIsFile;
37     private final IPath fDestinationPath;
38     private final boolean fIsDestinationProject;
39     private final INewNameQuery fNewNameQuery;
40
41     ResourceReorgChange(IResource res, IContainer dest, INewNameQuery nameQuery){
42         Assert.isTrue(res instanceof IFile || res instanceof IFolder);
43         fIsFile= (res instanceof IFile);
44         fResourcePath= Utils.getResourcePath(res);
45     
46         Assert.isTrue(dest instanceof IProject || dest instanceof IFolder);
47         fIsDestinationProject= (dest instanceof IProject);
48         fDestinationPath= Utils.getResourcePath(dest);
49         fNewNameQuery= nameQuery;
50     }
51     
52     protected abstract Change doPerformReorg(IPath path, IProgressMonitor pm) throws CoreException;
53     
54     /* non java-doc
55      * @see IChange#perform(ChangeContext, IProgressMonitor)
56      */

57     public final Change perform(IProgressMonitor pm) throws CoreException, OperationCanceledException {
58         try{
59             pm.beginTask(getName(), 2);
60             
61             String JavaDoc newName= getNewResourceName();
62             IResource resource= getResource();
63             boolean performReorg= deleteIfAlreadyExists(new SubProgressMonitor(pm, 1), newName);
64             if (!performReorg)
65                 return null;
66             final Change result= doPerformReorg(getDestinationPath(newName), new SubProgressMonitor(pm, 1));
67             markAsExecuted(resource);
68             return result;
69         } finally {
70             pm.done();
71         }
72     }
73
74     protected IPath getDestinationPath(String JavaDoc newName) {
75         return getDestination().getFullPath().append(newName);
76     }
77
78     /**
79      * returns false if source and destination are the same (in workspace or on disk)
80      * in such case, no action should be performed
81      */

82     private boolean deleteIfAlreadyExists(IProgressMonitor pm, String JavaDoc newName) throws CoreException {
83         pm.beginTask("", 1); //$NON-NLS-1$
84
IResource current= getDestination().findMember(newName);
85         if (current == null)
86             return true;
87         if (! current.exists())
88             return true;
89
90         IResource resource= getResource();
91         Assert.isNotNull(resource);
92             
93         if (ReorgUtils.areEqualInWorkspaceOrOnDisk(resource, current))
94             return false;
95         
96         if (current instanceof IFile)
97             ((IFile)current).delete(false, true, new SubProgressMonitor(pm, 1));
98         else if (current instanceof IFolder)
99             ((IFolder)current).delete(false, true, new SubProgressMonitor(pm, 1));
100         else
101             Assert.isTrue(false);
102             
103         return true;
104     }
105     
106
107     private String JavaDoc getNewResourceName() throws OperationCanceledException {
108         if (fNewNameQuery == null)
109             return getResource().getName();
110         String JavaDoc name= fNewNameQuery.getNewName();
111         if (name == null)
112             return getResource().getName();
113         return name;
114     }
115     
116     /* non java-doc
117      * @see IChange#getModifiedLanguageElement()
118      */

119     public Object JavaDoc getModifiedElement() {
120         return getResource();
121     }
122
123     private IFile getFile(){
124         return Utils.getFile(fResourcePath);
125     }
126     
127     private IFolder getFolder(){
128         return Utils.getFolder(fResourcePath);
129     }
130     
131     protected IResource getResource(){
132         if (fIsFile)
133             return getFile();
134         else
135             return getFolder();
136     }
137     
138     IContainer getDestination(){
139         if (fIsDestinationProject)
140             return Utils.getProject(fDestinationPath);
141         else
142             return Utils.getFolder(fDestinationPath);
143     }
144
145     protected int getReorgFlags() {
146         return IResource.KEEP_HISTORY | IResource.SHALLOW;
147     }
148     
149     private void markAsExecuted(IResource resource) {
150         ReorgExecutionLog log= (ReorgExecutionLog)getAdapter(ReorgExecutionLog.class);
151         if (log != null) {
152             log.markAsProcessed(resource);
153         }
154     }
155 }
156
157
Popular Tags