diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/org.eclipse.jface.databinding/src/org/eclipse/jface/internal/databinding/provisional/swt/SWTUtil.d	Tue Apr 14 11:35:29 2009 +0200
@@ -0,0 +1,187 @@
+/*******************************************************************************
+ * Copyright (c) 2006, 2008 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ *******************************************************************************/
+module org.eclipse.jface.internal.databinding.provisional.swt.SWTUtil;
+
+import java.lang.all;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.eclipse.jface.util.SafeRunnable;
+import org.eclipse.swt.graphics.RGB;
+import org.eclipse.swt.widgets.Display;
+
+/**
+ * NON-API - Utility methods, mainly having to do with posting runnables to the UI thread
+ * in a particular way.
+ * @since 1.1
+ * 
+ */
+public class SWTUtil {
+    /**
+     * Stores a work queue for each display
+     */
+    private static Map mapDisplayOntoWorkQueue = new HashMap();
+
+    private this() {
+    }
+
+    /**
+     * Runs the given runnable on the given display as soon as possible. If
+     * possible, the runnable will be executed before the next widget is
+     * repainted, but this behavior is not guaranteed. Use this method to
+     * schedule work will affect the way one or more widgets are drawn.
+     * 
+     * <p>
+     * This is threadsafe.
+     * </p>
+     * 
+     * @param d
+     *            display
+     * @param r
+     *            runnable to execute in the UI thread.
+     */
+    public static void greedyExec(Display d, Runnable r) {
+        if (d.isDisposed()) {
+            return;
+        }
+
+        // if (Display.getCurrent() is d) {
+        // r.run();
+        // } else {
+        WorkQueue queue = getQueueFor(d);
+        queue.asyncExec(r);
+        // }
+    }
+
+    /**
+     * Runs the given runnable on the given display as soon as possible. Unlike
+     * greedyExec, this has no effect if the given runnable has already been
+     * scheduled for execution. Use this method to schedule work that will
+     * affect the way one or more wigdets are drawn, but that should only happen
+     * once.
+     * 
+     * <p>
+     * This is threadsafe.
+     * </p>
+     * 
+     * @param d
+     *            display
+     * @param r
+     *            runnable to execute in the UI thread. Has no effect if the
+     *            given runnable has already been scheduled but has not yet run.
+     */
+    public static void runOnce(Display d, Runnable r) {
+        if (d.isDisposed()) {
+            return;
+        }
+        WorkQueue queue = getQueueFor(d);
+        queue.runOnce(r);
+    }
+
+    /**
+     * Cancels a greedyExec or runOnce that was previously scheduled on the
+     * given display. Has no effect if the given runnable is not in the queue
+     * for the given display
+     * 
+     * @param d
+     *            target display
+     * @param r
+     *            runnable to execute
+     */
+    public static void cancelExec(Display d, Runnable r) {
+        if (d.isDisposed()) {
+            return;
+        }
+        WorkQueue queue = getQueueFor(d);
+        queue.cancelExec(r);
+    }
+
+    /**
+     * Returns the work queue for the given display. Creates a work queue if
+     * none exists yet.
+     * 
+     * @param d
+     *            display to return queue for
+     * @return a work queue (never null)
+     */
+    private static WorkQueue getQueueFor(Display d) {
+        WorkQueue result;
+        synchronized (mapDisplayOntoWorkQueue) {
+            // Look for existing queue
+            result = cast(WorkQueue) mapDisplayOntoWorkQueue.get(d);
+
+            if (result is null) {
+                // If none, create new queue
+                result = new WorkQueue(d);
+                final WorkQueue q = result;
+                mapDisplayOntoWorkQueue.put(d, result);
+                d.asyncExec(dgRunnable( (Display d_) {
+                    d_.disposeExec(new class() Runnable {
+                        public void run() {
+                            synchronized (mapDisplayOntoWorkQueue) {
+                                q.cancelAll();
+                                mapDisplayOntoWorkQueue.remove(d_);
+                            }
+                        }
+                    });
+                }, d));
+            }
+            return result;
+        }
+    }
+    
+    /**
+     * @param rgb1
+     * @param rgb2
+     * @param ratio
+     * @return the RGB object
+     */
+    public static RGB mix(RGB rgb1, RGB rgb2, double ratio) {
+        return new RGB(interp(rgb1.red, rgb2.red, ratio), 
+                interp(rgb1.green, rgb2.green, ratio),
+                interp(rgb1.blue, rgb2.blue, ratio));
+    }
+    
+    private static int interp(int i1, int i2, double ratio) {
+        int result = cast(int)(i1 * ratio + i2 * (1.0 - ratio));
+        if (result < 0) result = 0;
+        if (result > 255) result = 255;
+        return result;
+    }
+    
+    /**
+     * Logs an exception as though it was thrown by a SafeRunnable being run
+     * with the default ISafeRunnableRunner. Will not open modal dialogs or spin
+     * the event loop.
+     * 
+     * @param t
+     *            throwable to log
+     * @deprecated
+     * @noreference This method is not intended to be referenced by clients. It
+     *              remains here for API backwards compatibility.
+     */
+    public static void logException(Exception t) {
+        SafeRunnable.run(new class(t) SafeRunnable {
+            Exception t_;
+            this(Exception a){ t_=a; }
+            public void run() {
+                throw t_;
+            }
+            public void handleException(Throwable e) {
+                // IMPORTANT: Do not call the super implementation, since
+                // it opens a modal dialog, and may cause *syncExecs to run
+                // too early.
+            }
+        });
+    }
+
+}