]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/core refactoring/org/eclipse/jdt/internal/corext/refactoring/reorg/JavaMoveProcessor.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / core refactoring / org / eclipse / jdt / internal / corext / refactoring / reorg / JavaMoveProcessor.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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.lang.reflect.InvocationTargetException;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19
20 import org.eclipse.swt.events.SelectionAdapter;
21 import org.eclipse.swt.events.SelectionEvent;
22 import org.eclipse.swt.widgets.Button;
23 import org.eclipse.swt.widgets.Shell;
24
25 import org.eclipse.core.runtime.Assert;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IAdaptable;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.OperationCanceledException;
30
31 import org.eclipse.core.resources.IResource;
32
33 import org.eclipse.jface.operation.IRunnableContext;
34
35 import org.eclipse.ui.PlatformUI;
36
37 import org.eclipse.ltk.core.refactoring.Change;
38 import org.eclipse.ltk.core.refactoring.ChangeDescriptor;
39 import org.eclipse.ltk.core.refactoring.Refactoring;
40 import org.eclipse.ltk.core.refactoring.RefactoringCore;
41 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
42 import org.eclipse.ltk.core.refactoring.TextEditBasedChange;
43 import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
44 import org.eclipse.ltk.core.refactoring.participants.MoveProcessor;
45 import org.eclipse.ltk.core.refactoring.participants.MoveRefactoring;
46 import org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant;
47 import org.eclipse.ltk.core.refactoring.participants.SharableParticipants;
48 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
49
50 import org.eclipse.jdt.core.IJavaElement;
51 import org.eclipse.jdt.core.JavaModelException;
52
53 import org.eclipse.jdt.internal.corext.refactoring.JavaRefactoringArguments;
54 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
55 import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange;
56 import org.eclipse.jdt.internal.corext.refactoring.participants.JavaProcessors;
57 import org.eclipse.jdt.internal.corext.refactoring.participants.ResourceProcessors;
58 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy;
59 import org.eclipse.jdt.internal.corext.refactoring.tagging.IQualifiedNameUpdating;
60 import org.eclipse.jdt.internal.corext.util.Resources;
61
62 import org.eclipse.jdt.ui.refactoring.IRefactoringProcessorIds;
63
64 import org.eclipse.jdt.internal.ui.refactoring.RefactoringExecutionHelper;
65 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
66 import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter;
67 import org.eclipse.jdt.internal.ui.refactoring.reorg.CreateTargetQueries;
68 import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgMoveWizard;
69 import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgMoveWizard.MoveInputPage;
70 import org.eclipse.jdt.internal.ui.refactoring.reorg.ReorgQueries;
71
72 public final class JavaMoveProcessor extends MoveProcessor implements IQualifiedNameUpdating, IReorgDestinationValidator {
73
74         public ICreateTargetQueries fCreateTargetQueries;
75
76         public IMovePolicy fMovePolicy;
77
78         private IReorgQueries fReorgQueries;
79
80         private boolean fWasCanceled;
81
82         public JavaMoveProcessor(IMovePolicy policy) {
83                 fMovePolicy= policy;
84         }
85
86         public JavaMoveProcessor(JavaRefactoringArguments arguments, RefactoringStatus status) {
87                 RefactoringStatus initializeStatus= initialize(arguments);
88                 status.merge(initializeStatus);
89         }
90
91         public boolean canChildrenBeDestinations(IReorgDestination destination) {
92                 return fMovePolicy.canChildrenBeDestinations(destination);
93         }
94
95         public boolean canElementBeDestination(IReorgDestination destination) {
96                 return fMovePolicy.canElementBeDestination(destination);
97         }
98
99         public boolean canEnableQualifiedNameUpdating() {
100                 return fMovePolicy.canEnableQualifiedNameUpdating();
101         }
102
103         public boolean canUpdateQualifiedNames() {
104                 return fMovePolicy.canUpdateQualifiedNames();
105         }
106
107         /**
108          * Checks if <b>Java</b> references to the selected element(s) can be updated if moved to
109          * the selected destination. Even if <code>false</code>, participants could still update
110          * non-Java references.
111          * 
112          * @return <code>true</code> iff <b>Java</b> references to the moved element can be updated
113          * @since 3.5
114          */
115         public boolean canUpdateJavaReferences() {
116                 return fMovePolicy.canUpdateJavaReferences();
117         }
118
119         /**
120          * DO NOT REMOVE, used in a product, see https://bugs.eclipse.org/299631 .
121          * @return <code>true</code> iff <b>Java</b> references to the moved element can be updated
122          * @deprecated since 3.5, replaced by {@link #canUpdateJavaReferences()}
123          */
124         public boolean canUpdateReferences() {
125                 return canUpdateJavaReferences();
126         }
127         
128         @Override
129         public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException {
130                 try {
131                         Assert.isNotNull(fReorgQueries);
132                         fWasCanceled= false;
133                         return fMovePolicy.checkFinalConditions(pm, context, fReorgQueries);
134                 } catch (OperationCanceledException e) {
135                         fWasCanceled= true;
136                         throw e;
137                 }
138         }
139
140         @Override
141         public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
142                 pm.beginTask("", 1); //$NON-NLS-1$
143                 try {
144                         RefactoringStatus result= new RefactoringStatus();
145                         result.merge(RefactoringStatus.create(Resources.checkInSync(ReorgUtils.getNotNulls(fMovePolicy.getResources()))));
146                         IResource[] javaResources= ReorgUtils.getResources(fMovePolicy.getJavaElements());
147                         result.merge(RefactoringStatus.create(Resources.checkInSync(ReorgUtils.getNotNulls(javaResources))));
148                         return result;
149                 } finally {
150                         pm.done();
151                 }
152         }
153
154         @Override
155         public Change createChange(IProgressMonitor pm) throws CoreException {
156                 Assert.isTrue(fMovePolicy.getJavaElementDestination() == null || fMovePolicy.getResourceDestination() == null);
157                 Assert.isTrue(fMovePolicy.getJavaElementDestination() != null || fMovePolicy.getResourceDestination() != null);
158                 try {
159                         final DynamicValidationStateChange result= new DynamicValidationStateChange(RefactoringCoreMessages.JavaMoveProcessor_change_name) {
160
161                                 @Override
162                                 public ChangeDescriptor getDescriptor() {
163                                         return fMovePolicy.getDescriptor();
164                                 }
165
166                                 @Override
167                                 public Change perform(IProgressMonitor pm2) throws CoreException {
168                                         Change change= super.perform(pm2);
169                                         Change[] changes= getChildren();
170                                         for (int index= 0; index < changes.length; index++) {
171                                                 if (!(changes[index] instanceof TextEditBasedChange))
172                                                         return null;
173                                         }
174                                         return change;
175                                 }
176                         };
177                         CreateTargetExecutionLog log= null;
178                         return result.generated_8597765922631664186(pm, this, log);
179                 } finally {
180                         pm.done();
181                 }
182         }
183
184         private String[] getAffectedProjectNatures() throws CoreException {
185                 String[] jNatures= JavaProcessors.computeAffectedNaturs(fMovePolicy.getJavaElements());
186                 String[] rNatures= ResourceProcessors.computeAffectedNatures(fMovePolicy.getResources());
187                 Set<String> result= new HashSet<String>();
188                 result.addAll(Arrays.asList(jNatures));
189                 result.addAll(Arrays.asList(rNatures));
190                 return result.toArray(new String[result.size()]);
191         }
192
193         public Object getCommonParentForInputElements() {
194                 return new ParentChecker(fMovePolicy.getResources(), fMovePolicy.getJavaElements()).getCommonParent();
195         }
196
197         public ICreateTargetQuery getCreateTargetQuery() {
198                 return fMovePolicy.getCreateTargetQuery(fCreateTargetQueries);
199         }
200
201         protected Object getDestination() {
202                 IJavaElement je= fMovePolicy.getJavaElementDestination();
203                 if (je != null)
204                         return je;
205                 return fMovePolicy.getResourceDestination();
206         }
207
208         @Override
209         public Object[] getElements() {
210                 List<IAdaptable> result= new ArrayList<IAdaptable>();
211                 result.addAll(Arrays.asList(fMovePolicy.getJavaElements()));
212                 result.addAll(Arrays.asList(fMovePolicy.getResources()));
213                 return result.toArray();
214         }
215
216         public String getFilePatterns() {
217                 return fMovePolicy.getFilePatterns();
218         }
219
220         @Override
221         public String getIdentifier() {
222                 return IRefactoringProcessorIds.MOVE_PROCESSOR;
223         }
224
225         public IJavaElement[] getJavaElements() {
226                 return fMovePolicy.getJavaElements();
227         }
228
229         @Override
230         public String getProcessorName() {
231                 return RefactoringCoreMessages.MoveRefactoring_0;
232         }
233
234         public IResource[] getResources() {
235                 return fMovePolicy.getResources();
236         }
237
238         public boolean getUpdateQualifiedNames() {
239                 return fMovePolicy.getUpdateQualifiedNames();
240         }
241
242         public boolean getUpdateReferences() {
243                 return fMovePolicy.getUpdateReferences();
244         }
245
246         public boolean hasAllInputSet() {
247                 return fMovePolicy.hasAllInputSet();
248         }
249
250         public boolean hasDestinationSet() {
251                 return fMovePolicy.getJavaElementDestination() != null || fMovePolicy.getResourceDestination() != null;
252         }
253
254         private RefactoringStatus initialize(JavaRefactoringArguments arguments) {
255                 setReorgQueries(new NullReorgQueries());
256                 final RefactoringStatus status= new RefactoringStatus();
257                 arguments.generated_3618032577734140300(this, status);
258                 return status;
259         }
260
261         @Override
262         public boolean isApplicable() throws CoreException {
263                 return fMovePolicy.canEnable();
264         }
265
266         public boolean isTextualMove() {
267                 return fMovePolicy.isTextualMove();
268         }
269
270         @Override
271         public RefactoringParticipant[] loadParticipants(RefactoringStatus status, SharableParticipants shared) throws CoreException {
272                 return fMovePolicy.loadParticipants(status, this, getAffectedProjectNatures(), shared);
273         }
274
275         @Override
276         public Change postCreateChange(Change[] participantChanges, IProgressMonitor pm) throws CoreException {
277                 return fMovePolicy.postCreateChange(participantChanges, pm);
278         }
279
280         public void setCreateTargetQueries(ICreateTargetQueries queries) {
281                 Assert.isNotNull(queries);
282                 fCreateTargetQueries= new MonitoringCreateTargetQueries(queries, fMovePolicy.getCreateTargetExecutionLog());
283         }
284
285         public RefactoringStatus setDestination(IReorgDestination destination) throws JavaModelException {
286                 fMovePolicy.setDestination(destination);
287                 return fMovePolicy.verifyDestination(destination);
288         }
289
290         public void setFilePatterns(String patterns) {
291                 fMovePolicy.setFilePatterns(patterns);
292         }
293
294         public void setReorgQueries(IReorgQueries queries) {
295                 Assert.isNotNull(queries);
296                 fReorgQueries= queries;
297         }
298
299         public void setUpdateQualifiedNames(boolean update) {
300                 fMovePolicy.setUpdateQualifiedNames(update);
301         }
302
303         public void setUpdateReferences(boolean update) {
304                 fMovePolicy.setUpdateReferences(update);
305         }
306
307         public boolean wasCanceled() {
308                 return fWasCanceled;
309         }
310
311         public int getSaveMode() {
312                 return fMovePolicy.getSaveMode();
313         }
314
315         public void generated_6830952479925279332(final MoveInputPage moveinputpage, String text) {
316                 moveinputpage.fReferenceCheckbox.setText(text);
317                 moveinputpage.fReferenceCheckbox.setSelection(getUpdateReferences());
318         
319                 moveinputpage.fReferenceCheckbox.addSelectionListener(new SelectionAdapter() {
320                         @Override
321                         public void widgetSelected(SelectionEvent e) {
322                                 setUpdateReferences(((Button)e.widget).getSelection());
323                                 moveinputpage.updateUIStatus();
324                         }
325                 });
326         }
327
328         public void generated_2153474229328148215(MoveInputPage moveinputpage) {
329                 moveinputpage.updateQualifiedNameUpdating(this, getUpdateQualifiedNames());
330         }
331
332         public boolean generated_841604632702829879(Shell parent) throws InterruptedException, InvocationTargetException {
333                 Refactoring ref= new MoveRefactoring(this);
334                 if (hasAllInputSet()) {
335                         IRunnableContext context= PlatformUI.getWorkbench().getActiveWorkbenchWindow();
336                         setCreateTargetQueries(new CreateTargetQueries(parent));
337                         setReorgQueries(new ReorgQueries(parent));
338                         new RefactoringExecutionHelper(ref, RefactoringCore.getConditionCheckingFailedSeverity(), getSaveMode(), parent, context).perform(false, false);
339                         return true;
340                 } else {
341                         RefactoringWizard wizard= new ReorgMoveWizard(this, ref);
342                         /*
343                          * We want to get the shell from the refactoring dialog but it's not known at this point,
344                          * so we pass the wizard and then, once the dialog is open, we will have access to its shell.
345                          */
346                         setCreateTargetQueries(new CreateTargetQueries(wizard));
347                         setReorgQueries(new ReorgQueries(wizard));
348                         return new RefactoringStarter().activate(wizard, parent, RefactoringMessages.OpenRefactoringWizardAction_refactoring, getSaveMode());
349                 }
350         }
351 }