KickJava   Java API By Example, From Geeks To Geeks.

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


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.resources.mapping.ResourceMapping;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.OperationCanceledException;
20 import org.eclipse.core.runtime.SubProgressMonitor;
21
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.IResource;
24
25 import org.eclipse.ltk.core.refactoring.Change;
26 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
27 import org.eclipse.ltk.core.refactoring.participants.ReorgExecutionLog;
28
29 import org.eclipse.jdt.core.IJavaProject;
30 import org.eclipse.jdt.core.IPackageFragmentRoot;
31 import org.eclipse.jdt.core.JavaCore;
32 import org.eclipse.jdt.core.JavaModelException;
33
34 import org.eclipse.jdt.internal.corext.refactoring.base.JDTChange;
35 import org.eclipse.jdt.internal.corext.refactoring.reorg.INewNameQuery;
36 import org.eclipse.jdt.internal.corext.refactoring.reorg.IPackageFragmentRootManipulationQuery;
37 import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil;
38 import org.eclipse.jdt.internal.corext.util.JavaElementResourceMapping;
39
40 abstract class PackageFragmentRootReorgChange extends JDTChange {
41
42     private final String JavaDoc fRootHandle;
43     private final IPath fDestinationPath;
44     private final INewNameQuery fNewNameQuery;
45     private final IPackageFragmentRootManipulationQuery fUpdateClasspathQuery;
46     
47     PackageFragmentRootReorgChange(IPackageFragmentRoot root, IProject destination, INewNameQuery newNameQuery,
48             IPackageFragmentRootManipulationQuery updateClasspathQuery) {
49         Assert.isTrue(! root.isExternal());
50         fRootHandle= root.getHandleIdentifier();
51         fDestinationPath= Utils.getResourcePath(destination);
52         fNewNameQuery= newNameQuery;
53         fUpdateClasspathQuery= updateClasspathQuery;
54     }
55
56     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
57         // we already ask for confirmation of move read only
58
// resources. Furthermore we don't do a validate
59
// edit since move source folders doesn't change
60
// an content
61
return isValid(pm, NONE);
62     }
63     
64     public final Change perform(IProgressMonitor pm) throws CoreException, OperationCanceledException {
65         pm.beginTask(getName(), 2);
66         try {
67             String JavaDoc newName= getNewResourceName();
68             IPackageFragmentRoot root= getRoot();
69             ResourceMapping mapping= JavaElementResourceMapping.create(root);
70             final Change result= doPerformReorg(getDestinationProjectPath().append(newName), new SubProgressMonitor(pm, 1));
71             markAsExecuted(root, mapping);
72             return result;
73         } finally {
74             pm.done();
75         }
76     }
77
78     protected abstract Change doPerformReorg(IPath destinationPath, IProgressMonitor pm) throws JavaModelException;
79
80     public Object JavaDoc getModifiedElement() {
81         return getRoot();
82     }
83     
84     protected IPackageFragmentRoot getRoot(){
85         return (IPackageFragmentRoot)JavaCore.create(fRootHandle);
86     }
87     
88     protected IPath getDestinationProjectPath(){
89         return fDestinationPath;
90     }
91
92     protected IProject getDestinationProject(){
93         return Utils.getProject(getDestinationProjectPath());
94     }
95     
96     private String JavaDoc getNewResourceName() throws OperationCanceledException {
97         if (fNewNameQuery == null)
98             return getRoot().getElementName();
99         String JavaDoc name= fNewNameQuery.getNewName();
100         if (name == null)
101             return getRoot().getElementName();
102         return name;
103     }
104     
105     protected int getUpdateModelFlags(boolean isCopy) throws JavaModelException{
106         final int destination= IPackageFragmentRoot.DESTINATION_PROJECT_CLASSPATH;
107         final int replace= IPackageFragmentRoot.REPLACE;
108         final int originating;
109         final int otherProjects;
110         if (isCopy){
111             originating= 0; //ORIGINATING_PROJECT_CLASSPATH does not apply to copy
112
otherProjects= 0;//OTHER_REFERRING_PROJECTS_CLASSPATH does not apply to copy
113
} else{
114             originating= IPackageFragmentRoot.ORIGINATING_PROJECT_CLASSPATH;
115             otherProjects= IPackageFragmentRoot.OTHER_REFERRING_PROJECTS_CLASSPATH;
116         }
117         
118         if (! JavaCore.create(getDestinationProject()).exists())
119             return replace | originating;
120
121         if (fUpdateClasspathQuery == null)
122             return replace | originating | destination;
123
124         IJavaProject[] referencingProjects= JavaElementUtil.getReferencingProjects(getRoot());
125         if (referencingProjects.length <= 1)
126             return replace | originating | destination;
127
128         boolean updateOtherProjectsToo= fUpdateClasspathQuery.confirmManipulation(getRoot(), referencingProjects);
129         if (updateOtherProjectsToo)
130             return replace | originating | destination | otherProjects;
131         else
132             return replace | originating | destination;
133     }
134     
135     protected int getResourceUpdateFlags(){
136         return IResource.KEEP_HISTORY | IResource.SHALLOW;
137     }
138     
139     private void markAsExecuted(IPackageFragmentRoot root, ResourceMapping mapping) {
140         ReorgExecutionLog log= (ReorgExecutionLog)getAdapter(ReorgExecutionLog.class);
141         if (log != null) {
142             log.markAsProcessed(root);
143             log.markAsProcessed(mapping);
144         }
145     }
146 }
147
Popular Tags