aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/miglayout
diff options
context:
space:
mode:
authorscgilardi <scgilardi@gmail.com>2008-10-14 13:21:15 +0000
committerscgilardi <scgilardi@gmail.com>2008-10-14 13:21:15 +0000
commit7d343ae94fc925d2a37bae02978d0ec04f409d4e (patch)
tree5a1e8bbc4ea1e4ef3ef3e21916d0315722a71db1 /src/clojure/contrib/miglayout
parentc7791c8b95a6471d2abb1ca0831e25a1923ca83e (diff)
miglayout: internal changes to be more FP oriented
Diffstat (limited to 'src/clojure/contrib/miglayout')
-rw-r--r--src/clojure/contrib/miglayout/internal/internal.clj50
-rw-r--r--src/clojure/contrib/miglayout/miglayout.clj40
2 files changed, 48 insertions, 42 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)))
diff --git a/src/clojure/contrib/miglayout/miglayout.clj b/src/clojure/contrib/miglayout/miglayout.clj
index d0b6b297..79a111b1 100644
--- a/src/clojure/contrib/miglayout/miglayout.clj
+++ b/src/clojure/contrib/miglayout/miglayout.clj
@@ -6,7 +6,7 @@
;; this license. You must not remove this notice, or any other, from this
;; software.
;;
-;; miglayout.clj
+;; clojure.contrib.miglayout
;;
;; Clojure support for the MiGLayout layout manager
;; http://www.miglayout.com/
@@ -42,38 +42,22 @@
: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.
+ arguments.
- A keyword specifies a single constraint without arguments
- A vector specifies a single constraint with one or more arguments
- A map specifies one or more constraints as keys, each mapped to a
single argument"
[#^Container container & args]
- (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))))
+ (let [{:keys [keyword-items components]}
+ (apply parse-item-constraints args)]
+ (.setLayout container
+ (MigLayout.
+ (:layout keyword-items)
+ (:column keyword-items)
+ (:row keyword-items)))
+ (doseq [#^Component component constraints] components
+ (.add container component constraints))
+ container))