KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > reorg > ReorgMoveStarter


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.ui.refactoring.reorg;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16
17 import org.eclipse.core.resources.IResource;
18
19 import org.eclipse.swt.widgets.Shell;
20
21 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
22 import org.eclipse.jface.operation.IRunnableContext;
23
24 import org.eclipse.ltk.core.refactoring.RefactoringCore;
25 import org.eclipse.ltk.ui.refactoring.RefactoringWizard;
26
27 import org.eclipse.jdt.core.IJavaElement;
28 import org.eclipse.jdt.core.JavaModelException;
29
30 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaMoveProcessor;
31 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgPolicyFactory;
32 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.IMovePolicy;
33 import org.eclipse.jdt.internal.corext.refactoring.structure.JavaMoveRefactoring;
34
35 import org.eclipse.jdt.internal.ui.refactoring.RefactoringExecutionHelper;
36 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
37 import org.eclipse.jdt.internal.ui.refactoring.RefactoringSaveHelper;
38 import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringStarter;
39 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
40
41
42 public class ReorgMoveStarter {
43     private final JavaMoveProcessor fMoveProcessor;
44
45     private ReorgMoveStarter(JavaMoveProcessor moveProcessor) {
46         Assert.isNotNull(moveProcessor);
47         fMoveProcessor= moveProcessor;
48     }
49     
50     public static ReorgMoveStarter create(IJavaElement[] javaElements, IResource[] resources, IJavaElement destination) throws JavaModelException {
51         Assert.isNotNull(javaElements);
52         Assert.isNotNull(resources);
53         Assert.isNotNull(destination);
54         IMovePolicy policy= ReorgPolicyFactory.createMovePolicy(resources, javaElements);
55         if (!policy.canEnable())
56             return null;
57         JavaMoveProcessor processor= new JavaMoveProcessor(policy);
58         if (! processor.setDestination(destination).isOK())
59             return null;
60         return new ReorgMoveStarter(processor);
61     }
62
63     public static ReorgMoveStarter create(IJavaElement[] javaElements, IResource[] resources, IResource destination) throws JavaModelException {
64         Assert.isNotNull(javaElements);
65         Assert.isNotNull(resources);
66         Assert.isNotNull(destination);
67         IMovePolicy policy= ReorgPolicyFactory.createMovePolicy(resources, javaElements);
68         if (!policy.canEnable())
69             return null;
70         JavaMoveProcessor processor= new JavaMoveProcessor(policy);
71         if (! processor.setDestination(destination).isOK())
72             return null;
73         return new ReorgMoveStarter(processor);
74     }
75     
76     public void run(Shell parent) throws InterruptedException JavaDoc, InvocationTargetException JavaDoc {
77         try {
78             JavaMoveRefactoring ref= new JavaMoveRefactoring(fMoveProcessor);
79             if (fMoveProcessor.hasAllInputSet()) {
80                 IRunnableContext context= new ProgressMonitorDialog(parent);
81                 fMoveProcessor.setCreateTargetQueries(new CreateTargetQueries(parent));
82                 fMoveProcessor.setReorgQueries(new ReorgQueries(parent));
83                 new RefactoringExecutionHelper(ref, RefactoringCore.getConditionCheckingFailedSeverity(), RefactoringSaveHelper.SAVE_ALL, parent, context).perform(false, false);
84             } else {
85                 RefactoringWizard wizard= new ReorgMoveWizard(ref);
86                 /*
87                  * We want to get the shell from the refactoring dialog but it's not known at this point,
88                  * so we pass the wizard and then, once the dialog is open, we will have access to its shell.
89                  */

90                 fMoveProcessor.setCreateTargetQueries(new CreateTargetQueries(wizard));
91                 fMoveProcessor.setReorgQueries(new ReorgQueries(wizard));
92                 new RefactoringStarter().activate(ref, wizard, parent, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringSaveHelper.SAVE_ALL);
93             }
94         } catch (JavaModelException e) {
95             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
96         }
97     }
98 }
99
Popular Tags