Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update-tree! for updating JTree #50

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/seesaw/tree.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)
17 changes: 15 additions & 2 deletions test/seesaw/test/tree.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))))