diff options
author | scgilardi <scgilardi@gmail.com> | 2008-10-13 21:05:10 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2008-10-13 21:05:10 +0000 |
commit | c7791c8b95a6471d2abb1ca0831e25a1923ca83e (patch) | |
tree | 514f2df157d0eb76e9a262a192ab056be1c41f45 | |
parent | e0d51e894300f727081f75eda20f3ad4eb2335d7 (diff) |
miglayout: clean up implementation, :layout, :column, and :row are now pseudo-components taking inline constraints like components do
-rw-r--r-- | src/clojure/contrib/miglayout/internal/internal.clj | 50 | ||||
-rw-r--r-- | src/clojure/contrib/miglayout/miglayout.clj | 99 | ||||
-rw-r--r-- | src/clojure/contrib/miglayout/test/test.clj | 4 |
3 files changed, 91 insertions, 62 deletions
diff --git a/src/clojure/contrib/miglayout/internal/internal.clj b/src/clojure/contrib/miglayout/internal/internal.clj new file mode 100644 index 00000000..045714b6 --- /dev/null +++ b/src/clojure/contrib/miglayout/internal/internal.clj @@ -0,0 +1,50 @@ +;; 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. +;; +;; internal.clj +;; +;; Internal functions for 'clojure.contrib.miglayout + +(ns clojure.contrib.miglayout.internal + (:import (java.awt Container Component))) + +(defn format-constraints + "Formats constraints expressed as a series of strings, keywords, vectors + and/or maps into strings for miglayout." + [& constraints] + (loop [[c & cs] constraints + cv []] + (if c + (recur cs (concat cv [", "] + (cond (or (string? c) (keyword? c)) + [c] + (vector? c) + (interpose " " c) + (map? c) + (apply concat (interpose [", "] (map #(interpose " " %) c))) + :else + (throw + (IllegalArgumentException. + (format "unrecognized constraint: %s (%s)" c (class c))))))) + (apply str (map #((if (keyword? %) name str) %) (rest cv)))))) + +(defn component? + [x] + (instance? Component x)) + +(defn keyword-item? + [x] + (#{:layout :column :row} x)) + +(defn item? + [x] + (or (component? x) (keyword-item? x))) + +(defn constraint? + [x] + (not (item? x))) diff --git a/src/clojure/contrib/miglayout/miglayout.clj b/src/clojure/contrib/miglayout/miglayout.clj index 3d8bbd9e..d0b6b297 100644 --- a/src/clojure/contrib/miglayout/miglayout.clj +++ b/src/clojure/contrib/miglayout/miglayout.clj @@ -21,80 +21,59 @@ (ns clojure.contrib.miglayout (:import (java.awt Container Component) - (net.miginfocom.swing MigLayout))) + (net.miginfocom.swing MigLayout)) + (:use clojure.contrib.miglayout.internal)) (defn miglayout "Adds java.awt.Components to a java.awt.Container with constraints formatted for the MiGLayout layout manager. - Arguments: container layout-constraints? [component constraint*]* + Arguments: container [item constraint*]* - container: the container for the specified components, its layout manager will be set to a new instance of MigLayout - - layout-constraints: an optional map that maps any or all of - :layout, :column, and/or :row to a string that specifies the - corresponding constraints for the whole layout - - an inline series of components and constraints: each component may be - followed by zero or more component constraints - The set of constraints for each component is presented to MiGLayout as a - single string with each constraint and its arguments separated from any - subsequent constraint by a comma. + - an inline series of items and constraints--each item may be followed + by zero or more constraints. - Component constraint: string, keyword, vector, or map + Item: + + - An item is either a Component or one of the keywords :layout + :column or :row. Constraints for a keyword item affect the entire + layout. + + Constraints: + + - The set of constraints for each item is presented to MiGLayout as a + single string with each constraint and its arguments separated from + any subsequent constraint by a comma. + + Constraint: string, keyword, vector, or map - 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 one or more arguments - - A map specifiess one or more constraints as keys, each mapped to a - single argument - - Empty strings, vectors, and maps are accepted but don't affect the - layout." + - A map specifies one or more constraints as keys, each mapped to a + single argument" [#^Container 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 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)))))))) + (loop [[item & args] args + item-constraints {:layout {} :component []}] + (if item + (let [[constraints args] (split-with constraint? args)] + (recur args + (update-in + item-constraints + [(if (component? item) :component :layout)] + #(conj % [item (apply format-constraints constraints)])))) + (do + (.setLayout + container + (MigLayout. + (get-in item-constraints [:layout :layout]) + (get-in item-constraints [:layout :column]) + (get-in item-constraints [:layout :row]))) + (doseq [component constraints] (:component item-constraints) + (.add container component constraints)) + container)))) diff --git a/src/clojure/contrib/miglayout/test/test.clj b/src/clojure/contrib/miglayout/test/test.clj index 15a5c085..3ff7a97c 100644 --- a/src/clojure/contrib/miglayout/test/test.clj +++ b/src/clojure/contrib/miglayout/test/test.clj @@ -45,7 +45,7 @@ (fn test1 [panel] (miglayout panel - {:column "[right]"} + :column "[right]" (JLabel. "General") "split, span" (JSeparator.) "growx, wrap" (JLabel. "Company") "gap 10" @@ -67,7 +67,7 @@ (fn test2 [panel] (miglayout panel - {:column "[right]"} + :column "[right]" (JLabel. "General") "split, span" (JSeparator.) :growx :wrap (JLabel. "Company") [:gap 10] |