1 11 package org.eclipse.team.internal.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.core.resources.IWorkspaceRunnable; 16 import org.eclipse.core.resources.ResourcesPlugin; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.jobs.ISchedulingRule; 20 import org.eclipse.jface.operation.IRunnableContext; 21 import org.eclipse.jface.operation.IRunnableWithProgress; 22 import org.eclipse.ui.PlatformUI; 23 import org.eclipse.ui.progress.IProgressService; 24 25 29 public class ProgressDialogRunnableContext implements ITeamRunnableContext { 30 31 private IRunnableContext runnableContext; 32 private ISchedulingRule schedulingRule; 33 private boolean postponeBuild; 34 35 public ProgressDialogRunnableContext() { 36 } 37 38 43 public void setPostponeBuild(boolean postponeBuild) { 44 this.postponeBuild = postponeBuild; 45 } 46 47 52 public void setSchedulingRule(ISchedulingRule schedulingRule) { 53 this.schedulingRule = schedulingRule; 54 } 55 56 61 public void setRunnableContext(IRunnableContext runnableContext) { 62 this.runnableContext = runnableContext; 63 } 64 65 68 public void run(IRunnableWithProgress runnable) throws InvocationTargetException , InterruptedException { 69 getRunnableContext().run(true , true , wrapRunnable(runnable)); 70 } 71 72 private IRunnableContext getRunnableContext() { 73 if (runnableContext == null) { 74 return new IRunnableContext() { 75 public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable) 76 throws InvocationTargetException , InterruptedException { 77 IProgressService manager = PlatformUI.getWorkbench().getProgressService(); 78 manager.busyCursorWhile(runnable); 79 } 80 }; 81 } 82 return runnableContext; 83 } 84 85 89 private IRunnableWithProgress wrapRunnable(final IRunnableWithProgress runnable) { 90 return new IRunnableWithProgress() { 91 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 92 try { 93 if (schedulingRule == null && !postponeBuild) { 94 runnable.run(monitor); 95 } else { 96 final Exception [] exception = new Exception [] { null }; 97 ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() { 98 public void run(IProgressMonitor pm) throws CoreException { 99 try { 100 runnable.run(pm); 101 } catch (InvocationTargetException e) { 102 exception[0] = e; 103 } catch (InterruptedException e) { 104 exception[0] = e; 105 } 106 } 107 }, schedulingRule, 0 , monitor); 108 if (exception[0] != null) { 109 if (exception[0] instanceof InvocationTargetException ) { 110 throw (InvocationTargetException )exception[0]; 111 } else if (exception[0] instanceof InterruptedException ) { 112 throw (InterruptedException )exception[0]; 113 } 114 } 115 } 116 } catch (CoreException e) { 117 throw new InvocationTargetException (e); 118 } 119 } 120 }; 121 } 122 123 } 124 | Popular Tags |