diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/clojure/contrib/miglayout.clj | 6 | ||||
-rw-r--r-- | src/clojure/contrib/miglayout/internal.clj | 16 | ||||
-rw-r--r-- | src/clojure/contrib/miglayout/test.clj | 134 |
3 files changed, 111 insertions, 45 deletions
diff --git a/src/clojure/contrib/miglayout.clj b/src/clojure/contrib/miglayout.clj index 13ed92ef..e841a450 100644 --- a/src/clojure/contrib/miglayout.clj +++ b/src/clojure/contrib/miglayout.clj @@ -42,14 +42,16 @@ :column or :row. Constraints for a keyword item affect the entire layout. - Constraint: string, keyword, vector, or map + Constraint: string, keyword, vector, map, or set - A string specifies one or more constraints each with zero or more arguments. - A keyword specifies a single constraint without arguments - A vector specifies a single constraint with one or more arguments - A map specifies one or more constraints as keys, each mapped to a - single argument" + single argument + - A set groups two or more constraints, each a string, keyword, + vector, map, or set" [#^Container container & args] (let [item-constraints (apply parse-item-constraints args) {:keys [keywords components]} item-constraints diff --git a/src/clojure/contrib/miglayout/internal.clj b/src/clojure/contrib/miglayout/internal.clj index 00be8785..77476086 100644 --- a/src/clojure/contrib/miglayout/internal.clj +++ b/src/clojure/contrib/miglayout/internal.clj @@ -17,10 +17,12 @@ (:import (java.awt Component)) (:use (clojure.contrib except fcase))) +(declare format-constraints) + (defn format-constraint "Returns a vector of vectors representing one or more constraints separated by commas. Constraints may be specified in Clojure using - strings, keywords, vectors, and/or maps." + strings, keywords, vectors, maps, and/or sets." [c] [[", "] (fcase #(%1 %2) c @@ -28,6 +30,7 @@ keyword? [c] vector? (interpose " " c) map? (apply concat (interpose [", "] (map #(interpose " " %) c))) + set? (apply concat (interpose [", "] (map format-constraints c))) (throwf IllegalArgumentException "unrecognized constraint: %s (%s)" c (class c)))]) @@ -40,10 +43,13 @@ "Returns a string representing all the constraints for one keyword-item or component formatted for miglayout." [& constraints] - (apply str - (map the-str - (rest (reduce concat [] - (mapcat format-constraint constraints)))))) + (let [formatted + (apply str + (map the-str + (rest (reduce concat [] + (mapcat format-constraint constraints)))))] + ;(prn formatted) + formatted)) (defn component? "Returns true if x is a java.awt.Component" diff --git a/src/clojure/contrib/miglayout/test.clj b/src/clojure/contrib/miglayout/test.clj index 888285c0..abdc89c4 100644 --- a/src/clojure/contrib/miglayout/test.clj +++ b/src/clojure/contrib/miglayout/test.clj @@ -27,15 +27,31 @@ (.pack) (.setVisible true))) +(defn label + "Returns a swing label" + [text] + (JLabel. text)) + +(defn text-field + "Returns a swing text field" + ([] (text-field 10)) + ([width] + (JTextField. width))) + +(defn sep + "Returns a swing separator" + [] + (JSeparator.)) + (def tests [ (fn test0 [panel] (miglayout panel - (JLabel. "Hello") - (JLabel. "World") {:gap :unrelated} - (JTextField. 10) :wrap - (JLabel. "Bonus!") + (label "Hello") + (label "World") {:gap :unrelated} + (text-field) :wrap + (label "Bonus!") (JButton. "Bang it") {:wmin :button :grow :x :span 2} :center)) ;; test1 and test2 are based on code from @@ -45,42 +61,84 @@ (fn test1 [panel] (miglayout panel - :column "[right]" - (JLabel. "General") "split, span" - (JSeparator.) "growx, wrap" - (JLabel. "Company") "gap 10" - (JTextField. "") "span, growx" - (JLabel. "Contact") "gap 10" - (JTextField. "") "span, growx, wrap" - (JLabel. "Propeller") "split, span, gaptop 10" - (JSeparator.) "growx, wrap, gaptop 10" - (JLabel. "PTI/kW") "gapx 10, gapy 15" - (JTextField. 10) - (JLabel. "Power/kW") "gap 10" - (JTextField. 10) "wrap" - (JLabel. "R/mm") "gap 10" - (JTextField. 10) - (JLabel. "D/mm") "gap 10" - (JTextField. 10))) + :column "[right]" + (label "General") "split, span" + (sep) "growx, wrap" + (label "Company") "gap 10" + (text-field "") "span, growx" + (label "Contact") "gap 10" + (text-field "") "span, growx, wrap" + (label "Propeller") "split, span, gaptop 10" + (sep) "growx, wrap, gaptop 10" + (label "PTI/kW") "gapx 10, gapy 15" + (text-field) + (label "Power/kW") "gap 10" + (text-field) "wrap" + (label "R/mm") "gap 10" + (text-field) + (label "D/mm") "gap 10" + (text-field))) ;; the same constraints as strings, keywords, vectors, and maps (fn test2 [panel] (miglayout panel - :column "[right]" - (JLabel. "General") "split, span" - (JSeparator.) :growx :wrap - (JLabel. "Company") [:gap 10] - (JTextField. "") :span :growx - (JLabel. "Contact") [:gap 10] - (JTextField. "") :span :growx :wrap - (JLabel. "Propeller") :split :span [:gaptop 10] - (JSeparator.) :growx :wrap [:gaptop 10] - (JLabel. "PTI/kW") {:gapx 10 :gapy 15} - (JTextField. 10) - (JLabel. "Power/kW") [:gap 10] - (JTextField. 10) :wrap - (JLabel. "R/mm") [:gap 10] - (JTextField. 10) - (JLabel. "D/mm") [:gap 10] - (JTextField. 10)))]) + :column "[right]" + (label "General") "split, span" + (sep) :growx :wrap + (label "Company") [:gap 10] + (text-field "") :span :growx + (label "Contact") [:gap 10] + (text-field "") :span :growx :wrap + (label "Propeller") :split :span [:gaptop 10] + (sep) :growx :wrap [:gaptop 10] + (label "PTI/kW") {:gapx 10 :gapy 15} + (text-field) + (label "Power/kW") [:gap 10] + (text-field) :wrap + (label "R/mm") [:gap 10] + (text-field) + (label "D/mm") [:gap 10] + (text-field))) + + ;; the same constraints using symbols to name groups of constraints + (fn test3 + [panel] + (let [g [:gap 10] + gt [:gaptop 10] + gxs #{:growx :span} + gxw #{:growx :wrap} + gxy {:gapx 10 :gapy 15} + right "[right]" + ss #{:split :span} + w :wrap] + (miglayout panel + :column right + (label "General") ss + (sep) gxw + (label "Company") g + (text-field "") gxs + (label "Contact") g + (text-field "") gxs + (label "Propeller") ss gt + (sep) gxw g + (label "PTI/kW") gxy + (text-field) + (label "Power/kW") g + (text-field) w + (label "R/mm") g + (text-field) + (label "D/mm") g + (text-field)))) + + (fn test4 + [panel] + (miglayout panel + (label "First Name") + (text-field) + (label "Surname") [:gap :unrelated] + (text-field) :wrap + (label "Address") + (text-field) :span :grow)) + +]) |