some refactoring

master
Abhinav Sarkar 2010-09-02 20:19:31 +05:30
parent 90393b0065
commit 0bb5911351
4 changed files with 41 additions and 33 deletions

View File

@ -53,6 +53,7 @@ hungry
hurt hurt
ill ill
infamous infamous
irritated
jealous jealous
lonely lonely
mad mad
@ -71,6 +72,7 @@ scary
shy shy
shitty shitty
sick sick
sinister
sleepy sleepy
sore sore
strange strange

View File

@ -62,6 +62,7 @@ lively
lovely lovely
lucky lucky
modern modern
nice
obedient obedient
open open
outgoing outgoing
@ -84,6 +85,7 @@ smiling
splendid splendid
successful successful
super super
sweet
thoughtful thoughtful
victorious victorious
vivacious vivacious

View File

@ -58,7 +58,8 @@
(lazy-seq (lazy-seq
(if-let [line (.readLine rdr)] (if-let [line (.readLine rdr)]
(cons line (this rdr)) (cons line (this rdr))
(.close rdr)))) (do (.close rdr)
(.. client getConnectionManager shutdown)))))
baseurl "http://stream.twitter.com/1/statuses/" baseurl "http://stream.twitter.com/1/statuses/"
url (str baseurl method ".json") url (str baseurl method ".json")
http-params http-params

View File

@ -6,8 +6,8 @@
JButton JOptionPane JButton JOptionPane
Timer WindowConstants UIManager] Timer WindowConstants UIManager]
[org.jfree.chart ChartFactory ChartPanel] [org.jfree.chart ChartFactory ChartPanel]
[org.jfree.chart.plot PiePlot]
[org.jfree.chart.labels StandardPieSectionLabelGenerator] [org.jfree.chart.labels StandardPieSectionLabelGenerator]
[org.jfree.chart.plot PiePlot]
[org.jfree.data.general DefaultPieDataset] [org.jfree.data.general DefaultPieDataset]
[org.jfree.data.time Millisecond TimeSeries TimeSeriesCollection] [org.jfree.data.time Millisecond TimeSeries TimeSeriesCollection]
[org.jfree.ui RefineryUtilities]) [org.jfree.ui RefineryUtilities])
@ -40,10 +40,12 @@
(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
respectively.
Returns the dialog. Returns the dialog.
Arguments are: Arguments are:
parent: the parent frame parent: the parent frame
dialog-title: the title of the dialog dialog-title: the title of the dialog
dialog-message: the message shown in the dialog dialog-message: the message shown in the dialog
@ -72,12 +74,28 @@
^String username-lbl-text ^String password-lbl-text input-field-size ^String username-lbl-text ^String password-lbl-text input-field-size
^String ok-btn-text ^String cancel-btn-text ^String ok-btn-text ^String cancel-btn-text
validation-fn ok-fn cancel-fn] validation-fn ok-fn cancel-fn]
(let [username-input (JTextField. (int input-field-size)) (let [dialog (JDialog. parent dialog-title true)
username-input (JTextField. (int input-field-size))
password-input (JPasswordField. (int input-field-size)) password-input (JPasswordField. (int input-field-size))
validation-msg-lbl (JLabel. " ") validation-msg-lbl (JLabel. " ")
ok-btn (JButton. ok-btn-text) ok-btn
cancel-btn (JButton. cancel-btn-text) (doto (JButton. ok-btn-text)
dialog (JDialog. parent dialog-title true)] (add-action-listener
(fn [_]
(let [username (.getText username-input)
password (.getText password-input)]
(if-let [validation-msg
(validation-fn username password dialog)]
(.setText validation-msg-lbl validation-msg)
(do (.setText validation-msg-lbl " ")
(.setVisible dialog false)
(ok-fn username password dialog)))))))
cancel-btn
(doto (JButton. cancel-btn-text)
(add-action-listener
(fn [_]
(.setVisible dialog false)
(cancel-fn dialog))))]
(doseq [^JTextField in [username-input password-input]] (doseq [^JTextField in [username-input password-input]]
(.addKeyListener in (.addKeyListener in
(proxy [KeyAdapter] [] (proxy [KeyAdapter] []
@ -96,32 +114,18 @@
(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"}
(miglayout (JPanel.) (miglayout (JPanel.) ok-btn cancel-btn) {:span 2 :align "center"}
(doto ok-btn ))
(add-action-listener
(fn [e]
(let [username (.getText username-input)
password (.getText password-input)]
(if-let [validation-msg
(validation-fn username password dialog)]
(.setText validation-msg-lbl validation-msg)
(do (.setText validation-msg-lbl " ")
(.setVisible dialog false)
(ok-fn username password dialog)))))))
(doto cancel-btn
(add-action-listener
(fn [e]
(.setVisible dialog false)
(cancel-fn dialog)))))
{:span 2 :align "center"}))
(.setSize dialog-width dialog-height)))) (.setSize dialog-width dialog-height))))
(defn init-gui [adjective-map] (defn init-gui [adjective-map]
(let [frame (JFrame. "Twitter Feelings") (let [frame (JFrame. "Twitter Feelings")
adjective-types (sort (keys @adjective-type-count))
^DefaultPieDataset pie-dataset ^DefaultPieDataset pie-dataset
(reduce #(do (.setValue ^DefaultPieDataset %1 ^String %2 0) %1) (reduce
(DefaultPieDataset.) (sort (keys @adjective-type-count))) (fn [^DefaultPieDataset ds ^String key] (doto ds (.setValue key 0)))
(DefaultPieDataset.) adjective-types)
pie-chart (ChartFactory/createPieChart pie-chart (ChartFactory/createPieChart
"Distribution" pie-dataset true false false) "Distribution" pie-dataset true false false)
pie-chart-panel (doto (ChartPanel. pie-chart) pie-chart-panel (doto (ChartPanel. pie-chart)
@ -129,10 +133,9 @@
time-series-map time-series-map
(into (sorted-map) (into (sorted-map)
(map #(vector % (TimeSeries. % Millisecond)) (map #(vector % (TimeSeries. % Millisecond)) adjective-types))
(keys @adjective-type-count)))
time-series-dataset time-series-dataset
(reduce #(do (.addSeries ^TimeSeriesCollection %1 %2) %1) (reduce #(doto ^TimeSeriesCollection %1 (.addSeries %2))
(TimeSeriesCollection.) (vals time-series-map)) (TimeSeriesCollection.) (vals time-series-map))
time-series-chart time-series-chart
(ChartFactory/createTimeSeriesChart (ChartFactory/createTimeSeriesChart
@ -161,7 +164,7 @@
"Credentials" "Input your Twitter credentials" 220 150 "Credentials" "Input your Twitter credentials" 220 150
"Screen Name" "Password" 20 "Screen Name" "Password" 20
"OK" "Cancel" "OK" "Cancel"
(fn [uname pass dialog] (fn [uname pass _]
(when (or (empty? uname) (empty? pass)) (when (or (empty? uname) (empty? pass))
(str "Please input Screen Name and Password"))) (str "Please input Screen Name and Password")))
(fn [uname pass ^JDialog dialog] (fn [uname pass ^JDialog dialog]
@ -178,7 +181,7 @@
"Error" :error) "Error" :error)
(.setVisible dialog true))))) (.setVisible dialog true)))))
(.start timer)) (.start timer))
(fn [dialog] (exit-app frame)))] (fn [_] (exit-app frame)))]
(add-watch adjective-seen :adjective-lbl (add-watch adjective-seen :adjective-lbl
(fn [_ _ _ n] (fn [_ _ _ n]
(do-swing (do-swing