diff --git a/src/seesaw/tree.clj b/src/seesaw/tree.clj index 8a9b0a32..742756f0 100644 --- a/src/seesaw/tree.clj +++ b/src/seesaw/tree.clj @@ -25,3 +25,22 @@ (removeTreeModelListener [this listener]) (valueForPathChanged [this path newValue]))) +(defn update-tree! + "Update a tree. + The model is optional, if not supplied this function refreshes + the tree (useful for e.g. file trees). + + Expanded nodes will still be expanded after update, given that + the expanded node didn't change. + " + {:arglists '([tree model?])} + [tree & [model]] + (if model + (let [visible_paths (doall + (for [row (range (.getRowCount tree))] + (.getPathForRow tree row)))] + (.setModel tree (if model model (.getModel tree))) + (doseq [path visible_paths] + (.makeVisible tree path))) + (.updateUI tree)) + tree) diff --git a/test/seesaw/test/tree.clj b/test/seesaw/test/tree.clj index e0d80a1e..77ae9392 100644 --- a/test/seesaw/test/tree.clj +++ b/test/seesaw/test/tree.clj @@ -10,7 +10,7 @@ (ns seesaw.test.tree (:use seesaw.tree) - (:use [lazytest.describe :only (describe it testing given)] + (:use [lazytest.describe :only (describe do-it it testing given)] [lazytest.expect :only (expect)])) (describe simple-tree-model @@ -32,4 +32,17 @@ (it "should retrieve the index of a child" (= [0 1 2] (map #(.getIndexOfChild m "dir" %) [1 2 3]))))) - +(describe update-tree! + (given [tree (javax.swing.JTree. (simple-tree-model (constantly true) #(range (inc %)) 1)) + path (javax.swing.tree.TreePath. (into-array [1 1 1 1]))] + (do-it "expand path" + (.makeVisible tree path)) + (it "should be visible before update" + (.isVisible tree path)) + (do-it "update tree" + (update-tree! tree (simple-tree-model (constantly true) #(range (+ % 2)) 1))) + (it "should update the tree" + (= [1 2] + (vec (.getPath (.getPathForRow tree (dec (.getRowCount tree))))))) + (it "should retain expanded paths after update" + (.isVisible tree path))))