aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/swing_utils.clj
diff options
context:
space:
mode:
authorscgilardi <scgilardi@gmail.com>2009-06-05 15:51:54 +0000
committerscgilardi <scgilardi@gmail.com>2009-06-05 15:51:54 +0000
commit3dc2e34ea55aaba33c563cd3048ba076106094cc (patch)
treec0fa021d5573896bce6349ec65037dddabd59e55 /src/clojure/contrib/swing_utils.clj
parent3a33bd9ac44b434625c91772b1bbbcdc414a3c49 (diff)
swing-utils: add do-swing, do-swing-and-wait macros from Meikel Brandmeyer
Diffstat (limited to 'src/clojure/contrib/swing_utils.clj')
-rw-r--r--src/clojure/contrib/swing_utils.clj32
1 files changed, 31 insertions, 1 deletions
diff --git a/src/clojure/contrib/swing_utils.clj b/src/clojure/contrib/swing_utils.clj
index edcec999..ac41b29a 100644
--- a/src/clojure/contrib/swing_utils.clj
+++ b/src/clojure/contrib/swing_utils.clj
@@ -14,7 +14,8 @@
;; Created 31 May 2009
(ns clojure.contrib.swing-utils
- (:import (java.awt.event ActionListener KeyAdapter)))
+ (:import (java.awt.event ActionListener KeyAdapter)
+ (javax.swing SwingUtilities)))
(defn add-action-listener
"Adds an ActionLister to component. When the action fires, f will be
@@ -35,3 +36,32 @@
(keyTyped [event] (apply f event args)))]
(.addKeyListener component listener)
listener))
+
+;; ----------------------------------------------------------------------
+;; Meikel Brandmeyer
+
+(defn do-swing*
+ "Runs thunk in the Swing event thread according to schedule:
+ - :later => schedule the execution and return immediately
+ - :now => wait until the execution completes."
+ [schedule thunk]
+ (cond
+ (= schedule :later) (SwingUtilities/invokeLater thunk)
+ (= schedule :now) (if (SwingUtilities/isEventDispatchThread)
+ (thunk)
+ (SwingUtilities/invokeAndWait thunk)))
+ nil)
+
+(defmacro do-swing
+ "Executes body in the Swing event thread asynchronously. Returns
+ immediately after scheduling the execution."
+ [& body]
+ `(do-swing* :later (fn [] ~@body)))
+
+(defmacro do-swing-and-wait
+ "Executes body in the Swing event thread synchronously. Returns
+ after the execution is complete."
+ [& body]
+ `(do-swing* :now (fn [] ~@body)))
+
+;; ----------------------------------------------------------------------