1 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 [] exception= new Throwable [1]; 54 final ISchedulingRule rule= ResourcesPlugin.getWorkspace().getRoot(); 55 final Thread 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 e) { 79 exception[0]= e; 80 } 81 }; 82 Runnable r= new Runnable () { 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 throw new CoreException(new MultiStatus( 96 RefactoringUIPlugin.getPluginId(), IStatus.ERROR, 97 new IStatus[] {status}, status.getMessage(), exception[0])); 98 } else { 99 String 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 |