         
 (def ^{:dynamic true} stop (fn []))
 
-(def default-directory "C:\\projects\\micromanager\\bin_x64")
-
-(defn show-plugin [plugin]
-  (println "starting DLLAutoReloader plugin")
+(defn activate
+  "Start device adapter library reloading."
+  [path]
   (stop)
-  (def stop (reload-modules-on-device-adapter-change default-directory))
+  (def stop (reload-modules-on-device-adapter-change path))
   (notify-user true "Ready to autoreload!"))
 
-(defn handle-exit [] (stop))
+(defn deactivate []
+  "Stop device adapter library reloading."
+  []
+  (stop)
+  (notify-user true "Autoreloading deactivated!"))
+
+(def dll-directory-type (FileDialogs$FileType. "DLL Directory" "New DLL location" "" false nil))
+
+(defn choose-dll-dir
+  []
+  (FileDialogs/openDir nil "Please choose a directory where new DLLs will appear"
+                       dll-directory-type))
+
+(def prefs (.. Preferences userRoot (node "DLLAutoReloader")))
+
+(defn startup
+  []
+  (let [frame (proxy [JFrame] []) ; use proxy to get unique JFrame class
+        path (.get prefs "dir" nil)
+        path-button (JButton. (or path "(choose path)"))]
+    (when path
+      (activate path))
+    (doto frame
+      (.setBounds 100 100 700 50)
+      (.setResizable false)
+      (GUIUtils/recallPosition)
+      (.setTitle "DLL Auto Reloading"))
+    (.addActionListener path-button
+                        (proxy [ActionListener] []
+                          (actionPerformed [e]
+                            (when-let [dir (file (choose-dll-dir))]
+                              (let [path (.getAbsolutePath dir)]
+                              (.setText path-button path)
+                              (activate path)
+                              (.put prefs "dir" path))))))
+    (doto (.getContentPane frame)
+      (.add path-button))
+    (.show frame)))
+
+(defn showPlugin [this]
+  (startup))
+
+(defn handle-exit []
+  (stop))
 
 ;; testing
 

      (.setText label (apply label-html message-parts))
      (.show frame))
    (when temporary?
    (future (Thread/sleep 3000)
            (edt (.hide frame))))))

(defn reload-updated-module
  "Unload a module, copy the new version to the Micro-Manager

        [clojure.data :only (diff)]
        [org.micromanager.mm :only (edt load-mm mmc core gui)]
        [clojure.pprint :only (pprint)])
  (:import (javax.swing JButton JFrame JLabel JWindow)
           (java.awt Color)
           (java.awt.event ActionListener)
           (java.util.prefs Preferences)
           (org.micromanager.utils FileDialogs FileDialogs$FileType
                                   GUIUtils JavaUtils)))

(load-mm (org.micromanager.MMStudioMainFrame/getInstance))

