view src/clojure/inspector.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line source
1 ; Copyright (c) Rich Hickey. 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 distribution.
5 ; By using this software in any fashion, you are agreeing to be bound by
6 ; the terms of this license.
7 ; You must not remove this notice, or any other, from this software.
9 (ns ^{:doc "Graphical object inspector for Clojure data structures."
10 :author "Rich Hickey"}
11 clojure.inspector
12 (:import
13 (java.awt BorderLayout)
14 (java.awt.event ActionEvent ActionListener)
15 (javax.swing.tree TreeModel)
16 (javax.swing.table TableModel AbstractTableModel)
17 (javax.swing JPanel JTree JTable JScrollPane JFrame JToolBar JButton SwingUtilities)))
19 (defn atom? [x]
20 (not (coll? x)))
22 (defn collection-tag [x]
23 (cond
24 (instance? java.util.Map$Entry x) :entry
25 (instance? java.util.Map x) :map
26 (sequential? x) :seq
27 :else :atom))
29 (defmulti is-leaf collection-tag)
30 (defmulti get-child (fn [parent index] (collection-tag parent)))
31 (defmulti get-child-count collection-tag)
33 (defmethod is-leaf :default [node]
34 (atom? node))
35 (defmethod get-child :default [parent index]
36 (nth parent index))
37 (defmethod get-child-count :default [parent]
38 (count parent))
40 (defmethod is-leaf :entry [e]
41 (is-leaf (val e)))
42 (defmethod get-child :entry [e index]
43 (get-child (val e) index))
44 (defmethod get-child-count :entry [e]
45 (count (val e)))
47 (defmethod is-leaf :map [m]
48 false)
49 (defmethod get-child :map [m index]
50 (nth (seq m) index))
52 (defn tree-model [data]
53 (proxy [TreeModel] []
54 (getRoot [] data)
55 (addTreeModelListener [treeModelListener])
56 (getChild [parent index]
57 (get-child parent index))
58 (getChildCount [parent]
59 (get-child-count parent))
60 (isLeaf [node]
61 (is-leaf node))
62 (valueForPathChanged [path newValue])
63 (getIndexOfChild [parent child]
64 -1)
65 (removeTreeModelListener [treeModelListener])))
68 (defn old-table-model [data]
69 (let [row1 (first data)
70 colcnt (count row1)
71 cnt (count data)
72 vals (if (map? row1) vals identity)]
73 (proxy [TableModel] []
74 (addTableModelListener [tableModelListener])
75 (getColumnClass [columnIndex] Object)
76 (getColumnCount [] colcnt)
77 (getColumnName [columnIndex]
78 (if (map? row1)
79 (name (nth (keys row1) columnIndex))
80 (str columnIndex)))
81 (getRowCount [] cnt)
82 (getValueAt [rowIndex columnIndex]
83 (nth (vals (nth data rowIndex)) columnIndex))
84 (isCellEditable [rowIndex columnIndex] false)
85 (removeTableModelListener [tableModelListener]))))
87 (defn inspect-tree
88 "creates a graphical (Swing) inspector on the supplied hierarchical data"
89 {:added "1.0"}
90 [data]
91 (doto (JFrame. "Clojure Inspector")
92 (.add (JScrollPane. (JTree. (tree-model data))))
93 (.setSize 400 600)
94 (.setVisible true)))
96 (defn inspect-table
97 "creates a graphical (Swing) inspector on the supplied regular
98 data, which must be a sequential data structure of data structures
99 of equal length"
100 {:added "1.0"}
101 [data]
102 (doto (JFrame. "Clojure Inspector")
103 (.add (JScrollPane. (JTable. (old-table-model data))))
104 (.setSize 400 600)
105 (.setVisible true)))
108 (defmulti list-provider class)
110 (defmethod list-provider :default [x]
111 {:nrows 1 :get-value (fn [i] x) :get-label (fn [i] (.getName (class x)))})
113 (defmethod list-provider java.util.List [c]
114 (let [v (if (vector? c) c (vec c))]
115 {:nrows (count v)
116 :get-value (fn [i] (v i))
117 :get-label (fn [i] i)}))
119 (defmethod list-provider java.util.Map [c]
120 (let [v (vec (sort (map (fn [[k v]] (vector k v)) c)))]
121 {:nrows (count v)
122 :get-value (fn [i] ((v i) 1))
123 :get-label (fn [i] ((v i) 0))}))
125 (defn list-model [provider]
126 (let [{:keys [nrows get-value get-label]} provider]
127 (proxy [AbstractTableModel] []
128 (getColumnCount [] 2)
129 (getRowCount [] nrows)
130 (getValueAt [rowIndex columnIndex]
131 (cond
132 (= 0 columnIndex) (get-label rowIndex)
133 (= 1 columnIndex) (print-str (get-value rowIndex)))))))
135 (defmulti table-model class)
137 (defmethod table-model :default [x]
138 (proxy [AbstractTableModel] []
139 (getColumnCount [] 2)
140 (getRowCount [] 1)
141 (getValueAt [rowIndex columnIndex]
142 (if (zero? columnIndex)
143 (class x)
144 x))))
146 ;(defn make-inspector [x]
147 ; (agent {:frame frame :data x :parent nil :index 0}))
150 (defn inspect
151 "creates a graphical (Swing) inspector on the supplied object"
152 {:added "1.0"}
153 [x]
154 (doto (JFrame. "Clojure Inspector")
155 (.add
156 (doto (JPanel. (BorderLayout.))
157 (.add (doto (JToolBar.)
158 (.add (JButton. "Back"))
159 (.addSeparator)
160 (.add (JButton. "List"))
161 (.add (JButton. "Table"))
162 (.add (JButton. "Bean"))
163 (.add (JButton. "Line"))
164 (.add (JButton. "Bar"))
165 (.addSeparator)
166 (.add (JButton. "Prev"))
167 (.add (JButton. "Next")))
168 BorderLayout/NORTH)
169 (.add
170 (JScrollPane.
171 (doto (JTable. (list-model (list-provider x)))
172 (.setAutoResizeMode JTable/AUTO_RESIZE_LAST_COLUMN)))
173 BorderLayout/CENTER)))
174 (.setSize 400 400)
175 (.setVisible true)))
178 (comment
180 (load-file "src/inspector.clj")
181 (refer 'inspector)
182 (inspect-tree {:a 1 :b 2 :c [1 2 3 {:d 4 :e 5 :f [6 7 8]}]})
183 (inspect-table [[1 2 3][4 5 6][7 8 9][10 11 12]])
185 )