1 11 package org.eclipse.ui.internal; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.swt.widgets.Display; 18 import org.eclipse.swt.widgets.Synchronizer; 19 import org.eclipse.ui.internal.StartupThreading.StartupRunnable; 20 21 public class UISynchronizer extends Synchronizer { 22 protected UILockListener lockListener; 23 24 28 protected boolean isStarting = true; 29 30 33 protected List pendingStartup = new ArrayList (); 34 35 39 public static final ThreadLocal startupThread = new ThreadLocal () { 40 41 46 protected Object initialValue() { 47 return Boolean.FALSE; 48 } 49 50 53 public void set(Object value) { 54 if (value != Boolean.TRUE && value != Boolean.FALSE) 55 throw new IllegalArgumentException (); 56 super.set(value); 57 } 58 }; 59 60 public UISynchronizer(Display display, UILockListener lock) { 61 super(display); 62 this.lockListener = lock; 63 } 64 65 public void started() { 66 synchronized (this) { 67 if (!isStarting) 68 throw new IllegalStateException (); 69 isStarting = false; 70 for (Iterator i = pendingStartup.iterator(); i.hasNext();) { 71 Runnable runnable = (Runnable ) i.next(); 72 try { 73 super.asyncExec(runnable); 75 } catch (RuntimeException e) { 76 } 78 } 79 pendingStartup = null; 80 this.notifyAll(); 82 } 83 } 84 85 88 protected void asyncExec(Runnable runnable) { 89 if (runnable != null) { 90 synchronized (this) { 91 if (isStarting 92 && !(runnable instanceof StartupRunnable)) { 93 pendingStartup.add(runnable); 94 95 return; 96 } 97 } 98 } 99 super.asyncExec(runnable); 100 } 101 102 public void syncExec(Runnable runnable) { 103 104 synchronized (this) { 105 if (isStarting && UISynchronizer.startupThread.get() == Boolean.FALSE) { 106 do { 107 try { 108 this.wait(); 109 } catch (InterruptedException e) { 110 } 111 } while (isStarting); 112 } 113 } 114 115 if ((runnable == null) || lockListener.isUI() 117 || !lockListener.isLockOwner()) { 118 super.syncExec(runnable); 119 return; 120 } 121 Semaphore work = new Semaphore(runnable); 122 work.setOperationThread(Thread.currentThread()); 123 lockListener.addPendingWork(work); 124 asyncExec(new Runnable () { 125 public void run() { 126 lockListener.doPendingWork(); 127 } 128 }); 129 try { 130 do { 133 if (lockListener.isUIWaiting()) { 134 lockListener.interruptUI(); 135 } 136 } while (!work.acquire(1000)); 137 } catch (InterruptedException e) { 138 } 139 } 140 } 141 | Popular Tags |