(defn mapref-tree-model
[map-ref root-node &
{node-wrapper :node-wrapper leaf? :leaf?
:or {node-wrapper (fn [node path] (Pathed. node (str node) path))
leaf? (fn [node map-ref]
(not (map? (get-in @map-ref (path node)))))}}]
(let [listeners (atom [])
root (node-wrapper root-node [])
model
(proxy [TreeModel] []
(addTreeModelListener [l] (swap! listeners conj l))
(getChild [node idx]
(let [par (get-in @map-ref (path node))
ks (keys par)
chi (nth ks idx)]
(node-wrapper chi (conj (path node) chi))))
(getChildCount [node]
(count (keys (get-in @map-ref (path node)))))
(getIndexOfChild [n chi]
(let [ks (keys (get-in @map-ref (path n)))]
(index-of ks (node chi))))
(getRoot []
root)
(isLeaf [node]
(leaf? node map-ref))
(removeTreeModelListener [l] (swap! listeners #(remove (partial = l) %)))
(valueForPathChanged
[path value]
(dosync
(alter map-ref update-in (.getLastPathComponent path) (constantly value)))))]
(add-watch map-ref (gensym)
(fn [k r o n]
(do-swing
(let [c (changed-path n o)
k-n (set (keys (get-in n c)))
k-o (set (keys (get-in o c)))
only-n (difference k-n k-o)
only-o (difference k-o k-n)
_ (prn 1 only-n only-o)
node-changed (and
(= 1 (count only-n) (count only-o))
(= (get-in n (concat c only-n))
(get-in o (concat c only-o))))
; c (if node-changed (concat c only-n) c)
p (loop [ps [] c c]
(if (empty? c)
(reverse (conj ps root))
(recur (conj ps (node-wrapper (last c) (vec c))) (butlast c))))
tp (TreePath. (to-array p))
e (TreeModelEvent. model tp)]
(doseq [l @listeners]
(if node-changed
(do
(.treeStructureChanged l e)
)
(.treeStructureChanged l e)))))))
model))
Used in 0 other vars
Comments top
No comments for mapref-tree-model. Log in to add a comment.