diff options
author | scgilardi <scgilardi@gmail.com> | 2008-11-12 22:31:20 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2008-11-12 22:31:20 +0000 |
commit | 6f7a77e9cdc8df4f58fd86eed0dcd0dd6fc2d6fd (patch) | |
tree | 84939b269427bb8213c7db3e002628aa13816bd3 /src/clojure/contrib/miglayout | |
parent | db748f4c8b6f37da894b3c8f7a3bb683eea3f0aa (diff) |
first cut at changes for Clojure SVN 1094+, my contribs and ones they depend on now load again
Diffstat (limited to 'src/clojure/contrib/miglayout')
-rw-r--r-- | src/clojure/contrib/miglayout/internal.clj | 75 | ||||
-rw-r--r-- | src/clojure/contrib/miglayout/test.clj | 86 |
2 files changed, 161 insertions, 0 deletions
diff --git a/src/clojure/contrib/miglayout/internal.clj b/src/clojure/contrib/miglayout/internal.clj new file mode 100644 index 00000000..07391b77 --- /dev/null +++ b/src/clojure/contrib/miglayout/internal.clj @@ -0,0 +1,75 @@ +;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and +;; distribution terms for this software are covered by the Common Public +;; License 1.0 (http://opensource.org/licenses/cpl.php) which can be found +;; in the file CPL.TXT at the root of this distribution. By using this +;; software in any fashion, you are agreeing to be bound by the terms of +;; this license. You must not remove this notice, or any other, from this +;; software. +;; +;; clojure.contrib.miglayout.internal +;; +;; Internal functions for 'clojure.contrib.miglayout +;; +;; scgilardi (gmail) +;; Created 13 October 2008 + +(ns clojure.contrib.miglayout.internal + (:import (java.awt Component)) + (:use (clojure.contrib except fcase))) + +(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." + [c] + [[", "] + (fcase #(%1 %2) c + string? [c] + keyword? [c] + vector? (interpose " " c) + map? (apply concat (interpose [", "] (map #(interpose " " %) c))) + (throwf IllegalArgumentException + "unrecognized constraint: %s (%s)" c (class c)))]) + +(defn the-str + "Returns the string for x--its name if it's a keyword." + [x] + ((if (keyword? x) name str) x)) + +(defn format-constraints + "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)))))) + +(defn component? + "Returns true if x is a java.awt.Component" + [x] + (instance? Component x)) + +(defn constraint? + "Returns true if x is not a keyword-item or component" + [x] + (not + (or (component? x) + (#{:layout :column :row} x)))) + +(defn parse-item-constraints + "Iterates over args and builds a map containing :keywords, a map of from + keyword-item to constraints string and :components, a vector of vectors + each associating a component with its constraints string. :components is + a vector because ordering of components matters." + [& args] + (loop [[item & args] args + item-constraints {:components [] :keyword-items {}}] + (if item + (let [[constraints args] (split-with constraint? args)] + (recur args + (update-in + item-constraints + [(if (component? item) :components :keyword-items)] + conj [item (apply format-constraints constraints)]))) + item-constraints))) diff --git a/src/clojure/contrib/miglayout/test.clj b/src/clojure/contrib/miglayout/test.clj new file mode 100644 index 00000000..3ff7a97c --- /dev/null +++ b/src/clojure/contrib/miglayout/test.clj @@ -0,0 +1,86 @@ +;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and +;; distribution terms for this software are covered by the Common Public +;; License 1.0 (http://opensource.org/licenses/cpl.php) which can be found +;; in the file CPL.TXT at the root of this distribution. By using this +;; software in any fashion, you are agreeing to be bound by the terms of +;; this license. You must not remove this notice, or any other, from this +;; software. +;; +;; clojure.contrib.miglayout.test +;; +;; Test/example for clojure.contrib.miglayout +;; +;; scgilardi (gmail) +;; Created 5 October 2008 + +(ns clojure.contrib.miglayout.test + (:import (javax.swing JButton JFrame JLabel JList JPanel + JScrollPane JTabbedPane JTextField JSeparator)) + (:use clojure.contrib.miglayout)) + +(def tests) + +(defn run-test + [index] + (doto (JFrame. (format "MigLayout Test %d" index)) + (add ((tests index) (JPanel.))) + (pack) + (setVisible true))) + +(def tests [ + + (fn test0 + [panel] + (miglayout panel + (JLabel. "Hello") + (JLabel. "World") {:gap :unrelated} + (JTextField. 10) :wrap + (JLabel. "Bonus!") + (JButton. "Bang it") {:wmin :button :grow :x :span 2} :center)) + + ;; test1 and test2 are based on code from + ;; http://www.devx.com/java/Article/38017/1954 + + ;; constraints as strings exclusively + (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))) + + ;; 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)))]) |