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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.refactoring.reorg;
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;
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;
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;
31 import org.eclipse.core.resources.IResource;
33 import org.eclipse.jface.operation.IRunnableContext;
35 import org.eclipse.ui.PlatformUI;
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;
50 import org.eclipse.jdt.core.IJavaElement;
51 import org.eclipse.jdt.core.JavaModelException;
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;
62 import org.eclipse.jdt.ui.refactoring.IRefactoringProcessorIds;
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;
72 public final class JavaMoveProcessor extends MoveProcessor implements IQualifiedNameUpdating, IReorgDestinationValidator {
74 public ICreateTargetQueries fCreateTargetQueries;
76 public IMovePolicy fMovePolicy;
78 private IReorgQueries fReorgQueries;
80 private boolean fWasCanceled;
82 public JavaMoveProcessor(IMovePolicy policy) {
86 public JavaMoveProcessor(JavaRefactoringArguments arguments, RefactoringStatus status) {
87 RefactoringStatus initializeStatus= initialize(arguments);
88 status.merge(initializeStatus);
91 public boolean canChildrenBeDestinations(IReorgDestination destination) {
92 return fMovePolicy.canChildrenBeDestinations(destination);
95 public boolean canElementBeDestination(IReorgDestination destination) {
96 return fMovePolicy.canElementBeDestination(destination);
99 public boolean canEnableQualifiedNameUpdating() {
100 return fMovePolicy.canEnableQualifiedNameUpdating();
103 public boolean canUpdateQualifiedNames() {
104 return fMovePolicy.canUpdateQualifiedNames();
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.
112 * @return <code>true</code> iff <b>Java</b> references to the moved element can be updated
115 public boolean canUpdateJavaReferences() {
116 return fMovePolicy.canUpdateJavaReferences();
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()}
124 public boolean canUpdateReferences() {
125 return canUpdateJavaReferences();
129 public RefactoringStatus checkFinalConditions(IProgressMonitor pm, CheckConditionsContext context) throws CoreException {
131 Assert.isNotNull(fReorgQueries);
133 return fMovePolicy.checkFinalConditions(pm, context, fReorgQueries);
134 } catch (OperationCanceledException e) {
141 public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
142 pm.beginTask("", 1); //$NON-NLS-1$
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))));
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);
159 final DynamicValidationStateChange result= new DynamicValidationStateChange(RefactoringCoreMessages.JavaMoveProcessor_change_name) {
162 public ChangeDescriptor getDescriptor() {
163 return fMovePolicy.getDescriptor();
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))
177 CreateTargetExecutionLog log= null;
178 return result.generated_8597765922631664186(pm, this, log);
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()]);
193 public Object getCommonParentForInputElements() {
194 return new ParentChecker(fMovePolicy.getResources(), fMovePolicy.getJavaElements()).getCommonParent();
197 public ICreateTargetQuery getCreateTargetQuery() {
198 return fMovePolicy.getCreateTargetQuery(fCreateTargetQueries);
201 protected Object getDestination() {
202 IJavaElement je= fMovePolicy.getJavaElementDestination();
205 return fMovePolicy.getResourceDestination();
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();
216 public String getFilePatterns() {
217 return fMovePolicy.getFilePatterns();
221 public String getIdentifier() {
222 return IRefactoringProcessorIds.MOVE_PROCESSOR;
225 public IJavaElement[] getJavaElements() {
226 return fMovePolicy.getJavaElements();
230 public String getProcessorName() {
231 return RefactoringCoreMessages.MoveRefactoring_0;
234 public IResource[] getResources() {
235 return fMovePolicy.getResources();
238 public boolean getUpdateQualifiedNames() {
239 return fMovePolicy.getUpdateQualifiedNames();
242 public boolean getUpdateReferences() {
243 return fMovePolicy.getUpdateReferences();
246 public boolean hasAllInputSet() {
247 return fMovePolicy.hasAllInputSet();
250 public boolean hasDestinationSet() {
251 return fMovePolicy.getJavaElementDestination() != null || fMovePolicy.getResourceDestination() != null;
254 private RefactoringStatus initialize(JavaRefactoringArguments arguments) {
255 setReorgQueries(new NullReorgQueries());
256 final RefactoringStatus status= new RefactoringStatus();
257 arguments.generated_3618032577734140300(this, status);
262 public boolean isApplicable() throws CoreException {
263 return fMovePolicy.canEnable();
266 public boolean isTextualMove() {
267 return fMovePolicy.isTextualMove();
271 public RefactoringParticipant[] loadParticipants(RefactoringStatus status, SharableParticipants shared) throws CoreException {
272 return fMovePolicy.loadParticipants(status, this, getAffectedProjectNatures(), shared);
276 public Change postCreateChange(Change[] participantChanges, IProgressMonitor pm) throws CoreException {
277 return fMovePolicy.postCreateChange(participantChanges, pm);
280 public void setCreateTargetQueries(ICreateTargetQueries queries) {
281 Assert.isNotNull(queries);
282 fCreateTargetQueries= new MonitoringCreateTargetQueries(queries, fMovePolicy.getCreateTargetExecutionLog());
285 public RefactoringStatus setDestination(IReorgDestination destination) throws JavaModelException {
286 fMovePolicy.setDestination(destination);
287 return fMovePolicy.verifyDestination(destination);
290 public void setFilePatterns(String patterns) {
291 fMovePolicy.setFilePatterns(patterns);
294 public void setReorgQueries(IReorgQueries queries) {
295 Assert.isNotNull(queries);
296 fReorgQueries= queries;
299 public void setUpdateQualifiedNames(boolean update) {
300 fMovePolicy.setUpdateQualifiedNames(update);
303 public void setUpdateReferences(boolean update) {
304 fMovePolicy.setUpdateReferences(update);
307 public boolean wasCanceled() {
311 public int getSaveMode() {
312 return fMovePolicy.getSaveMode();
315 public void generated_6830952479925279332(final MoveInputPage moveinputpage, String text) {
316 moveinputpage.fReferenceCheckbox.setText(text);
317 moveinputpage.fReferenceCheckbox.setSelection(getUpdateReferences());
319 moveinputpage.fReferenceCheckbox.addSelectionListener(new SelectionAdapter() {
321 public void widgetSelected(SelectionEvent e) {
322 setUpdateReferences(((Button)e.widget).getSelection());
323 moveinputpage.updateUIStatus();
328 public void generated_2153474229328148215(MoveInputPage moveinputpage) {
329 moveinputpage.updateQualifiedNameUpdating(this, getUpdateQualifiedNames());
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);
341 RefactoringWizard wizard= new ReorgMoveWizard(this, ref);
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.
346 setCreateTargetQueries(new CreateTargetQueries(wizard));
347 setReorgQueries(new ReorgQueries(wizard));
348 return new RefactoringStarter().activate(wizard, parent, RefactoringMessages.OpenRefactoringWizardAction_refactoring, getSaveMode());