On Wed, Oct 8, 2008 at 5:37 PM, Bob <[EMAIL PROTECTED]> wrote:
> Does anyone have a non-trivial example of a Swing program done the
> Clojure way?

This probably only counts as trivial, but it's what I've got, so I
hope it helps a little.  I wrote it up for a talk I gave, so it can be
run a demo, dropping each top-level form into a REPL one at a time,
watching the GUI change as you go.  I hope it helps a little...

I start my REPL like this:
rlwrap java -server -cp
~/build/clojure/clojure.jar:/usr/share/java/postgresql-jdbc3-8.2.jar
-Djdbc.drivers=org.postgresql.Driver clojure.lang.Repl

Then I paste into it each of these forms:

(import '(javax.swing JFrame JPanel JScrollPane JTable SwingUtilities)
        '(javax.swing.table AbstractTableModel DefaultTableCellRenderer)
        '(java.awt Dimension GridLayout Font Color)
        '(java.awt.event MouseAdapter)
        '(java.sql DriverManager))

(def table (JTable.))

(. SwingUtilities invokeLater
   (fn []
       (doto (JFrame. "demo")
             (setDefaultCloseOperation (. JFrame EXIT_ON_CLOSE))
             (setContentPane
               (doto (JPanel. (GridLayout. 1 0))
                     (setOpaque true)
                     (add (JScrollPane.
                            (doto table
                                  (setPreferredScrollableViewportSize
                                    (Dimension. 500 70))
                                  (setFillsViewportHeight true))))))
             (pack)
             (setVisible true))))

; at this point you should have a window

; modify this line based on your database server, db name, password, etc:
(def conn
  (. DriverManager getConnection "jdbc:postgresql:mydb" "postgres" "system"))

(defn query-vec [sql]
  (with-open stmt (.createStatement conn)
     (vec (resultset-seq (.executeQuery stmt sql)))))

; modify this line based on your table name:
(def rows (query-vec "SELECT * FROM LogB"))

(defn model [rows col-names value-at]
      (proxy [AbstractTableModel] []
             (getRowCount []    (count rows))
             (getColumnCount [] (count col-names))
             (getColumnName [c] (nth col-names c))
             (getValueAt [r c]  (value-at r c))
             (isCellEditable [r c] false)))

(.setModel table (model rows
                        ["foo" "bar" "baz"]
                        (fn [r c] (str r ", " c))))

; at this point you should have a fairly meaningless grid

(.setModel table (model rows
                        ["record"]
                        (fn [r c] (prn-str (nth rows r)))))

(.setModel table (model rows
                        (vec (map str (keys (first rows))))
                        (fn [r c] ((nth rows r) (nth (keys (first rows)) c)))))

; modify this based on the actual columns in your table:
(.setModel table (model rows
                        ["Time" "Source" "Message"]
                        (fn [r c] (let [row (nth rows r)]
                                    ((nth [#(:createtime row)
                                           #(str (:path row) ":" (:line row))
                                           #(:buffer row)] c))))))

(.setDefaultRenderer table Object
  (proxy [DefaultTableCellRenderer] []
    (getTableCellRendererComponent [tbl obj isSelected hasFocus r c]
      (let [{:keys [level]} (nth rows r)]
        (doto this
              (setForeground (cond (<= level  0) (. Color white)
                                   (<= level 10) (. Color blue)
                                   (<= level 20) (. Color red)
                                   (<= level 30) (. Color magenta)
                                   :else         (. Color black)))
              (setText (str obj)))))))

(.addMouseListener table
  (proxy [MouseAdapter] []
    (mouseClicked [e]
      (when (== (.getClickCount e) 1)
        (let [p (.getPoint e)
              r (.rowAtPoint table p)
              c (.columnAtPoint table p)]
          (prn "click row " r)
          (flush))))))

--Chouser

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to