added comments; some refactoring/reformatting

This commit is contained in:
Abhinav Sarkar 2010-11-04 10:52:12 +05:30
parent 63635873ca
commit f8a12ec76a

View File

@ -3,8 +3,7 @@
[java.awt.event KeyEvent KeyAdapter] [java.awt.event KeyEvent KeyAdapter]
[javax.imageio ImageIO] [javax.imageio ImageIO]
[javax.swing JPanel JFrame JLabel JDialog JTextField JPasswordField [javax.swing JPanel JFrame JLabel JDialog JTextField JPasswordField
JButton JOptionPane JButton JOptionPane Timer WindowConstants UIManager]
Timer WindowConstants UIManager]
[org.jfree.chart ChartFactory ChartPanel] [org.jfree.chart ChartFactory ChartPanel]
[org.jfree.chart.labels StandardPieSectionLabelGenerator] [org.jfree.chart.labels StandardPieSectionLabelGenerator]
[org.jfree.chart.plot PiePlot] [org.jfree.chart.plot PiePlot]
@ -25,12 +24,12 @@
"org.jvnet.substance.skin.SubstanceModerateLookAndFeel") "org.jvnet.substance.skin.SubstanceModerateLookAndFeel")
(let [message-type { (let [message-type {
:error JOptionPane/ERROR_MESSAGE :error JOptionPane/ERROR_MESSAGE
:info JOptionPane/INFORMATION_MESSAGE :info JOptionPane/INFORMATION_MESSAGE
:warn JOptionPane/WARNING_MESSAGE :warn JOptionPane/WARNING_MESSAGE
:question JOptionPane/QUESTION_MESSAGE :question JOptionPane/QUESTION_MESSAGE
:plain JOptionPane/PLAIN_MESSAGE :plain JOptionPane/PLAIN_MESSAGE
}] }]
(defn show-message [frame message title type] (defn show-message [frame message title type]
(JOptionPane/showMessageDialog frame message title (type message-type)))) (JOptionPane/showMessageDialog frame message title (type message-type))))
@ -40,7 +39,7 @@
(defn create-auth-input-dialog (defn create-auth-input-dialog
"Creates a JDialog to take the input of username and password from the user. "Creates a JDialog to take the input of username and password from the user.
Supports <ENTER> and <ESCAPE> keys for OKing and Cancelling the input Supports the <ENTER> and <ESCAPE> keys for OKing and Cancelling the dialog
respectively. respectively.
Returns the dialog. Returns the dialog.
@ -61,13 +60,16 @@
the function is called with arguments: username, password, this dialog. the function is called with arguments: username, password, this dialog.
if the function return a string, it is shown on the dialog as the error if the function return a string, it is shown on the dialog as the error
message and the dialog remains visible. otherwise ok-fn is called. message and the dialog remains visible. otherwise ok-fn is called.
this function is called in the swing event thread.
ok-fn: a function which is called when the user presses ok button and the ok-fn: a function which is called when the user presses ok button and the
input is valid as per the call to validation-fn. input is valid as per the call to validation-fn.
the function is called with arguments: username, password, this dialog. the function is called with arguments: username, password, this dialog.
the dialog is hidden before the call. the dialog is hidden before the call.
this function is called in the swing event thread.
cancel-fn: a function which is called when the user presses cancel button. cancel-fn: a function which is called when the user presses cancel button.
the function is called with arguments: this dialog. the function is called with arguments: this dialog.
the dialog is hidden before the call. the dialog is hidden before the call.
this function is called in the swing event thread.
" "
[^JFrame parent [^JFrame parent
^String dialog-title ^String dialog-message dialog-width dialog-height ^String dialog-title ^String dialog-message dialog-width dialog-height
@ -110,7 +112,7 @@
(.setContentPane (.setContentPane
(miglayout (JPanel.) (miglayout (JPanel.)
:layout {:wrap 2} :layout {:wrap 2}
(JLabel. dialog-message) {:span 2} (JLabel. dialog-message) {:span 2 :align "center"}
(JLabel. username-lbl-text) username-input (JLabel. username-lbl-text) username-input
(JLabel. password-lbl-text) password-input (JLabel. password-lbl-text) password-input
validation-msg-lbl {:span 2 :align "center"} validation-msg-lbl {:span 2 :align "center"}
@ -126,6 +128,7 @@
(.setResizable false)) (.setResizable false))
adjective-types (sort (keys @adjective-type-count)) adjective-types (sort (keys @adjective-type-count))
^DefaultPieDataset pie-dataset ^DefaultPieDataset pie-dataset
(reduce (reduce
(fn [^DefaultPieDataset ds ^String key] (doto ds (.setValue key 0))) (fn [^DefaultPieDataset ds ^String key] (doto ds (.setValue key 0)))
@ -155,7 +158,7 @@
timer (doto (Timer. 1000 nil) timer (doto (Timer. 1000 nil)
(add-action-listener (add-action-listener
(fn [e] (fn [_]
(let [a-count @adjective-type-count (let [a-count @adjective-type-count
total (reduce + 0 (vals a-count))] total (reduce + 0 (vals a-count))]
(doseq [[^String k v] a-count] (doseq [[^String k v] a-count]
@ -165,13 +168,13 @@
^JDialog auth-input-dialog ^JDialog auth-input-dialog
(create-auth-input-dialog frame (create-auth-input-dialog frame
"Credentials" "Input your Twitter credentials" 220 150 "Credentials" "Input your Twitter credentials" 250 150
"Screen Name" "Password" 20 "Screen Name" "Password" 20
"OK" "Cancel" "OK" "Cancel"
;validation-fn ;validation-fn
(fn [uname pass _] (fn [uname pass _]
(when (or (empty? uname) (empty? pass)) (when (or (empty? uname) (empty? pass))
(str "Please input Screen Name and Password"))) (str "Screen Name and Password cannot be empty!")))
;ok-fn ;ok-fn
(fn [uname pass ^JDialog dialog] (fn [uname pass ^JDialog dialog]
(future (future
@ -181,6 +184,7 @@
[(adjective-processor adjective-map) (status-processor)]) [(adjective-processor adjective-map) (status-processor)])
(catch Exception e (catch Exception e
(do-swing (do-swing
(.stop timer)
(show-message frame (show-message frame
(str "Error happened: " (.getMessage e) (str "Error happened: " (.getMessage e)
".\nPlease restart.") ".\nPlease restart.")
@ -192,11 +196,11 @@
;add watches ;add watches
(add-watch adjective-seen :adjective-lbl (add-watch adjective-seen :adjective-lbl
(fn [_ _ _ n] (fn [_ _ _ adj]
(do-swing (do-swing
(.setText adjective-lbl (str "<html><h2>" n "</h2></html>"))))) (.setText adjective-lbl (str "<html><h2>" adj "</h2></html>")))))
(add-watch status-seen :status-lbl (add-watch status-seen :status-lbl
(fn [_ _ _ n] (do-swing (.setText status-lbl n)))) (fn [_ _ _ sts] (do-swing (.setText status-lbl sts))))
;do some configuration of the plots ;do some configuration of the plots
(doto ^PiePlot (.getPlot pie-chart) (doto ^PiePlot (.getPlot pie-chart)
@ -215,9 +219,9 @@
(doto frame (doto frame
(.setContentPane (.setContentPane
(miglayout (JPanel.) (miglayout (JPanel.)
:layout [:wrap 1] :layout {:wrap 1}
(miglayout (JPanel.) (miglayout (JPanel.)
:layout [:wrap 2] :layout {:wrap 2}
(JLabel. "<html><h1>How is Twitter feeling now?</h1></html>") (JLabel. "<html><h1>How is Twitter feeling now?</h1></html>")
{:align "left"} {:align "left"}
adjective-lbl {:align "right"} adjective-lbl {:align "right"}