comparison org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/provisional/swt/SWTUtil.d @ 78:0a55d2d5a946

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