diff src/clojure/contrib/test_contrib/test_miglayout.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/test_miglayout.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,145 @@
     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.test
    1.13 +;;
    1.14 +;;  Test/example for clojure.contrib.miglayout
    1.15 +;;
    1.16 +;;  scgilardi (gmail)
    1.17 +;;  Created 5 October 2008
    1.18 +
    1.19 +(ns clojure.contrib.test-miglayout
    1.20 +  (:import (javax.swing JButton JFrame JLabel JList JPanel
    1.21 +                        JScrollPane JTabbedPane JTextField JSeparator))
    1.22 +  (:use clojure.contrib.miglayout))
    1.23 +
    1.24 +(def tests)
    1.25 +
    1.26 +(defn run-test
    1.27 +  [index]
    1.28 +  (let [panel ((tests index) (JPanel.))]
    1.29 +    (println index (components panel))
    1.30 +    (doto (JFrame. (format "MigLayout Test %d" index))
    1.31 +      (.add panel)
    1.32 +      (.pack)
    1.33 +      (.setVisible true))))
    1.34 +
    1.35 +(defn label
    1.36 +  "Returns a swing label"
    1.37 +  [text]
    1.38 +  (JLabel. text))
    1.39 +
    1.40 +(defn text-field
    1.41 +  "Returns a swing text field"
    1.42 +  ([] (text-field 10))
    1.43 +  ([width]
    1.44 +     (JTextField. width)))
    1.45 +
    1.46 +(defn sep
    1.47 +  "Returns a swing separator"
    1.48 +  []
    1.49 +  (JSeparator.))
    1.50 +
    1.51 +(def tests [
    1.52 +
    1.53 +  (fn test0
    1.54 +    [panel]
    1.55 +    (miglayout panel
    1.56 +      (label "Hello")
    1.57 +      (label "World") {:gap :unrelated}
    1.58 +      (text-field) :wrap
    1.59 +      (label "Bonus!")
    1.60 +      (JButton. "Bang it") {:wmin :button :grow :x :span 2} :center))
    1.61 +
    1.62 +  ;; test1 and test2 are based on code from
    1.63 +  ;; http://www.devx.com/java/Article/38017/1954
    1.64 +
    1.65 +  ;; constraints as strings exclusively
    1.66 +  (fn test1
    1.67 +    [panel]
    1.68 +    (miglayout panel
    1.69 +      :column             "[right]"
    1.70 +      (label "General")   "split, span"
    1.71 +      (sep)               "growx, wrap"
    1.72 +      (label "Company")   "gap 10"
    1.73 +      (text-field "")     "span, growx"
    1.74 +      (label "Contact")   "gap 10"
    1.75 +      (text-field "")     "span, growx, wrap"
    1.76 +      (label "Propeller") "split, span, gaptop 10"
    1.77 +      (sep)               "growx, wrap, gaptop 10"
    1.78 +      (label "PTI/kW")    "gapx 10, gapy 15"
    1.79 +      (text-field)
    1.80 +      (label "Power/kW")  "gap 10"
    1.81 +      (text-field)        "wrap"
    1.82 +      (label "R/mm")      "gap 10"
    1.83 +      (text-field)
    1.84 +      (label "D/mm")      "gap 10"
    1.85 +      (text-field)))
    1.86 +
    1.87 +  ;; the same constraints as strings, keywords, vectors, and maps
    1.88 +  (fn test2
    1.89 +    [panel]
    1.90 +    (miglayout panel
    1.91 +      :column             "[right]"
    1.92 +      (label "General")   "split, span"
    1.93 +      (sep)               :growx :wrap
    1.94 +      (label "Company")   [:gap 10]
    1.95 +      (text-field "")     :span :growx
    1.96 +      (label "Contact")   [:gap 10]
    1.97 +      (text-field "")     :span :growx :wrap
    1.98 +      (label "Propeller") :split :span [:gaptop 10]
    1.99 +      (sep)               :growx :wrap [:gaptop 10]
   1.100 +      (label "PTI/kW")    {:gapx 10 :gapy 15}
   1.101 +      (text-field)
   1.102 +      (label "Power/kW")  [:gap 10]
   1.103 +      (text-field)        :wrap
   1.104 +      (label "R/mm")      [:gap 10]
   1.105 +      (text-field)
   1.106 +      (label "D/mm")      [:gap 10]
   1.107 +      (text-field)))
   1.108 +
   1.109 +  ;; the same constraints using symbols to name groups of constraints
   1.110 +  (fn test3
   1.111 +    [panel]
   1.112 +    (let [g [:gap 10]
   1.113 +          gt [:gaptop 10]
   1.114 +          gxs #{:growx :span}
   1.115 +          gxw #{:growx :wrap}
   1.116 +          gxy {:gapx 10 :gapy 15}
   1.117 +          right "[right]"
   1.118 +          ss #{:split :span}
   1.119 +          w :wrap]
   1.120 +      (miglayout panel
   1.121 +        :column             right
   1.122 +        (label "General")   ss
   1.123 +        (sep)               gxw
   1.124 +        (label "Company")   g
   1.125 +        (text-field "")     gxs
   1.126 +        (label "Contact")   g
   1.127 +        (text-field "")     gxs
   1.128 +        (label "Propeller") ss gt
   1.129 +        (sep)               gxw g
   1.130 +        (label "PTI/kW")    gxy
   1.131 +        (text-field)
   1.132 +        (label "Power/kW")  g
   1.133 +        (text-field)        w
   1.134 +        (label "R/mm")      g
   1.135 +        (text-field)
   1.136 +        (label "D/mm")      g
   1.137 +        (text-field))))
   1.138 +
   1.139 +  (fn test4
   1.140 +    [panel]
   1.141 +    (miglayout panel
   1.142 +      (label "First Name") 
   1.143 +      (text-field)        {:id :firstname}
   1.144 +      (label "Surname")   [:gap :unrelated]
   1.145 +      (text-field)        {:id :surname} :wrap
   1.146 +      (label "Address")
   1.147 +      (text-field)        {:id :address} :span :grow))
   1.148 +])