view 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 source
1 ;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and
2 ;; distribution terms for this software are covered by the Eclipse Public
3 ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
4 ;; be found in the file epl-v10.html at the root of this distribution. By
5 ;; using this software in any fashion, you are agreeing to be bound by the
6 ;; terms of this license. You must not remove this notice, or any other,
7 ;; from this software.
8 ;;
9 ;; clojure.contrib.miglayout.test
10 ;;
11 ;; Test/example for clojure.contrib.miglayout
12 ;;
13 ;; scgilardi (gmail)
14 ;; Created 5 October 2008
16 (ns clojure.contrib.test-miglayout
17 (:import (javax.swing JButton JFrame JLabel JList JPanel
18 JScrollPane JTabbedPane JTextField JSeparator))
19 (:use clojure.contrib.miglayout))
21 (def tests)
23 (defn run-test
24 [index]
25 (let [panel ((tests index) (JPanel.))]
26 (println index (components panel))
27 (doto (JFrame. (format "MigLayout Test %d" index))
28 (.add panel)
29 (.pack)
30 (.setVisible true))))
32 (defn label
33 "Returns a swing label"
34 [text]
35 (JLabel. text))
37 (defn text-field
38 "Returns a swing text field"
39 ([] (text-field 10))
40 ([width]
41 (JTextField. width)))
43 (defn sep
44 "Returns a swing separator"
45 []
46 (JSeparator.))
48 (def tests [
50 (fn test0
51 [panel]
52 (miglayout panel
53 (label "Hello")
54 (label "World") {:gap :unrelated}
55 (text-field) :wrap
56 (label "Bonus!")
57 (JButton. "Bang it") {:wmin :button :grow :x :span 2} :center))
59 ;; test1 and test2 are based on code from
60 ;; http://www.devx.com/java/Article/38017/1954
62 ;; constraints as strings exclusively
63 (fn test1
64 [panel]
65 (miglayout panel
66 :column "[right]"
67 (label "General") "split, span"
68 (sep) "growx, wrap"
69 (label "Company") "gap 10"
70 (text-field "") "span, growx"
71 (label "Contact") "gap 10"
72 (text-field "") "span, growx, wrap"
73 (label "Propeller") "split, span, gaptop 10"
74 (sep) "growx, wrap, gaptop 10"
75 (label "PTI/kW") "gapx 10, gapy 15"
76 (text-field)
77 (label "Power/kW") "gap 10"
78 (text-field) "wrap"
79 (label "R/mm") "gap 10"
80 (text-field)
81 (label "D/mm") "gap 10"
82 (text-field)))
84 ;; the same constraints as strings, keywords, vectors, and maps
85 (fn test2
86 [panel]
87 (miglayout panel
88 :column "[right]"
89 (label "General") "split, span"
90 (sep) :growx :wrap
91 (label "Company") [:gap 10]
92 (text-field "") :span :growx
93 (label "Contact") [:gap 10]
94 (text-field "") :span :growx :wrap
95 (label "Propeller") :split :span [:gaptop 10]
96 (sep) :growx :wrap [:gaptop 10]
97 (label "PTI/kW") {:gapx 10 :gapy 15}
98 (text-field)
99 (label "Power/kW") [:gap 10]
100 (text-field) :wrap
101 (label "R/mm") [:gap 10]
102 (text-field)
103 (label "D/mm") [:gap 10]
104 (text-field)))
106 ;; the same constraints using symbols to name groups of constraints
107 (fn test3
108 [panel]
109 (let [g [:gap 10]
110 gt [:gaptop 10]
111 gxs #{:growx :span}
112 gxw #{:growx :wrap}
113 gxy {:gapx 10 :gapy 15}
114 right "[right]"
115 ss #{:split :span}
116 w :wrap]
117 (miglayout panel
118 :column right
119 (label "General") ss
120 (sep) gxw
121 (label "Company") g
122 (text-field "") gxs
123 (label "Contact") g
124 (text-field "") gxs
125 (label "Propeller") ss gt
126 (sep) gxw g
127 (label "PTI/kW") gxy
128 (text-field)
129 (label "Power/kW") g
130 (text-field) w
131 (label "R/mm") g
132 (text-field)
133 (label "D/mm") g
134 (text-field))))
136 (fn test4
137 [panel]
138 (miglayout panel
139 (label "First Name")
140 (text-field) {:id :firstname}
141 (label "Surname") [:gap :unrelated]
142 (text-field) {:id :surname} :wrap
143 (label "Address")
144 (text-field) {:id :address} :span :grow))
145 ])