KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > provisional > swt > SWTUtil


1 /*******************************************************************************
2  * Copyright (c) 2006 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 package org.eclipse.jface.internal.databinding.provisional.swt;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.jface.util.SafeRunnable;
17 import org.eclipse.swt.graphics.RGB;
18 import org.eclipse.swt.widgets.Display;
19
20 /**
21  * NON-API - Utility methods, mainly having to do with posting runnables to the UI thread
22  * in a particular way.
23  * @since 1.1
24  */

25 public class SWTUtil {
26     /**
27      * Stores a work queue for each display
28      */

29     private static Map JavaDoc mapDisplayOntoWorkQueue = new HashMap JavaDoc();
30
31     private SWTUtil() {
32     }
33
34     /**
35      * Runs the given runnable on the given display as soon as possible. If
36      * possible, the runnable will be executed before the next widget is
37      * repainted, but this behavior is not guaranteed. Use this method to
38      * schedule work will affect the way one or more widgets are drawn.
39      *
40      * <p>
41      * This is threadsafe.
42      * </p>
43      *
44      * @param d
45      * display
46      * @param r
47      * runnable to execute in the UI thread.
48      */

49     public static void greedyExec(Display d, Runnable JavaDoc r) {
50         if (d.isDisposed()) {
51             return;
52         }
53
54         // if (Display.getCurrent() == d) {
55
// r.run();
56
// } else {
57
WorkQueue queue = getQueueFor(d);
58         queue.asyncExec(r);
59         // }
60
}
61
62     /**
63      * Runs the given runnable on the given display as soon as possible. Unlike
64      * greedyExec, this has no effect if the given runnable has already been
65      * scheduled for execution. Use this method to schedule work that will
66      * affect the way one or more wigdets are drawn, but that should only happen
67      * once.
68      *
69      * <p>
70      * This is threadsafe.
71      * </p>
72      *
73      * @param d
74      * display
75      * @param r
76      * runnable to execute in the UI thread. Has no effect if the
77      * given runnable has already been scheduled but has not yet run.
78      */

79     public static void runOnce(Display d, Runnable JavaDoc r) {
80         if (d.isDisposed()) {
81             return;
82         }
83         WorkQueue queue = getQueueFor(d);
84         queue.runOnce(r);
85     }
86
87     /**
88      * Cancels a greedyExec or runOnce that was previously scheduled on the
89      * given display. Has no effect if the given runnable is not in the queue
90      * for the given display
91      *
92      * @param d
93      * target display
94      * @param r
95      * runnable to execute
96      */

97     public static void cancelExec(Display d, Runnable JavaDoc r) {
98         if (d.isDisposed()) {
99             return;
100         }
101         WorkQueue queue = getQueueFor(d);
102         queue.cancelExec(r);
103     }
104
105     /**
106      * Returns the work queue for the given display. Creates a work queue if
107      * none exists yet.
108      *
109      * @param d
110      * display to return queue for
111      * @return a work queue (never null)
112      */

113     private static WorkQueue getQueueFor(final Display d) {
114         WorkQueue result;
115         synchronized (mapDisplayOntoWorkQueue) {
116             // Look for existing queue
117
result = (WorkQueue) mapDisplayOntoWorkQueue.get(d);
118
119             if (result == null) {
120                 // If none, create new queue
121
result = new WorkQueue(d);
122                 final WorkQueue q = result;
123                 mapDisplayOntoWorkQueue.put(d, result);
124                 d.asyncExec(new Runnable JavaDoc() {
125                     public void run() {
126                         d.disposeExec(new Runnable JavaDoc() {
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     /**
142      * @param rgb1
143      * @param rgb2
144      * @param ratio
145      * @return the RGB object
146      */

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     /**
161      * Logs an exception as though it was thrown by a SafeRunnable
162      * being run with the default ISafeRunnableRunner. Will not
163      * open modal dialogs or spin the event loop.
164      *
165      * @param t throwable to log
166      */

167     public static void logException(final Exception JavaDoc t) {
168         SafeRunnable.run(new SafeRunnable() {
169             public void run() throws Exception JavaDoc {
170                 throw t;
171             }
172             public void handleException(Throwable JavaDoc e) {
173                 // IMPORTANT: Do not call the super implementation, since
174
// it opens a modal dialog, and may cause *syncExecs to run
175
// too early.
176
}
177         });
178     }
179 }
180
Popular Tags