    )
   
   (:import
-   (java.awt.BorderLayout)
-   (javax.swing JEditorPane JScrollPane JPanel SwingUtilities)
+   (java.awt Color BorderLayout Dimension)
+   (javax.swing JEditorPane JFrame JScrollPane JPanel SwingUtilities AbstractAction)
+   (javax.swing.border LineBorder)
    )
   
   (:require [clojure.main :as r])

    (fn [pair] (-> pair set (filter string) count (mod 2) zero?))
    pairs)))

(comment
  (defn repl []
    (let [group (sg-group)
          history (javax.swing.JEditorPane.)
          input (javax.swing.JEditorPane.)
          scroll (JScrollPane. history)

          println-history #(EDT (let [ doc (.getDocument history)
                                      length (.getLength doc)]
                                  (.insertString doc length % nil)))

          bb (CircularByteBuffer.)
          ps (java.io.PrintStream. (.getOutputStream bb) true)
          cr (ConsoleReader. (.getInputStream bb) (PrintWriter. (System/out)))
          ]

      (EDT (.setEditable input false)   
           (.setEditable history false)
           (.setVerticalScrollBarPolicy scroll (JScrollPane/VERTICAL_SCROLLBAR_ALWAYS))
           (.setHorizontalScrollBarPolicy scroll (JScrollPane/HORIZONTAL_SCROLLBAR_NEVER)))
    
      (doto group
        (add! (sg-component scroll 500 300))
        (add! (translate 0 305 (sg-component input 500 25))))

      (doto (.getKeymap input)
        (.addActionForKeyStroke
         (javax.swing.KeyStroke/getKeyStroke (java.awt.event.KeyEvent/VK_ENTER) 0)
         (proxy [javax.swing.AbstractAction] [] (actionPerformed [evt]  (let [text (.getText input)]
                                                                          (if (and (not (empty? text))
                                                                                   (balanced? text))
                                                                            (do (event ::repl-write :text text)
                                                                                (EDT
                                                                                 (.setText input "")
                                                                                 (.requestFocus input)))))))))

     
     
      (on ::repl-print #(println-history (:text %) ))
    
      (on ::repl-ready #((println-history "repl ready\r\n")
                         (event ::repl-write :text "(in-ns 'user)\r\n(use 'overtone.live)")
                         (EDT (.setEditable input true))))

      (on ::repl-exited #((println-history "repl exited\r\n")
                          (EDT (.setEditable input false))))
    
      (if (repl-thread-running?)
        (do (EDT (.setEditable input true))
            (println-history "repl thread allready running\r\n"))
        (start-repl-thread))
      
      group)))


(defn repl-panel []
  (let [panel (javax.swing.JPanel.)

        history (javax.swing.JEditorPane.)
        input (javax.swing.JEditorPane.)
        scroll (javax.swing.JScrollPane. history)

        println-history #(in-swing (let [ doc (.getDocument history)
                                         length (.getLength doc)]
                                     (.insertString doc length % nil)))
        
        ]



    panel))
 

  (:use [clojure.stacktrace :only (e)])
    
  (:use (overtone.core time-utils sc ugen synth synthdef envelope event)
        (overtone.gui swing)
        (overtone.music rhythm pitch tuning))

  (:use clj-scenegraph.core))
