diff options
author | scgilardi <scgilardi@gmail.com> | 2008-10-28 04:28:01 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2008-10-28 04:28:01 +0000 |
commit | a103dbb5f484e90de02aa0fa73aa3a5114093c0e (patch) | |
tree | 9472e3998283d12a9092931ca4eac094ca8ab353 /src/clojure/contrib/miglayout/internal/internal.clj | |
parent | 564e78b9b0269cdfe61fcea740e39ac4363c0379 (diff) |
miglayout/internal: gettin' that reduce religion
Diffstat (limited to 'src/clojure/contrib/miglayout/internal/internal.clj')
-rw-r--r-- | src/clojure/contrib/miglayout/internal/internal.clj | 57 |
1 files changed, 30 insertions, 27 deletions
diff --git a/src/clojure/contrib/miglayout/internal/internal.clj b/src/clojure/contrib/miglayout/internal/internal.clj index ba4f1cd5..07391b77 100644 --- a/src/clojure/contrib/miglayout/internal/internal.clj +++ b/src/clojure/contrib/miglayout/internal/internal.clj @@ -14,33 +14,36 @@ ;; Created 13 October 2008 (ns clojure.contrib.miglayout.internal - (:import (java.awt Component))) + (: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. In Clojure, the constraints may be - specified using strings, keywords, vectors, and/or maps." + or component formatted for miglayout." [& constraints] - (loop [[c & cs] constraints - v []] - (if c - (recur cs (concat v [", "] - (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 v)))))) - -(defn keyword-item? - "Returns true if x is a keyword-item" - [x] - (#{:layout :column :row} x)) + (apply str + (map the-str + (rest (reduce concat [] + (mapcat format-constraint constraints)))))) (defn component? "Returns true if x is a java.awt.Component" @@ -51,8 +54,8 @@ "Returns true if x is not a keyword-item or component" [x] (not - (or (keyword-item? x) - (component? x)))) + (or (component? x) + (#{:layout :column :row} x)))) (defn parse-item-constraints "Iterates over args and builds a map containing :keywords, a map of from @@ -61,12 +64,12 @@ a vector because ordering of components matters." [& args] (loop [[item & args] args - item-constraints {:keyword-items {} :components []}] + 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)])))) + conj [item (apply format-constraints constraints)]))) item-constraints))) |