1 11 package org.eclipse.jdt.internal.ui.util; 12 13 import java.lang.reflect.InvocationTargetException ; 14 15 import org.eclipse.swt.custom.BusyIndicator; 16 17 import org.eclipse.core.runtime.NullProgressMonitor; 18 import org.eclipse.core.runtime.OperationCanceledException; 19 20 import org.eclipse.jface.operation.IRunnableContext; 21 import org.eclipse.jface.operation.IRunnableWithProgress; 22 import org.eclipse.jface.operation.ModalContext; 23 24 31 public class BusyIndicatorRunnableContext implements IRunnableContext { 32 33 private static class BusyRunnable implements Runnable { 34 35 private static class ThreadContext extends Thread { 36 IRunnableWithProgress fRunnable; 37 Throwable fThrowable; 38 39 public ThreadContext(IRunnableWithProgress runnable) { 40 this(runnable, "BusyCursorRunnableContext-Thread"); } 42 protected ThreadContext(IRunnableWithProgress runnable, String name) { 43 super(name); 44 fRunnable= runnable; 45 } 46 public void run() { 47 try { 48 fRunnable.run(new NullProgressMonitor()); 49 } catch (InvocationTargetException e) { 50 fThrowable= e; 51 } catch (InterruptedException e) { 52 fThrowable= e; 53 } catch (ThreadDeath e) { 54 fThrowable= e; 55 throw e; 56 } catch (RuntimeException e) { 57 fThrowable= e; 58 } catch (Error e) { 59 fThrowable= e; 60 } 61 } 62 void sync() { 63 try { 64 join(); 65 } catch (InterruptedException e) { 66 } 68 } 69 } 70 71 public Throwable fThrowable; 72 private boolean fFork; 73 private IRunnableWithProgress fRunnable; 74 public BusyRunnable(boolean fork, IRunnableWithProgress runnable) { 75 fFork= fork; 76 fRunnable= runnable; 77 } 78 public void run() { 79 try { 80 internalRun(fFork, fRunnable); 81 } catch (InvocationTargetException e) { 82 fThrowable= e; 83 } catch (InterruptedException e) { 84 fThrowable= e; 85 } 86 } 87 private void internalRun(boolean fork, final IRunnableWithProgress runnable) throws InvocationTargetException , InterruptedException { 88 Thread thread= Thread.currentThread(); 89 if (thread instanceof ThreadContext || ModalContext.isModalContextThread(thread)) 92 fork= false; 93 94 if (fork) { 95 final ThreadContext t= new ThreadContext(runnable); 96 t.start(); 97 t.sync(); 98 Throwable throwable= t.fThrowable; 100 if (throwable != null) { 101 if (throwable instanceof InvocationTargetException ) { 102 throw (InvocationTargetException ) throwable; 103 } else if (throwable instanceof InterruptedException ) { 104 throw (InterruptedException ) throwable; 105 } else if (throwable instanceof OperationCanceledException) { 106 throw new InterruptedException (); 107 } else { 108 throw new InvocationTargetException (throwable); 109 } 110 } 111 } else { 112 try { 113 runnable.run(new NullProgressMonitor()); 114 } catch (OperationCanceledException e) { 115 throw new InterruptedException (); 116 } 117 } 118 } 119 } 120 121 124 public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable) throws InvocationTargetException , InterruptedException { 125 BusyRunnable busyRunnable= new BusyRunnable(fork, runnable); 126 BusyIndicator.showWhile(null, busyRunnable); 127 Throwable throwable= busyRunnable.fThrowable; 128 if (throwable instanceof InvocationTargetException ) { 129 throw (InvocationTargetException )throwable; 130 } else if (throwable instanceof InterruptedException ) { 131 throw (InterruptedException )throwable; 132 } 133 } 134 } 135 | Popular Tags |