 (ns org.micromanager.reloader
   (:use [clojure.java.io :only (file copy)]
         [clojure.data :only (diff)]
-        [org.micromanager.mm :only (load-mm mmc core)]
-        [clojure.pprint :only (pprint)]))
+        [org.micromanager.mm :only (edt load-mm mmc core gui)]
+        [clojure.pprint :only (pprint)])
+  (:import (javax.swing JLabel JWindow)
+           (java.awt Color)))
 
 
 (load-mm (org.micromanager.MMStudioMainFrame/getInstance))

    #(reset! keep-following false)))
      
(defn watch-dates
  [date-atom handle-new-files-fn]
  (add-watch date-atom "file-date"
             (fn [_ _ old-val new-val]
               (when-let [new-files (keys (second (diff old-val new-val)))]
                 (println new-files)
                 (handle-new-files-fn new-files)))))

(defn module-for-dll [dll]
  (-> dll
      file .getName (.split "\\.")
      first (.replace "mmgr_dal_" "")))

(defn reload-updated-module [new-dll-version]
  (let [dll (file new-dll-version)
        module (module-for-dll dll)
        loaded-modules (keys (devices-in-each-module))]
    (when (some #{module} loaded-modules)
      (println "Reloading" module "module...")
      (reload-module module
        (copy dll
              (file "." (.getName dll))))
      (println "...done!"))))

(defn reload-updated-modules [new-dll-versions]
  (doseq [dll new-dll-versions]
    (reload-updated-module dll)))

(defn reload-modules-on-device-adapter-change [dll-dir-path]
  (let [date-atom (atom nil)
        stop-fn (follow-dir-dates date-atom dll-dir-path)]
    (watch-dates date-atom reload-updated-modules)
    stop-fn))
        


      (startup-device dev (dev-settings-map dev)))))

(defmacro reload-module
  "Unload all devices in a module. Run housekeeping code.
   Then load and restart again with the original settings."
  [module & housekeeping]
  `(reload-module-fn ~module (fn [] ~@housekeeping)))

(defn file-dates
  "Maps each file in path to its last modification date."
  [path]
  (let [files (.listFiles (file path))]
    (zipmap files (map #(.lastModified %) files))))

(defn follow-dir-dates
  "Polls a directory and keeps an up-to-date
