PageRenderTime 68ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/src/seesaw/examples/tree.clj

https://github.com/odyssomay/seesaw
Clojure | 60 lines | 36 code | 10 blank | 14 comment | 1 complexity | 4bff72d59240935a90f2f791c32ed3cf MD5 | raw file
  1. ; Copyright (c) Dave Ray, 2011. All rights reserved.
  2. ; The use and distribution terms for this software are covered by the
  3. ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
  4. ; which can be found in the file epl-v10.html at the root of this
  5. ; distribution.
  6. ; By using this software in any fashion, you are agreeing to be bound by
  7. ; the terms of this license.
  8. ; You must not remove this notice, or any other, from this software.
  9. (ns seesaw.examples.tree
  10. (:use [seesaw core tree]))
  11. (def source "http://www.4clojure.com/problems/rss")
  12. ; Make a tree model for some XML using same arguments (branch? and childnre)
  13. ; as (clojure.core/tree-seq)
  14. (defn load-model []
  15. (simple-tree-model
  16. (complement string?)
  17. (comp seq :content)
  18. (clojure.xml/parse source)))
  19. ; A custom renderer so that XML elements are displayed nicely
  20. (defn render-fn [renderer info]
  21. (let [v (:value info)]
  22. (config! renderer
  23. :text (if (map? v)
  24. (format "<%s>" (name (:tag v)))
  25. v))))
  26. ; The rest is boilerplate ...
  27. (defn make-frame []
  28. (frame :title "JTree Example" :width 400 :height 400 :content
  29. (border-panel
  30. :north (str "From: " source)
  31. :center (scrollable (tree :id :tree
  32. :model (load-model)
  33. :renderer render-fn))
  34. :south (label :id :sel :text "Selection: "))))
  35. (defn app []
  36. (let [f (make-frame)]
  37. ; Listen for selection changes and show them in the label
  38. (listen (select f [:#tree])
  39. :tree-will-expand #(println (str "Tree will expand\n" %))
  40. :tree-will-collapse #(println (str "Tree will collapse\n" %))
  41. :tree-expanded #(println (str "Tree expanded\n" %))
  42. :tree-collapsed #(println (str "Tree collapsed\n" %))
  43. :selection
  44. (fn [e]
  45. (config! (select f [:#sel])
  46. :text (str "Selection: " (-> e selection first last)))))
  47. f))
  48. (defn -main [& args]
  49. (invoke-later (show! (app))))
  50. ;(-main)