KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > ui > refactoring > RefactoringWizardOpenOperation


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.ltk.ui.refactoring;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.OperationCanceledException;
17 import org.eclipse.core.runtime.jobs.IJobManager;
18 import org.eclipse.core.runtime.jobs.Job;
19
20 import org.eclipse.core.resources.ResourcesPlugin;
21
22 import org.eclipse.ltk.core.refactoring.CheckConditionsOperation;
23 import org.eclipse.ltk.core.refactoring.Refactoring;
24 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
25
26 import org.eclipse.ltk.internal.ui.refactoring.ExceptionHandler;
27 import org.eclipse.ltk.internal.ui.refactoring.RefactoringUIMessages;
28 import org.eclipse.ltk.internal.ui.refactoring.WorkbenchRunnableAdapter;
29
30 import org.eclipse.swt.custom.BusyIndicator;
31 import org.eclipse.swt.widgets.Shell;
32
33 import org.eclipse.jface.dialogs.Dialog;
34 import org.eclipse.jface.dialogs.IDialogConstants;
35 import org.eclipse.jface.dialogs.MessageDialog;
36 import org.eclipse.jface.window.Window;
37 import org.eclipse.jface.wizard.IWizardContainer;
38
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.progress.IProgressService;
41
42 /**
43  * A helper class to open a refactoring wizard dialog. The class first checks
44  * the initial conditions of the refactoring and depending on its outcome
45  * the wizard dialog or an error dialog is shown.
46  * <p>
47  * Note: this class is not intended to be extended by clients.
48  * </p>
49  *
50  * @since 3.0
51  */

52 public class RefactoringWizardOpenOperation {
53
54     private RefactoringWizard fWizard;
55     private RefactoringStatus fInitialConditions;
56     
57     /**
58      * Constant (value 1025) indicating that the precondition check failed
59      * when opening a refactoring wizard dialog.
60      *
61      * @see #run(Shell, String)
62      */

63     public static final int INITIAL_CONDITION_CHECKING_FAILED= IDialogConstants.CLIENT_ID + 1;
64     
65     /**
66      * Creates a new refactoring wizard starter for the given wizard.
67      *
68      * @param wizard the wizard to open a dialog for
69      */

70     public RefactoringWizardOpenOperation(RefactoringWizard wizard) {
71         Assert.isNotNull(wizard);
72         fWizard= wizard;
73     }
74     
75     /**
76      * Returns the outcome of the initial condition checking.
77      *
78      * @return the outcome of the initial condition checking or <code>null</code>
79      * if the condition checking hasn't been performed yet
80      */

81     public RefactoringStatus getInitialConditionCheckingStatus() {
82         return fInitialConditions;
83     }
84     
85     /**
86      * Opens the refactoring dialog for the refactoring wizard passed to the constructor.
87      * The method first checks the initial conditions of the refactoring. If the condition
88      * checking returns a status with a severity of {@link RefactoringStatus#FATAL} then
89      * a message dialog is opened containing the corresponding status message. No wizard
90      * dialog is opened in this situation. If the condition checking passes then the
91      * refactoring dialog is opened.
92      * <p>
93      * The methods ensures that the workspace lock is held while the condition checking,
94      * change creation and change execution is performed. Clients can't make any assumption
95      * about the thread in which these steps are executed. However the framework ensures
96      * that the workspace lock is transfered to the thread in which the execution of the
97      * steps takes place.
98      * </p>
99      * @param parent the parent shell for the dialog or <code>null</code> if the dialog
100      * is a top level dialog
101      * @param dialogTitle the dialog title of the message box presenting the failed
102      * condition check (if any)
103      *
104      * @return {@link #INITIAL_CONDITION_CHECKING_FAILED} if the initial condition checking
105      * failed and no wizard dialog was presented. Otherwise either {@link IDialogConstants#OK_ID}
106      * or {@link IDialogConstants#CANCEL_ID} is returned depending on whether the user
107      * has pressed the OK or cancel button on the wizard dialog.
108      *
109      * @throws InterruptedException if the initial condition checking got canceled by
110      * the user.
111      */

112     public int run(final Shell parent, final String JavaDoc dialogTitle) throws InterruptedException JavaDoc {
113         Assert.isNotNull(dialogTitle);
114         final Refactoring refactoring= fWizard.getRefactoring();
115         final IJobManager manager= Job.getJobManager();
116         final int[] result= new int[1];
117         final InterruptedException JavaDoc[] canceled= new InterruptedException JavaDoc[1];
118         Runnable JavaDoc r= new Runnable JavaDoc() {
119             public void run() {
120                 try {
121                     // we are getting the block dialog for free if we pass in null
122
manager.beginRule(ResourcesPlugin.getWorkspace().getRoot(), null);
123                     
124                     refactoring.setValidationContext(parent);
125                     fInitialConditions= checkInitialConditions(refactoring, parent, dialogTitle);
126                     if (fInitialConditions.hasFatalError()) {
127                         String JavaDoc message= fInitialConditions.getMessageMatchingSeverity(RefactoringStatus.FATAL);
128                         MessageDialog.openInformation(parent, dialogTitle, message);
129                         result[0]= INITIAL_CONDITION_CHECKING_FAILED;
130                     } else {
131                         fWizard.setInitialConditionCheckingStatus(fInitialConditions);
132                         Dialog dialog= RefactoringUI.createRefactoringWizardDialog(fWizard, parent);
133                         dialog.create();
134                         IWizardContainer wizardContainer= (IWizardContainer) dialog;
135                         if (wizardContainer.getCurrentPage() == null)
136                             /*
137                              * Don't show the dialog at all if there are no user
138                              * input pages and change creation was cancelled.
139                              */

140                             result[0]= Window.CANCEL;
141                         else
142                             result[0]= dialog.open();
143                     }
144                 } catch (InterruptedException JavaDoc e) {
145                     canceled[0]= e;
146                 } catch (OperationCanceledException e) {
147                     canceled[0]= new InterruptedException JavaDoc(e.getMessage());
148                 } finally {
149                     manager.endRule(ResourcesPlugin.getWorkspace().getRoot());
150                     refactoring.setValidationContext(null);
151                 }
152             }
153         };
154         BusyIndicator.showWhile(parent.getDisplay(), r);
155         if (canceled[0] != null)
156             throw canceled[0];
157         return result[0];
158     }
159     
160     //---- private helper methods -----------------------------------------------------------------
161

162     private RefactoringStatus checkInitialConditions(Refactoring refactoring, Shell parent, String JavaDoc title) throws InterruptedException JavaDoc {
163         try {
164             CheckConditionsOperation cco= new CheckConditionsOperation(refactoring, CheckConditionsOperation.INITIAL_CONDITONS);
165             IProgressService service= PlatformUI.getWorkbench().getProgressService();
166             service.busyCursorWhile(new WorkbenchRunnableAdapter(cco, ResourcesPlugin.getWorkspace().getRoot()));
167             return cco.getStatus();
168         } catch (InvocationTargetException JavaDoc e) {
169             ExceptionHandler.handle(e, parent, title,
170                 RefactoringUIMessages.RefactoringUI_open_unexpected_exception);
171             return RefactoringStatus.createFatalErrorStatus(
172                 RefactoringUIMessages.RefactoringUI_open_unexpected_exception);
173         }
174     }
175 }
176
Popular Tags