1 11 package org.eclipse.jface.internal.databinding.provisional.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 25 public class SWTUtil { 26 29 private static Map mapDisplayOntoWorkQueue = new HashMap (); 30 31 private SWTUtil() { 32 } 33 34 49 public static void greedyExec(Display d, Runnable r) { 50 if (d.isDisposed()) { 51 return; 52 } 53 54 WorkQueue queue = getQueueFor(d); 58 queue.asyncExec(r); 59 } 61 62 79 public static void runOnce(Display d, Runnable r) { 80 if (d.isDisposed()) { 81 return; 82 } 83 WorkQueue queue = getQueueFor(d); 84 queue.runOnce(r); 85 } 86 87 97 public static void cancelExec(Display d, Runnable r) { 98 if (d.isDisposed()) { 99 return; 100 } 101 WorkQueue queue = getQueueFor(d); 102 queue.cancelExec(r); 103 } 104 105 113 private static WorkQueue getQueueFor(final Display d) { 114 WorkQueue result; 115 synchronized (mapDisplayOntoWorkQueue) { 116 result = (WorkQueue) mapDisplayOntoWorkQueue.get(d); 118 119 if (result == null) { 120 result = new WorkQueue(d); 122 final WorkQueue q = result; 123 mapDisplayOntoWorkQueue.put(d, result); 124 d.asyncExec(new Runnable () { 125 public void run() { 126 d.disposeExec(new Runnable () { 127 public void run() { 128 synchronized (mapDisplayOntoWorkQueue) { 129 q.cancelAll(); 130 mapDisplayOntoWorkQueue.remove(d); 131 } 132 } 133 }); 134 } 135 }); 136 } 137 return result; 138 } 139 } 140 141 147 public static RGB mix(RGB rgb1, RGB rgb2, double ratio) { 148 return new RGB(interp(rgb1.red, rgb2.red, ratio), 149 interp(rgb1.green, rgb2.green, ratio), 150 interp(rgb1.blue, rgb2.blue, ratio)); 151 } 152 153 private static int interp(int i1, int i2, double ratio) { 154 int result = (int)(i1 * ratio + i2 * (1.0d - ratio)); 155 if (result < 0) result = 0; 156 if (result > 255) result = 255; 157 return result; 158 } 159 160 167 public static void logException(final Exception t) { 168 SafeRunnable.run(new SafeRunnable() { 169 public void run() throws Exception { 170 throw t; 171 } 172 public void handleException(Throwable e) { 173 } 177 }); 178 } 179 } 180 | Popular Tags |