78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 *******************************************************************************/
|
|
11 module org.eclipse.jface.internal.databinding.provisional.swt.SWTUtil;
|
85
|
12 import org.eclipse.jface.internal.databinding.provisional.swt.WorkQueue;
|
78
|
13
|
|
14 import java.lang.all;
|
|
15
|
|
16 import java.util.HashMap;
|
|
17 import java.util.Map;
|
|
18
|
|
19 import org.eclipse.jface.util.SafeRunnable;
|
|
20 import org.eclipse.swt.graphics.RGB;
|
|
21 import org.eclipse.swt.widgets.Display;
|
|
22
|
|
23 /**
|
|
24 * NON-API - Utility methods, mainly having to do with posting runnables to the UI thread
|
|
25 * in a particular way.
|
|
26 * @since 1.1
|
|
27 *
|
|
28 */
|
|
29 public class SWTUtil {
|
|
30 /**
|
|
31 * Stores a work queue for each display
|
|
32 */
|
85
|
33 private static Map mapDisplayOntoWorkQueue;
|
|
34 static this(){
|
|
35 mapDisplayOntoWorkQueue = new HashMap();
|
|
36 }
|
78
|
37
|
|
38 private this() {
|
|
39 }
|
|
40
|
|
41 /**
|
|
42 * Runs the given runnable on the given display as soon as possible. If
|
|
43 * possible, the runnable will be executed before the next widget is
|
|
44 * repainted, but this behavior is not guaranteed. Use this method to
|
|
45 * schedule work will affect the way one or more widgets are drawn.
|
|
46 *
|
|
47 * <p>
|
|
48 * This is threadsafe.
|
|
49 * </p>
|
|
50 *
|
|
51 * @param d
|
|
52 * display
|
|
53 * @param r
|
|
54 * runnable to execute in the UI thread.
|
|
55 */
|
|
56 public static void greedyExec(Display d, Runnable r) {
|
|
57 if (d.isDisposed()) {
|
|
58 return;
|
|
59 }
|
|
60
|
|
61 // if (Display.getCurrent() is d) {
|
|
62 // r.run();
|
|
63 // } else {
|
|
64 WorkQueue queue = getQueueFor(d);
|
|
65 queue.asyncExec(r);
|
|
66 // }
|
|
67 }
|
|
68
|
|
69 /**
|
|
70 * Runs the given runnable on the given display as soon as possible. Unlike
|
|
71 * greedyExec, this has no effect if the given runnable has already been
|
|
72 * scheduled for execution. Use this method to schedule work that will
|
|
73 * affect the way one or more wigdets are drawn, but that should only happen
|
|
74 * once.
|
|
75 *
|
|
76 * <p>
|
|
77 * This is threadsafe.
|
|
78 * </p>
|
|
79 *
|
|
80 * @param d
|
|
81 * display
|
|
82 * @param r
|
|
83 * runnable to execute in the UI thread. Has no effect if the
|
|
84 * given runnable has already been scheduled but has not yet run.
|
|
85 */
|
|
86 public static void runOnce(Display d, Runnable r) {
|
|
87 if (d.isDisposed()) {
|
|
88 return;
|
|
89 }
|
|
90 WorkQueue queue = getQueueFor(d);
|
|
91 queue.runOnce(r);
|
|
92 }
|
|
93
|
|
94 /**
|
|
95 * Cancels a greedyExec or runOnce that was previously scheduled on the
|
|
96 * given display. Has no effect if the given runnable is not in the queue
|
|
97 * for the given display
|
|
98 *
|
|
99 * @param d
|
|
100 * target display
|
|
101 * @param r
|
|
102 * runnable to execute
|
|
103 */
|
|
104 public static void cancelExec(Display d, Runnable r) {
|
|
105 if (d.isDisposed()) {
|
|
106 return;
|
|
107 }
|
|
108 WorkQueue queue = getQueueFor(d);
|
|
109 queue.cancelExec(r);
|
|
110 }
|
|
111
|
|
112 /**
|
|
113 * Returns the work queue for the given display. Creates a work queue if
|
|
114 * none exists yet.
|
|
115 *
|
|
116 * @param d
|
|
117 * display to return queue for
|
|
118 * @return a work queue (never null)
|
|
119 */
|
|
120 private static WorkQueue getQueueFor(Display d) {
|
|
121 WorkQueue result;
|
|
122 synchronized (mapDisplayOntoWorkQueue) {
|
|
123 // Look for existing queue
|
|
124 result = cast(WorkQueue) mapDisplayOntoWorkQueue.get(d);
|
|
125
|
|
126 if (result is null) {
|
|
127 // If none, create new queue
|
|
128 result = new WorkQueue(d);
|
|
129 final WorkQueue q = result;
|
|
130 mapDisplayOntoWorkQueue.put(d, result);
|
|
131 d.asyncExec(dgRunnable( (Display d_) {
|
|
132 d_.disposeExec(new class() Runnable {
|
|
133 public void run() {
|
|
134 synchronized (mapDisplayOntoWorkQueue) {
|
|
135 q.cancelAll();
|
|
136 mapDisplayOntoWorkQueue.remove(d_);
|
|
137 }
|
|
138 }
|
|
139 });
|
|
140 }, d));
|
|
141 }
|
|
142 return result;
|
|
143 }
|
|
144 }
|
|
145
|
|
146 /**
|
|
147 * @param rgb1
|
|
148 * @param rgb2
|
|
149 * @param ratio
|
|
150 * @return the RGB object
|
|
151 */
|
|
152 public static RGB mix(RGB rgb1, RGB rgb2, double ratio) {
|
|
153 return new RGB(interp(rgb1.red, rgb2.red, ratio),
|
|
154 interp(rgb1.green, rgb2.green, ratio),
|
|
155 interp(rgb1.blue, rgb2.blue, ratio));
|
|
156 }
|
|
157
|
|
158 private static int interp(int i1, int i2, double ratio) {
|
|
159 int result = cast(int)(i1 * ratio + i2 * (1.0 - ratio));
|
|
160 if (result < 0) result = 0;
|
|
161 if (result > 255) result = 255;
|
|
162 return result;
|
|
163 }
|
|
164
|
|
165 /**
|
|
166 * Logs an exception as though it was thrown by a SafeRunnable being run
|
|
167 * with the default ISafeRunnableRunner. Will not open modal dialogs or spin
|
|
168 * the event loop.
|
|
169 *
|
|
170 * @param t
|
|
171 * throwable to log
|
|
172 * @deprecated
|
|
173 * @noreference This method is not intended to be referenced by clients. It
|
|
174 * remains here for API backwards compatibility.
|
|
175 */
|
|
176 public static void logException(Exception t) {
|
|
177 SafeRunnable.run(new class(t) SafeRunnable {
|
|
178 Exception t_;
|
|
179 this(Exception a){ t_=a; }
|
|
180 public void run() {
|
|
181 throw t_;
|
|
182 }
|
|
183 public void handleException(Throwable e) {
|
|
184 // IMPORTANT: Do not call the super implementation, since
|
|
185 // it opens a modal dialog, and may cause *syncExecs to run
|
|
186 // too early.
|
|
187 }
|
|
188 });
|
|
189 }
|
|
190
|
|
191 }
|