KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 import org.eclipse.core.resources.IResource;
25
26 import org.eclipse.ltk.core.refactoring.Change;
27 import org.eclipse.ltk.core.refactoring.ChangeDescriptor;
28 import org.eclipse.ltk.core.refactoring.CompositeChange;
29 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
30 import org.eclipse.ltk.core.refactoring.TextEditBasedChange;
31 import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
32 import org.eclipse.ltk.core.refactoring.participants.MoveProcessor;
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.SharableParticipants;
36
37 import org.eclipse.jdt.core.IJavaElement;
38 import org.eclipse.jdt.core.JavaModelException;
39
40 import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
41 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
42 import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange;
43 import org.eclipse.jdt.internal.corext.refactoring.participants.JavaProcessors;
44 import org.eclipse.jdt.internal.corext.refactoring.participants.ResourceProcessors;
45 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy;
46 import org.eclipse.jdt.internal.corext.refactoring.tagging.ICommentProvider;
47 import org.eclipse.jdt.internal.corext.refactoring.tagging.IQualifiedNameUpdating;
48 import org.eclipse.jdt.internal.corext.refactoring.tagging.IScriptableRefactoring;
49 import org.eclipse.jdt.internal.corext.util.Resources;
50
51 public final class JavaMoveProcessor extends MoveProcessor implements IScriptableRefactoring, ICommentProvider, IQualifiedNameUpdating, IReorgDestinationValidator {
52
53     public static final String JavaDoc IDENTIFIER= "org.eclipse.jdt.ui.MoveProcessor"; //$NON-NLS-1$
54

55     private String JavaDoc fComment;
56
57     private ICreateTargetQueries fCreateTargetQueries;
58
59     private IMovePolicy fMovePolicy;
60
61     private IReorgQueries fReorgQueries;
62
63     private boolean fWasCanceled;
64
65     public JavaMoveProcessor(IMovePolicy policy) {
66         fMovePolicy= policy;
67     }
68
69     public boolean canChildrenBeDestinations(IJavaElement javaElement) {
70         return fMovePolicy.canChildrenBeDestinations(javaElement);
71     }
72
73     public boolean canChildrenBeDestinations(IResource resource) {
74         return fMovePolicy.canChildrenBeDestinations(resource);
75     }
76
77     public boolean canElementBeDestination(IJavaElement javaElement) {
78         return fMovePolicy.canElementBeDestination(javaElement);
79     }
80
81     public boolean canElementBeDestination(IResource resource) {
82         return fMovePolicy.canElementBeDestination(resource);
83     }
84
85     public boolean canEnableComment() {
86         return true;
87     }
88
89     public boolean canEnableQualifiedNameUpdating() {
90         return fMovePolicy.canEnableQualifiedNameUpdating();
91     }
92
93     public boolean canUpdateQualifiedNames() {
94         return fMovePolicy.canUpdateQualifiedNames();
95     }
96
97     public boolean canUpdateReferences() {
98         return fMovePolicy.canUpdateReferences();
99     }
100
101     public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException {
102         try {
103             Assert.isNotNull(fReorgQueries);
104             fWasCanceled= false;
105             return fMovePolicy.checkFinalConditions(pm, context, fReorgQueries);
106         } catch (OperationCanceledException e) {
107             fWasCanceled= true;
108             throw e;
109         }
110     }
111
112     public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
113         pm.beginTask("", 1); //$NON-NLS-1$
114
try {
115             RefactoringStatus result= new RefactoringStatus();
116             result.merge(RefactoringStatus.create(Resources.checkInSync(ReorgUtils.getNotNulls(fMovePolicy.getResources()))));
117             IResource[] javaResources= ReorgUtils.getResources(fMovePolicy.getJavaElements());
118             result.merge(RefactoringStatus.create(Resources.checkInSync(ReorgUtils.getNotNulls(javaResources))));
119             return result;
120         } finally {
121             pm.done();
122         }
123     }
124
125     public Change createChange(IProgressMonitor pm) throws CoreException {
126         Assert.isTrue(fMovePolicy.getJavaElementDestination() == null || fMovePolicy.getResourceDestination() == null);
127         Assert.isTrue(fMovePolicy.getJavaElementDestination() != null || fMovePolicy.getResourceDestination() != null);
128         try {
129             final DynamicValidationStateChange result= new DynamicValidationStateChange(RefactoringCoreMessages.JavaMoveProcessor_change_name) {
130
131                 public ChangeDescriptor getDescriptor() {
132                     return fMovePolicy.getDescriptor();
133                 }
134
135                 public Change perform(IProgressMonitor pm2) throws CoreException {
136                     Change change= super.perform(pm2);
137                     Change[] changes= getChildren();
138                     for (int index= 0; index < changes.length; index++) {
139                         if (!(changes[index] instanceof TextEditBasedChange))
140                             return null;
141                     }
142                     return change;
143                 }
144             };
145             CreateTargetExecutionLog log= null;
146             if (fCreateTargetQueries instanceof MonitoringCreateTargetQueries) {
147                 final MonitoringCreateTargetQueries queries= (MonitoringCreateTargetQueries) fCreateTargetQueries;
148                 final ICreateTargetQueries delegate= queries.getDelegate();
149                 if (delegate instanceof LoggedCreateTargetQueries)
150                     log= queries.getCreateTargetExecutionLog();
151             }
152             if (log != null) {
153                 final Object JavaDoc[] selected= log.getSelectedElements();
154                 for (int index= 0; index < selected.length; index++) {
155                     result.add(new LoggedCreateTargetChange(selected[index], fCreateTargetQueries));
156                 }
157             }
158             Change change= fMovePolicy.createChange(pm);
159             if (change instanceof CompositeChange) {
160                 CompositeChange subComposite= (CompositeChange) change;
161                 result.merge(subComposite);
162             } else {
163                 result.add(change);
164             }
165             return result;
166         } finally {
167             pm.done();
168         }
169     }
170
171     private String JavaDoc[] getAffectedProjectNatures() throws CoreException {
172         String JavaDoc[] jNatures= JavaProcessors.computeAffectedNaturs(fMovePolicy.getJavaElements());
173         String JavaDoc[] rNatures= ResourceProcessors.computeAffectedNatures(fMovePolicy.getResources());
174         Set JavaDoc result= new HashSet JavaDoc();
175         result.addAll(Arrays.asList(jNatures));
176         result.addAll(Arrays.asList(rNatures));
177         return (String JavaDoc[]) result.toArray(new String JavaDoc[result.size()]);
178     }
179
180     public String JavaDoc getComment() {
181         return fComment;
182     }
183
184     public Object JavaDoc getCommonParentForInputElements() {
185         return new ParentChecker(fMovePolicy.getResources(), fMovePolicy.getJavaElements()).getCommonParent();
186     }
187
188     public ICreateTargetQuery getCreateTargetQuery() {
189         return fMovePolicy.getCreateTargetQuery(fCreateTargetQueries);
190     }
191
192     protected Object JavaDoc getDestination() {
193         IJavaElement je= fMovePolicy.getJavaElementDestination();
194         if (je != null)
195             return je;
196         return fMovePolicy.getResourceDestination();
197     }
198
199     public Object JavaDoc[] getElements() {
200         List JavaDoc result= new ArrayList JavaDoc();
201         result.addAll(Arrays.asList(fMovePolicy.getJavaElements()));
202         result.addAll(Arrays.asList(fMovePolicy.getResources()));
203         return result.toArray();
204     }
205
206     public String JavaDoc getFilePatterns() {
207         return fMovePolicy.getFilePatterns();
208     }
209
210     public String JavaDoc getIdentifier() {
211         return IDENTIFIER;
212     }
213
214     public IJavaElement[] getJavaElements() {
215         return fMovePolicy.getJavaElements();
216     }
217
218     public String JavaDoc getProcessorName() {
219         return RefactoringCoreMessages.MoveRefactoring_0;
220     }
221
222     public IResource[] getResources() {
223         return fMovePolicy.getResources();
224     }
225
226     public boolean getUpdateQualifiedNames() {
227         return fMovePolicy.getUpdateQualifiedNames();
228     }
229
230     public boolean getUpdateReferences() {
231         if (!canUpdateReferences())
232             return false;
233         return fMovePolicy.getUpdateReferences();
234     }
235
236     public boolean hasAllInputSet() {
237         return fMovePolicy.hasAllInputSet();
238     }
239
240     public boolean hasDestinationSet() {
241         return fMovePolicy.getJavaElementDestination() != null || fMovePolicy.getResourceDestination() != null;
242     }
243
244     public RefactoringStatus initialize(RefactoringArguments arguments) {
245         setReorgQueries(new NullReorgQueries());
246         final RefactoringStatus status= new RefactoringStatus();
247         if (arguments instanceof JavaRefactoringArguments) {
248             final JavaRefactoringArguments extended= (JavaRefactoringArguments) arguments;
249             fMovePolicy= ReorgPolicyFactory.createMovePolicy(status, arguments);
250             if (fMovePolicy != null && !status.hasFatalError()) {
251                 final CreateTargetExecutionLog log= ReorgPolicyFactory.loadCreateTargetExecutionLog(status, extended);
252                 if (log != null && !status.hasFatalError()) {
253                     fMovePolicy.setDestinationCheck(false);
254                     fCreateTargetQueries= new MonitoringCreateTargetQueries(new LoggedCreateTargetQueries(log), log);
255                 }
256                 status.merge(fMovePolicy.initialize(arguments));
257             }
258         } else
259             return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.InitializableRefactoring_inacceptable_arguments);
260         return status;
261     }
262
263     public boolean isApplicable() throws CoreException {
264         return fMovePolicy.canEnable();
265     }
266
267     public boolean isTextualMove() {
268         return fMovePolicy.isTextualMove();
269     }
270
271     public RefactoringParticipant[] loadParticipants(RefactoringStatus status, SharableParticipants shared) throws CoreException {
272         return fMovePolicy.loadParticipants(status, this, getAffectedProjectNatures(), shared);
273     }
274
275     public Change postCreateChange(Change[] participantChanges, IProgressMonitor pm) throws CoreException {
276         return fMovePolicy.postCreateChange(participantChanges, pm);
277     }
278
279     public void setComment(String JavaDoc comment) {
280         fComment= comment;
281     }
282
283     public void setCreateTargetQueries(ICreateTargetQueries queries) {
284         Assert.isNotNull(queries);
285         fCreateTargetQueries= new MonitoringCreateTargetQueries(queries, fMovePolicy.getCreateTargetExecutionLog());
286     }
287
288     public RefactoringStatus setDestination(IJavaElement destination) throws JavaModelException {
289         return fMovePolicy.setDestination(destination);
290     }
291
292     public RefactoringStatus setDestination(IResource destination) throws JavaModelException {
293         return fMovePolicy.setDestination(destination);
294     }
295
296     public void setFilePatterns(String JavaDoc patterns) {
297         fMovePolicy.setFilePatterns(patterns);
298     }
299
300     public void setReorgQueries(IReorgQueries queries) {
301         Assert.isNotNull(queries);
302         fReorgQueries= queries;
303     }
304
305     public void setUpdateQualifiedNames(boolean update) {
306         fMovePolicy.setUpdateQualifiedNames(update);
307     }
308
309     public void setUpdateReferences(boolean update) {
310         fMovePolicy.setUpdateReferences(update);
311     }
312
313     public boolean wasCanceled() {
314         return fWasCanceled;
315     }
316 }
317
Popular Tags