KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > internal > databinding > internal > 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.internal.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  * @since 1.0
22  */

23 public class SWTUtil {
24     /**
25      * Stores a work queue for each display
26      */

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

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

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

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

111     private static WorkQueue getQueueFor(final Display d) {
112         WorkQueue result;
113         synchronized (mapDisplayOntoWorkQueue) {
114             // Look for existing queue
115
result = (WorkQueue) mapDisplayOntoWorkQueue.get(d);
116
117             if (result == null) {
118                 // If none, create new queue
119
result = new WorkQueue(d);
120                 final WorkQueue q = result;
121                 mapDisplayOntoWorkQueue.put(d, result);
122                 d.asyncExec(new Runnable JavaDoc() {
123                     public void run() {
124                         d.disposeExec(new Runnable JavaDoc() {
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     /**
153      * Logs an exception as though it was thrown by a SafeRunnable
154      * being run with the default ISafeRunnableRunner. Will not
155      * open modal dialogs or spin the event loop.
156      *
157      * @param t throwable to log
158      */

159     public static void logException(final Exception JavaDoc t) {
160         SafeRunnable.run(new SafeRunnable() {
161             public void run() throws Exception JavaDoc {
162                 throw t;
163             };
164             public void handleException(Throwable JavaDoc e) {
165                 // IMPORTANT: Do not call the super implementation, since
166
// it opens a modal dialog, and may cause *syncExecs to run
167
// too early.
168
};
169         });
170     }
171 }
172
Popular Tags