Permalink
Browse files

add timer.

  • Loading branch information...
1 parent 1734f2e commit 78d3ef937c9f8f5dc79cb8148b181d896b9aef94 @reniclin committed Dec 17, 2012
Showing with 88 additions and 18 deletions.
  1. +3 −1 project.clj
  2. BIN resources/clock-icon.png
  3. +85 −10 src/do_alert/core.clj
  4. +0 −7 test/do_alert/core_test.clj
View
@@ -4,5 +4,7 @@
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.4.0"]
- [seesaw "1.4.2"]]
+ ;; [seesaw "1.4.2"]
+ ]
+ :plugins [[jonase/eastwood "0.0.2"]]
:main do-alert.core)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -1,10 +1,85 @@
-(ns do-alert.core
- (:use seesaw.core))
-
-(defn -main [& args]
- (invoke-later
- (-> (frame :title "do alert",
- :content "Hello, Seesaw",
- :on-close :exit)
- pack!
- show!)))
+(ns do-alert.core)
+
+(import '(javax.swing JOptionPane))
+(import '(java.awt Image MenuItem PopupMenu SystemTray Toolkit TrayIcon))
+(import '(java.awt.event ActionEvent ActionListener))
+
+;; utils -----------------------------------------------------------------------
+(defn parse-int [s]
+ (Integer. (re-find #"\d+" s)))
+
+(defn now []
+ (System/currentTimeMillis))
+
+(defn str-to-time [time-str]
+ (let [hour-min (clojure.string/split time-str #":")
+ date (java.util.Date.)]
+ (doto date
+ (.setHours (parse-int (first hour-min)))
+ (.setMinutes (parse-int (second hour-min))))
+ (.getTime date)))
+
+(defn count-interval [end-time]
+ (- end-time (now)))
+
+(defn read-from-file [file-path]
+ (line-seq (clojure.java.io/reader (clojure.java.io/file file-path))))
+
+;; menu ------------------------------------------------------------------------
+(def menu (PopupMenu.))
+
+(defn load-image [path]
+ (.getImage (Toolkit/getDefaultToolkit) (clojure.java.io/resource path)))
+
+(defn create-icon [img tip auto-size]
+ (let [icon (TrayIcon. img tip menu)]
+ (.setImageAutoSize icon auto-size)
+ icon))
+
+;; item ------------------------------------------------------------------------
+(defn create-item [label & [call-back]]
+ (let [item (MenuItem. label)]
+ (if (not= call-back nil)
+ (.addActionListener item
+ (reify ActionListener
+ (actionPerformed [this evt]
+ (call-back)))))
+ item))
+
+(defn create-time-item [label interval call-back]
+ (let [item (MenuItem. label)
+ timer (javax.swing.Timer.
+ interval
+ (reify ActionListener (actionPerformed [this evt] (call-back))))]
+ (println (str "interval: " interval))
+ (.start timer)
+ item))
+
+(defn add-item [item]
+ (.add menu item))
+
+(defn read-tasks []
+ (doseq [line (read-from-file "/Users/reniclin/test.txt")]
+ (let [time-str (first (clojure.string/split line #"\s+"))
+ task-str (second (clojure.string/split line #"\s+"))
+ interval (count-interval (str-to-time time-str))]
+ (if (> interval 0)
+ (add-item
+ (create-time-item
+ line
+ interval
+ #(do (println "call-back called!")
+ (JOptionPane/showMessageDialog nil, (str "It's time to " task-str)))))))))
+
+(defn add-close []
+ (add-item (create-item "close" #((System/exit 0)))))
+
+;; main ------------------------------------------------------------------------
+(defn init-tray []
+ (let [tray (SystemTray/getSystemTray)]
+ (.add tray (create-icon (load-image "clock-icon.png") "SystemTray Demo" true))))
+
+(defn -main []
+ (init-tray)
+ (read-tasks)
+ (add-close))
@@ -1,7 +0,0 @@
-(ns do-alert.core-test
- (:use clojure.test
- do-alert.core))
-
-(deftest a-test
- (testing "FIXME, I fail."
- (is (= 0 1))))

0 comments on commit 78d3ef9

Please sign in to comment.