aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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)))
+
+;; ----------------------------------------------------------------------