KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > UIPerformChangeOperation


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.internal.ui.refactoring;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.ISafeRunnable;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.MultiStatus;
18 import org.eclipse.core.runtime.SafeRunner;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.core.runtime.jobs.ISchedulingRule;
21 import org.eclipse.core.runtime.jobs.Job;
22
23 import org.eclipse.core.resources.ResourcesPlugin;
24
25 import org.eclipse.ltk.core.refactoring.Change;
26 import org.eclipse.ltk.core.refactoring.CreateChangeOperation;
27 import org.eclipse.ltk.core.refactoring.PerformChangeOperation;
28
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Display;
31
32 import org.eclipse.jface.wizard.IWizardContainer;
33
34 public class UIPerformChangeOperation extends PerformChangeOperation {
35
36     private Display fDisplay;
37     private IWizardContainer fWizardContainer;
38     
39     public UIPerformChangeOperation(Display display, Change change, IWizardContainer container) {
40         super(change);
41         fDisplay= display;
42         fWizardContainer= container;
43     }
44
45     public UIPerformChangeOperation(Display display, CreateChangeOperation op, IWizardContainer container) {
46         super(op);
47         fDisplay= display;
48         fWizardContainer= container;
49     }
50     
51     protected void executeChange(final IProgressMonitor pm) throws CoreException {
52         if (fDisplay != null && !fDisplay.isDisposed()) {
53             final Throwable JavaDoc[] exception= new Throwable JavaDoc[1];
54             final ISchedulingRule rule= ResourcesPlugin.getWorkspace().getRoot();
55             final Thread JavaDoc callerThread= Thread.currentThread();
56             final ISafeRunnable safeRunnable= new ISafeRunnable() {
57                 public void run() {
58                     try {
59                         final Button cancel= getCancelButton();
60                         boolean enabled= true;
61                         if (cancel != null && !cancel.isDisposed()) {
62                             enabled= cancel.isEnabled();
63                             cancel.setEnabled(false);
64                         }
65                         try {
66                             UIPerformChangeOperation.super.executeChange(pm);
67                         } finally {
68                             if (cancel != null && !cancel.isDisposed()) {
69                                 cancel.setEnabled(enabled);
70                             }
71                         }
72                     } catch(CoreException e) {
73                         exception[0]= e;
74                     } finally {
75                         Job.getJobManager().transferRule(rule, callerThread);
76                     }
77                 }
78                 public void handleException(Throwable JavaDoc e) {
79                     exception[0]= e;
80                 }
81             };
82             Runnable JavaDoc r= new Runnable JavaDoc() {
83                 public void run() {
84                     SafeRunner.run(safeRunnable);
85                 }
86             };
87             Job.getJobManager().transferRule(rule, fDisplay.getThread());
88             fDisplay.syncExec(r);
89             if (exception[0] != null) {
90                 if (exception[0] instanceof CoreException) {
91                     IStatus status= ((CoreException)exception[0]).getStatus();
92                     // it is more important to get the original cause of the
93
// exception. Therefore create a new status and take
94
// over the exception trace from the UI thread.
95
throw new CoreException(new MultiStatus(
96                             RefactoringUIPlugin.getPluginId(), IStatus.ERROR,
97                             new IStatus[] {status}, status.getMessage(), exception[0]));
98                 } else {
99                     String JavaDoc message= exception[0].getMessage();
100                     throw new CoreException(new Status(
101                         IStatus.ERROR, RefactoringUIPlugin.getPluginId(),IStatus.ERROR,
102                         message == null
103                             ? RefactoringUIMessages.ChangeExceptionHandler_no_details
104                             : message,
105                         exception[0]));
106                 }
107             }
108         } else {
109             super.executeChange(pm);
110         }
111     }
112
113     private Button getCancelButton() {
114         if (fWizardContainer instanceof RefactoringWizardDialog2) {
115             return ((RefactoringWizardDialog2)fWizardContainer).getCancelButton();
116         } else if (fWizardContainer instanceof RefactoringWizardDialog) {
117             return ((RefactoringWizardDialog)fWizardContainer).getCancelButton();
118         }
119         return null;
120     }
121 }
122
Popular Tags