         (when (> y (+ (.y r) (.height r)))
           (f))))))
 
+(defn start-editing-cell [table row col]
+  (edt (doto table
+         (.setRowSelectionInterval row row)
+         (.setColumnSelectionInterval col col)
+         (.. getCellEditor getComponent requestFocus)
+         (.editCellAt row col)
+         )))
+
+(defn create-new-group []
+  (swap! groups conj "New Group")
+  (let [table  (@widgets :groups-table)]
+    (.fireTableStructureChanged (.getModel table))
+    (let [row (dec (.getRowCount table))]
+      (start-editing-cell table row 0))))
+
+(defn show-popup-menu [popup-menu mouse-event]
+  (when (.isPopupTrigger mouse-event)
+    (def e mouse-event)
+          (let [point (.getPoint mouse-event)
+                source (.getSource mouse-event)
+                row (.rowAtPoint source point)
+                column (.columnAtPoint source point)]
+            (set-selected-row source row)
+            (.show popup-menu source (.x point) (.y point)))))
+
+(defn context-menu-on-table [table popup-menu]
+  (.addMouseListener table
+    (proxy [MouseAdapter] []
+      (mousePressed [e]
+        (show-popup-menu popup-menu e))
+      (mouseReleased [e]
+        (show-popup-menu popup-menu e)))))
+           
+(defn cancel-cell-editing [table]
+  (when-let [editor (.getCellEditor table)]
+    (.cancelCellEditing editor)))
+  
 (defn show []
   (let [f (JFrame. "Micro-Manager Configuration Preset Editor")
         cp (.getContentPane f)

(ns org.micromanager.config-editor
  (:import [javax.swing DefaultListModel JFrame JList JPanel JTable DefaultListSelectionModel
                        JScrollPane JViewport ListSelectionModel SpringLayout]
           [javax.swing.event CellEditorListener ListSelectionListener]
           [java.awt.event MouseAdapter]
           [javax.swing.table AbstractTableModel]
           [java.awt Color Dimension])
  (:use [org.micromanager.mm :only [load-mm gui core edt get-config]]))

;; utils

;(defn updater-agent [init-value update-fn]
;  (let [a (agent init-value)]
;    (await a 

;; swing layout 


                         (presets group))))))

(defn update-groups []
  (reset! groups (vec (seq (core getAvailableConfigGroups))))
  (update-group-data)
  @groups)

(defn properties [group-data]
  (sort (set (apply concat (map keys (vals group-data))))))
