1 11 package org.eclipse.jface.internal.databinding.internal.swt; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.jface.util.SafeRunnable; 17 import org.eclipse.swt.graphics.RGB; 18 import org.eclipse.swt.widgets.Display; 19 20 23 public class SWTUtil { 24 27 private static Map mapDisplayOntoWorkQueue = new HashMap (); 28 29 private SWTUtil() { 30 } 31 32 47 public static void greedyExec(Display d, Runnable r) { 48 if (d.isDisposed()) { 49 return; 50 } 51 52 WorkQueue queue = getQueueFor(d); 56 queue.asyncExec(r); 57 } 59 60 77 public static void runOnce(Display d, Runnable r) { 78 if (d.isDisposed()) { 79 return; 80 } 81 WorkQueue queue = getQueueFor(d); 82 queue.runOnce(r); 83 } 84 85 95 public static void cancelExec(Display d, Runnable r) { 96 if (d.isDisposed()) { 97 return; 98 } 99 WorkQueue queue = getQueueFor(d); 100 queue.cancelExec(r); 101 } 102 103 111 private static WorkQueue getQueueFor(final Display d) { 112 WorkQueue result; 113 synchronized (mapDisplayOntoWorkQueue) { 114 result = (WorkQueue) mapDisplayOntoWorkQueue.get(d); 116 117 if (result == null) { 118 result = new WorkQueue(d); 120 final WorkQueue q = result; 121 mapDisplayOntoWorkQueue.put(d, result); 122 d.asyncExec(new Runnable () { 123 public void run() { 124 d.disposeExec(new Runnable () { 125 public void run() { 126 synchronized (mapDisplayOntoWorkQueue) { 127 q.cancelAll(); 128 mapDisplayOntoWorkQueue.remove(d); 129 } 130 } 131 }); 132 } 133 }); 134 } 135 return result; 136 } 137 } 138 139 public static RGB mix(RGB rgb1, RGB rgb2, double ratio) { 140 return new RGB(interp(rgb1.red, rgb2.red, ratio), 141 interp(rgb1.green, rgb2.green, ratio), 142 interp(rgb1.blue, rgb2.blue, ratio)); 143 } 144 145 private static int interp(int i1, int i2, double ratio) { 146 int result = (int)(i1 * ratio + i2 * (1.0d - ratio)); 147 if (result < 0) result = 0; 148 if (result > 255) result = 255; 149 return result; 150 } 151 152 159 public static void logException(final Exception t) { 160 SafeRunnable.run(new SafeRunnable() { 161 public void run() throws Exception { 162 throw t; 163 }; 164 public void handleException(Throwable e) { 165 }; 169 }); 170 } 171 } 172 | Popular Tags |