diff src/clojure/contrib/test_contrib/miglayout/example.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/test_contrib/miglayout/example.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,60 @@
     1.4 +;;  Copyright (c) Stephen C. Gilardi. All rights reserved.  The use and
     1.5 +;;  distribution terms for this software are covered by the Eclipse Public
     1.6 +;;  License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
     1.7 +;;  be found in the file epl-v10.html at the root of this distribution.  By
     1.8 +;;  using this software in any fashion, you are agreeing to be bound by the
     1.9 +;;  terms of this license.  You must not remove this notice, or any other,
    1.10 +;;  from this software.
    1.11 +;;
    1.12 +;;  clojure.contrib.miglayout.example
    1.13 +;;
    1.14 +;;  A temperature converter using miglayout. Demonstrates accessing
    1.15 +;;  components by their id constraint.
    1.16 +;;
    1.17 +;;  scgilardi (gmail)
    1.18 +;;  Created 31 May 2009
    1.19 +
    1.20 +(ns clojure.contrib.miglayout.example
    1.21 +  (:import (javax.swing JButton JFrame JLabel JPanel JTextField
    1.22 +                        SwingUtilities))
    1.23 +  (:use (clojure.contrib
    1.24 +         [miglayout :only (miglayout components)]
    1.25 +         [swing-utils :only (add-key-typed-listener)])))
    1.26 +
    1.27 +(defn fahrenheit
    1.28 +  "Converts a Celsius temperature to Fahrenheit. Input and output are
    1.29 +  strings. Returns \"input?\" if the input can't be parsed as a Double."
    1.30 +  [celsius]
    1.31 +  (try
    1.32 +   (format "%.2f" (+ 32 (* 1.8 (Double/parseDouble celsius))))
    1.33 +   (catch NumberFormatException _ "input?")))
    1.34 +
    1.35 +(defn- handle-key
    1.36 +  "Clears output on most keys, shows conversion on \"Enter\""
    1.37 +  [event out]
    1.38 +  (.setText out
    1.39 +    (if (= (.getKeyChar event) \newline)
    1.40 +      (fahrenheit (-> event .getComponent .getText))
    1.41 +      "")))
    1.42 +
    1.43 +(defn converter-ui
    1.44 +  "Lays out and shows a Temperature Converter UI"
    1.45 +  []
    1.46 +  (let [panel
    1.47 +        (miglayout (JPanel.)
    1.48 +         (JTextField. 6) {:id :input}
    1.49 +         (JLabel. "\u00b0Celsius") :wrap
    1.50 +         (JLabel.) {:id :output}
    1.51 +         (JLabel. "\u00b0Fahrenheit"))
    1.52 +        {:keys [input output]} (components panel)]
    1.53 +    (add-key-typed-listener input handle-key output)
    1.54 +    (doto (JFrame. "Temperature Converter")
    1.55 +      (.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)
    1.56 +      (.add panel)
    1.57 +      (.pack)
    1.58 +      (.setVisible true))))
    1.59 +
    1.60 +(defn main
    1.61 +  "Invokes converter-ui in the AWT Event thread"
    1.62 +  []
    1.63 +  (SwingUtilities/invokeLater converter-ui))