aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorscgilardi <scgilardi@gmail.com>2008-10-08 01:44:01 +0000
committerscgilardi <scgilardi@gmail.com>2008-10-08 01:44:01 +0000
commite12c8d9b0d8bea8e4222d31e8597c56a4c174963 (patch)
tree1adfa253df1ba32f69802d24d8732c9e092b5dac
parent0aa0933c67a516e9bd2ea9aba4ef56b833e3024f (diff)
clojure.contrib.miglayout: Clojure support for laying out components in swing containers using miglayout (http://miglayout.com)
-rw-r--r--src/clojure/contrib/miglayout/miglayout.clj95
-rw-r--r--src/clojure/contrib/miglayout/test/test.clj86
2 files changed, 181 insertions, 0 deletions
diff --git a/src/clojure/contrib/miglayout/miglayout.clj b/src/clojure/contrib/miglayout/miglayout.clj
new file mode 100644
index 00000000..6a34ab17
--- /dev/null
+++ b/src/clojure/contrib/miglayout/miglayout.clj
@@ -0,0 +1,95 @@
+;; 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.
+;;
+;; miglayout.clj
+;;
+;; Clojure support for the MiGLayout layout manager
+;; http://www.miglayout.com/
+;;
+;; Example:
+;;
+;; (require '[clojure.contrib.miglayout.test :as mlt])
+;; (doseq i (range 3) (mlt/run-test i))
+;;
+;; scgilardi (gmail)
+;; Created 5 October 2008
+
+(ns clojure.contrib.miglayout
+ (:import (net.miginfocom.swing MigLayout)))
+
+(defn miglayout
+ "Adds java.awt.Components to a java.awt.Container with constraints
+ formatted for the MiGLayout layout manager.
+
+ Arguments:
+ - container is the container for the subsequent components
+ - an optional map mapping any or all of the keys :layout, :column
+ or :row to a string that specifies the corresponding constraints for
+ the whole layout.
+ - a series of compononents, each followed by zero or more component
+ constraints.
+
+ Component constraints may be supplied as strings, keywords, vectors, or
+ maps. The set of constraints for a single component is presented to
+ MiGLayout as a single string with each constraint and its arguments
+ separated from any subsequent constraint by a commas. Keywords appear
+ without their leading colons.
+
+ Component constraints:
+ - A string specifies one or more constraints each with zero or more
+ arguments. If it specifies more than one constraint, the string must
+ include commas to separate them.
+ - A keyword specifies a single constraint without arguments
+ - A vector specifies a single constraint with zero or more arguments
+ - A map specifiess one or more constraints as keys, each mapped to a
+ single argument."
+ [container & args]
+ (let [[f & r :as a] args
+ [constraints args] (if (map? f) [f r] [nil a])
+ the-str #((if (keyword? %) name str) %)]
+ (.setLayout container
+ (MigLayout.
+ (str (:layout constraints))
+ (str (:column constraints))
+ (str (:row constraints))))
+ (loop [component (first args)
+ constraints nil
+ [arg & args] (rest args)]
+ (cond (string? arg)
+ (recur component
+ (str constraints ", " arg)
+ args)
+ (keyword? arg)
+ (recur component
+ (str constraints ", " (name arg))
+ args)
+ (vector? arg)
+ (recur component
+ (apply str constraints ", "
+ (map the-str
+ (interpose " " arg)))
+ args)
+ (map? arg)
+ (recur component
+ (apply str constraints ", "
+ (map the-str
+ (apply concat
+ (interpose [", "]
+ (map #(interpose " " %) arg)))))
+ args)
+ (or (instance? java.awt.Component arg) (nil? arg))
+ (do
+ (if constraints
+ (.add container component (subs constraints 2))
+ (.add container component))
+ (if arg
+ (recur arg nil args)
+ container))
+ :else
+ (throw (IllegalArgumentException.
+ (format "unrecognized argument: %s (%s)" arg (class arg))))))))
diff --git a/src/clojure/contrib/miglayout/test/test.clj b/src/clojure/contrib/miglayout/test/test.clj
new file mode 100644
index 00000000..15a5c085
--- /dev/null
+++ b/src/clojure/contrib/miglayout/test/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)))])