KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > reorg > JavaCopyProcessor


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.reorg;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.OperationCanceledException;
23 import org.eclipse.core.runtime.SubProgressMonitor;
24
25 import org.eclipse.core.resources.IResource;
26
27 import org.eclipse.ltk.core.refactoring.Change;
28 import org.eclipse.ltk.core.refactoring.ChangeDescriptor;
29 import org.eclipse.ltk.core.refactoring.CompositeChange;
30 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
31 import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
32 import org.eclipse.ltk.core.refactoring.participants.CopyProcessor;
33 import org.eclipse.ltk.core.refactoring.participants.RefactoringArguments;
34 import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
35 import org.eclipse.ltk.core.refactoring.participants.ReorgExecutionLog;
36 import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
37
38 import org.eclipse.jdt.core.IJavaElement;
39 import org.eclipse.jdt.core.JavaModelException;
40
41 import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
42 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
43 import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange;
44 import org.eclipse.jdt.internal.corext.refactoring.participants.JavaProcessors;
45 import org.eclipse.jdt.internal.corext.refactoring.participants.ResourceProcessors;
46 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.ICopyPolicy;
47 import org.eclipse.jdt.internal.corext.refactoring.tagging.ICommentProvider;
48 import org.eclipse.jdt.internal.corext.refactoring.tagging.IScriptableRefactoring;
49 import org.eclipse.jdt.internal.corext.util.Resources;
50
51 public final class JavaCopyProcessor extends CopyProcessor implements IReorgDestinationValidator, IScriptableRefactoring, ICommentProvider {
52
53     public static final String JavaDoc IDENTIFIER= "org.eclipse.jdt.ui.CopyProcessor"; //$NON-NLS-1$
54

55     private String JavaDoc fComment;
56
57     private ICopyPolicy fCopyPolicy;
58
59     private ReorgExecutionLog fExecutionLog;
60
61     private INewNameQueries fNewNameQueries;
62
63     private IReorgQueries fReorgQueries;
64
65     public JavaCopyProcessor(ICopyPolicy copyPolicy) {
66         fCopyPolicy= copyPolicy;
67     }
68
69     public boolean canChildrenBeDestinations(IJavaElement javaElement) {
70         return fCopyPolicy.canChildrenBeDestinations(javaElement);
71     }
72
73     public boolean canChildrenBeDestinations(IResource resource) {
74         return fCopyPolicy.canChildrenBeDestinations(resource);
75     }
76
77     public boolean canElementBeDestination(IJavaElement javaElement) {
78         return fCopyPolicy.canElementBeDestination(javaElement);
79     }
80
81     public boolean canElementBeDestination(IResource resource) {
82         return fCopyPolicy.canElementBeDestination(resource);
83     }
84
85     public boolean canEnableComment() {
86         return true;
87     }
88
89     public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException {
90         Assert.isNotNull(fNewNameQueries, "Missing new name queries"); //$NON-NLS-1$
91
Assert.isNotNull(fReorgQueries, "Missing reorg queries"); //$NON-NLS-1$
92
pm.beginTask("", 2); //$NON-NLS-1$
93
RefactoringStatus result= fCopyPolicy.checkFinalConditions(new SubProgressMonitor(pm, 1), context, fReorgQueries);
94         result.merge(context.check(new SubProgressMonitor(pm, 1)));
95         return result;
96     }
97
98     public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
99         RefactoringStatus result= new RefactoringStatus();
100         result.merge(RefactoringStatus.create(Resources.checkInSync(ReorgUtils.getNotNulls(fCopyPolicy.getResources()))));
101         IResource[] javaResources= ReorgUtils.getResources(fCopyPolicy.getJavaElements());
102         result.merge(RefactoringStatus.create(Resources.checkInSync(ReorgUtils.getNotNulls(javaResources))));
103         return result;
104     }
105
106     public Change createChange(IProgressMonitor pm) throws CoreException {
107         Assert.isNotNull(fNewNameQueries);
108         Assert.isTrue(fCopyPolicy.getJavaElementDestination() == null || fCopyPolicy.getResourceDestination() == null);
109         Assert.isTrue(fCopyPolicy.getJavaElementDestination() != null || fCopyPolicy.getResourceDestination() != null);
110         try {
111             final DynamicValidationStateChange result= new DynamicValidationStateChange(getChangeName()) {
112
113                 public Object JavaDoc getAdapter(Class JavaDoc adapter) {
114                     if (ReorgExecutionLog.class.equals(adapter))
115                         return fExecutionLog;
116                     return super.getAdapter(adapter);
117                 }
118
119                 public ChangeDescriptor getDescriptor() {
120                     return fCopyPolicy.getDescriptor();
121                 }
122
123                 public Change perform(IProgressMonitor pm2) throws CoreException {
124                     try {
125                         super.perform(pm2);
126                     } catch (OperationCanceledException e) {
127                         fExecutionLog.markAsCanceled();
128                         throw e;
129                     }
130                     return null;
131                 }
132             };
133             Change change= fCopyPolicy.createChange(pm, new MonitoringNewNameQueries(fNewNameQueries, fExecutionLog));
134             if (change instanceof CompositeChange) {
135                 CompositeChange subComposite= (CompositeChange) change;
136                 result.merge(subComposite);
137             } else {
138                 result.add(change);
139             }
140             return result;
141         } finally {
142             pm.done();
143         }
144     }
145
146     private String JavaDoc[] getAffectedProjectNatures() throws CoreException {
147         String JavaDoc[] jNatures= JavaProcessors.computeAffectedNaturs(fCopyPolicy.getJavaElements());
148         String JavaDoc[] rNatures= ResourceProcessors.computeAffectedNatures(fCopyPolicy.getResources());
149         Set JavaDoc result= new HashSet JavaDoc();
150         result.addAll(Arrays.asList(jNatures));
151         result.addAll(Arrays.asList(rNatures));
152         return (String JavaDoc[]) result.toArray(new String JavaDoc[result.size()]);
153     }
154
155     private String JavaDoc getChangeName() {
156         return RefactoringCoreMessages.JavaCopyProcessor_changeName;
157     }
158
159     public String JavaDoc getComment() {
160         return fComment;
161     }
162
163     public Object JavaDoc getCommonParentForInputElements() {
164         return new ParentChecker(fCopyPolicy.getResources(), fCopyPolicy.getJavaElements()).getCommonParent();
165     }
166
167     public Object JavaDoc[] getElements() {
168         IJavaElement[] jElements= fCopyPolicy.getJavaElements();
169         IResource[] resources= fCopyPolicy.getResources();
170         List JavaDoc result= new ArrayList JavaDoc(jElements.length + resources.length);
171         result.addAll(Arrays.asList(jElements));
172         result.addAll(Arrays.asList(resources));
173         return result.toArray();
174     }
175
176     public String JavaDoc getIdentifier() {
177         return IDENTIFIER;
178     }
179
180     public IJavaElement[] getJavaElements() {
181         return fCopyPolicy.getJavaElements();
182     }
183
184     public String JavaDoc getProcessorName() {
185         return RefactoringCoreMessages.JavaCopyProcessor_processorName;
186     }
187
188     public IResource[] getResources() {
189         return fCopyPolicy.getResources();
190     }
191
192     public RefactoringStatus initialize(RefactoringArguments arguments) {
193         setReorgQueries(new NullReorgQueries());
194         final RefactoringStatus status= new RefactoringStatus();
195         if (arguments instanceof JavaRefactoringArguments) {
196             final JavaRefactoringArguments extended= (JavaRefactoringArguments) arguments;
197             fCopyPolicy= ReorgPolicyFactory.createCopyPolicy(status, extended);
198             if (fCopyPolicy != null && !status.hasFatalError()) {
199                 status.merge(fCopyPolicy.initialize(arguments));
200                 if (!status.hasFatalError()) {
201                     final ReorgExecutionLog log= ReorgPolicyFactory.loadReorgExecutionLog(status, extended);
202                     if (log != null && !status.hasFatalError())
203                         setNewNameQueries(new LoggedNewNameQueries(log));
204                 }
205             }
206         } else
207             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InitializableRefactoring_inacceptable_arguments);
208         return status;
209     }
210
211     public boolean isApplicable() throws CoreException {
212         return fCopyPolicy.canEnable();
213     }
214
215     public RefactoringParticipant[] loadParticipants(RefactoringStatus status, SharableParticipants sharedParticipants) throws CoreException {
216         RefactoringParticipant[] result= fCopyPolicy.loadParticipants(status, this, getAffectedProjectNatures(), sharedParticipants);
217         fExecutionLog= fCopyPolicy.getReorgExecutionLog();
218         return result;
219     }
220
221     public void setComment(String JavaDoc comment) {
222         fComment= comment;
223     }
224
225     public RefactoringStatus setDestination(IJavaElement destination) throws JavaModelException {
226         return fCopyPolicy.setDestination(destination);
227     }
228
229     public RefactoringStatus setDestination(IResource destination) throws JavaModelException {
230         return fCopyPolicy.setDestination(destination);
231     }
232
233     public void setNewNameQueries(INewNameQueries newNameQueries) {
234         Assert.isNotNull(newNameQueries);
235         fNewNameQueries= newNameQueries;
236     }
237
238     public void setReorgQueries(IReorgQueries queries) {
239         Assert.isNotNull(queries);
240         fReorgQueries= queries;
241     }
242 }
243
Popular Tags