Switch branches/tags
clojure-1.10.0-alpha4 clojure-1.10.0-alpha3 clojure-1.10.0-alpha2 clojure-1.10.0-alpha1 clojure-1.9.0 clojure-1.9.0-beta4 clojure-1.9.0-beta3 clojure-1.9.0-beta2 clojure-1.9.0-beta1 clojure-1.9.0-alpha20 clojure-1.9.0-alpha19 clojure-1.9.0-alpha18 clojure-1.9.0-alpha17 clojure-1.9.0-alpha16 clojure-1.9.0-alpha15 clojure-1.9.0-alpha14 clojure-1.9.0-alpha13 clojure-1.9.0-alpha12 clojure-1.9.0-alpha11 clojure-1.9.0-alpha10 clojure-1.9.0-alpha9 clojure-1.9.0-alpha8 clojure-1.9.0-alpha7 clojure-1.9.0-alpha6 clojure-1.9.0-alpha5 clojure-1.9.0-alpha4 clojure-1.9.0-alpha3 clojure-1.9.0-alpha2 clojure-1.9.0-alpha1 clojure-1.9.0-RC2 clojure-1.9.0-RC1 clojure-1.8.0 clojure-1.8.0-beta2 clojure-1.8.0-beta1 clojure-1.8.0-alpha5 clojure-1.8.0-alpha4 clojure-1.8.0-alpha3 clojure-1.8.0-alpha2 clojure-1.8.0-alpha1 clojure-1.8.0-RC5 clojure-1.8.0-RC4 clojure-1.8.0-RC3 clojure-1.8.0-RC2 clojure-1.8.0-RC1 clojure-1.7.0 clojure-1.7.0-beta3 clojure-1.7.0-beta2 clojure-1.7.0-beta1 clojure-1.7.0-alpha6 clojure-1.7.0-alpha5 clojure-1.7.0-alpha4 clojure-1.7.0-alpha3 clojure-1.7.0-alpha2 clojure-1.7.0-alpha1 clojure-1.7.0-RC2 clojure-1.7.0-RC1 clojure-1.6.0 clojure-1.6.0-beta2 clojure-1.6.0-beta1 clojure-1.6.0-alpha3 clojure-1.6.0-alpha2 clojure-1.6.0-alpha1 clojure-1.6.0-RC4 clojure-1.6.0-RC3 clojure-1.6.0-RC2 clojure-1.6.0-RC1 clojure-1.5.1 clojure-1.5.0 clojure-1.5.0-beta13 clojure-1.5.0-beta12 clojure-1.5.0-beta11 clojure-1.5.0-beta10 clojure-1.5.0-beta9 clojure-1.5.0-beta8 clojure-1.5.0-beta7 clojure-1.5.0-beta2 clojure-1.5.0-beta1 clojure-1.5.0-alpha7 clojure-1.5.0-alpha6 clojure-1.5.0-alpha5 clojure-1.5.0-alpha4 clojure-1.5.0-alpha3 clojure-1.5.0-alpha2 clojure-1.5.0-alpha1 clojure-1.5.0-RC17 clojure-1.5.0-RC16 clojure-1.5.0-RC15 clojure-1.5.0-RC14 clojure-1.5.0-RC6 clojure-1.5.0-RC5 clojure-1.5.0-RC4 clojure-1.5.0-RC3 clojure-1.5.0-RC2 clojure-1.5.0-RC1 clojure-1.4.0 clojure-1.4.0-beta7 clojure-1.4.0-beta6 clojure-1.4.0-beta5 clojure-1.4.0-beta4 clojure-1.4.0-beta3
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
186 lines (158 sloc) 5.45 KB
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns ^{:doc "Graphical object inspector for Clojure data structures."
:author "Rich Hickey"}
(java.awt BorderLayout)
(java.awt.event ActionEvent ActionListener)
(javax.swing.tree TreeModel)
(javax.swing.table TableModel AbstractTableModel)
(javax.swing JPanel JTree JTable JScrollPane JFrame JToolBar JButton SwingUtilities)))
(defn atom? [x]
(not (coll? x)))
(defn collection-tag [x]
(instance? java.util.Map$Entry x) :entry
(instance? java.util.Map x) :map
(sequential? x) :seq
:else :atom))
(defmulti is-leaf collection-tag)
(defmulti get-child (fn [parent index] (collection-tag parent)))
(defmulti get-child-count collection-tag)
(defmethod is-leaf :default [node]
(atom? node))
(defmethod get-child :default [parent index]
(nth parent index))
(defmethod get-child-count :default [parent]
(count parent))
(defmethod is-leaf :entry [e]
(is-leaf (val e)))
(defmethod get-child :entry [e index]
(get-child (val e) index))
(defmethod get-child-count :entry [e]
(count (val e)))
(defmethod is-leaf :map [m]
(defmethod get-child :map [m index]
(nth (seq m) index))
(defn tree-model [data]
(proxy [TreeModel] []
(getRoot [] data)
(addTreeModelListener [treeModelListener])
(getChild [parent index]
(get-child parent index))
(getChildCount [parent]
(get-child-count parent))
(isLeaf [node]
(is-leaf node))
(valueForPathChanged [path newValue])
(getIndexOfChild [parent child]
(removeTreeModelListener [treeModelListener])))
(defn old-table-model [data]
(let [row1 (first data)
colcnt (count row1)
cnt (count data)
vals (if (map? row1) vals identity)]
(proxy [TableModel] []
(addTableModelListener [tableModelListener])
(getColumnClass [columnIndex] Object)
(getColumnCount [] colcnt)
(getColumnName [columnIndex]
(if (map? row1)
(name (nth (keys row1) columnIndex))
(str columnIndex)))
(getRowCount [] cnt)
(getValueAt [rowIndex columnIndex]
(nth (vals (nth data rowIndex)) columnIndex))
(isCellEditable [rowIndex columnIndex] false)
(removeTableModelListener [tableModelListener]))))
(defn inspect-tree
"creates a graphical (Swing) inspector on the supplied hierarchical data"
{:added "1.0"}
(doto (JFrame. "Clojure Inspector")
(.add (JScrollPane. (JTree. (tree-model data))))
(.setSize 400 600)
(.setVisible true)))
(defn inspect-table
"creates a graphical (Swing) inspector on the supplied regular
data, which must be a sequential data structure of data structures
of equal length"
{:added "1.0"}
(doto (JFrame. "Clojure Inspector")
(.add (JScrollPane. (JTable. (old-table-model data))))
(.setSize 400 600)
(.setVisible true)))
(defmulti list-provider class)
(defmethod list-provider :default [x]
{:nrows 1 :get-value (fn [i] x) :get-label (fn [i] (.getName (class x)))})
(defmethod list-provider java.util.List [c]
(let [v (if (vector? c) c (vec c))]
{:nrows (count v)
:get-value (fn [i] (v i))
:get-label (fn [i] i)}))
(defmethod list-provider java.util.Map [c]
(let [v (vec (sort (map (fn [[k v]] (vector k v)) c)))]
{:nrows (count v)
:get-value (fn [i] ((v i) 1))
:get-label (fn [i] ((v i) 0))}))
(defn list-model [provider]
(let [{:keys [nrows get-value get-label]} provider]
(proxy [AbstractTableModel] []
(getColumnCount [] 2)
(getRowCount [] nrows)
(getValueAt [rowIndex columnIndex]
(= 0 columnIndex) (get-label rowIndex)
(= 1 columnIndex) (print-str (get-value rowIndex)))))))
(defmulti table-model class)
(defmethod table-model :default [x]
(proxy [AbstractTableModel] []
(getColumnCount [] 2)
(getRowCount [] 1)
(getValueAt [rowIndex columnIndex]
(if (zero? columnIndex)
(class x)
;(defn make-inspector [x]
; (agent {:frame frame :data x :parent nil :index 0}))
(defn inspect
"creates a graphical (Swing) inspector on the supplied object"
{:added "1.0"}
(doto (JFrame. "Clojure Inspector")
(doto (JPanel. (BorderLayout.))
(.add (doto (JToolBar.)
(.add (JButton. "Back"))
(.add (JButton. "List"))
(.add (JButton. "Table"))
(.add (JButton. "Bean"))
(.add (JButton. "Line"))
(.add (JButton. "Bar"))
(.add (JButton. "Prev"))
(.add (JButton. "Next")))
(doto (JTable. (list-model (list-provider x)))
(.setAutoResizeMode JTable/AUTO_RESIZE_LAST_COLUMN)))
(.setSize 400 400)
(.setVisible true)))
(load-file "src/inspector.clj")
(refer 'inspector)
(inspect-tree {:a 1 :b 2 :c [1 2 3 {:d 4 :e 5 :f [6 7 8]}]})
(inspect-table [[1 2 3][4 5 6][7 8 9][10 11 12]])