aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/miglayout/internal/internal.clj
diff options
context:
space:
mode:
Diffstat (limited to 'src/clojure/contrib/miglayout/internal/internal.clj')
-rw-r--r--src/clojure/contrib/miglayout/internal/internal.clj50
1 files changed, 36 insertions, 14 deletions
diff --git a/src/clojure/contrib/miglayout/internal/internal.clj b/src/clojure/contrib/miglayout/internal/internal.clj
index 045714b6..ba4f1cd5 100644
--- a/src/clojure/contrib/miglayout/internal/internal.clj
+++ b/src/clojure/contrib/miglayout/internal/internal.clj
@@ -6,21 +6,25 @@
;; this license. You must not remove this notice, or any other, from this
;; software.
;;
-;; internal.clj
+;; clojure.contrib.miglayout.internal
;;
;; Internal functions for 'clojure.contrib.miglayout
+;;
+;; scgilardi (gmail)
+;; Created 13 October 2008
(ns clojure.contrib.miglayout.internal
- (:import (java.awt Container Component)))
+ (:import (java.awt Component)))
(defn format-constraints
- "Formats constraints expressed as a series of strings, keywords, vectors
- and/or maps into strings for miglayout."
+ "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."
[& constraints]
(loop [[c & cs] constraints
- cv []]
+ v []]
(if c
- (recur cs (concat cv [", "]
+ (recur cs (concat v [", "]
(cond (or (string? c) (keyword? c))
[c]
(vector? c)
@@ -31,20 +35,38 @@
(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))
+ (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))
-(defn item?
+(defn component?
+ "Returns true if x is a java.awt.Component"
[x]
- (or (component? x) (keyword-item? x)))
+ (instance? Component x))
(defn constraint?
+ "Returns true if x is not a keyword-item or component"
[x]
- (not (item? x)))
+ (not
+ (or (keyword-item? x)
+ (component? 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 {:keyword-items {} :components []}]
+ (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)))