aboutsummaryrefslogtreecommitdiff
path: root/modules/miglayout
diff options
context:
space:
mode:
authorStuart Sierra <mail@stuartsierra.com>2010-08-07 16:41:53 -0400
committerStuart Sierra <mail@stuartsierra.com>2010-08-07 16:41:53 -0400
commita6a92b9b3d2bfd9a56e1e5e9cfba706d1aeeaae5 (patch)
treef1f3da9887dc2dc557df3282b0bcbd4d701ec593 /modules/miglayout
parente7930c85290f77815cdb00a60604feedfa2d0194 (diff)
Split all namespaces into sub-modules.
* Examples and tests have not been copied over. * Clojure test/compile phases are commented out in parent POM. * May require installing parent POM before full build.
Diffstat (limited to 'modules/miglayout')
-rw-r--r--modules/miglayout/pom.xml14
-rw-r--r--modules/miglayout/src/main/clojure/clojure/contrib/miglayout.clj79
-rw-r--r--modules/miglayout/src/main/clojure/clojure/contrib/miglayout/internal.clj120
3 files changed, 213 insertions, 0 deletions
diff --git a/modules/miglayout/pom.xml b/modules/miglayout/pom.xml
new file mode 100644
index 00000000..c94b6726
--- /dev/null
+++ b/modules/miglayout/pom.xml
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http//www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
+ http://maven.apache.org/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <groupId>org.clojure.contrib</groupId>
+ <artifactId>parent</artifactId>
+ <version>1.3.0-SNAPSHOT</version>
+ <relativePath>../parent</relativePath>
+ </parent>
+ <artifactId>miglayout</artifactId>
+</project> \ No newline at end of file
diff --git a/modules/miglayout/src/main/clojure/clojure/contrib/miglayout.clj b/modules/miglayout/src/main/clojure/clojure/contrib/miglayout.clj
new file mode 100644
index 00000000..f9c03a13
--- /dev/null
+++ b/modules/miglayout/src/main/clojure/clojure/contrib/miglayout.clj
@@ -0,0 +1,79 @@
+;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and
+;; distribution terms for this software are covered by the Eclipse Public
+;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
+;; be found in the file epl-v10.html 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
+;;
+;; Clojure support for the MiGLayout layout manager
+;; http://www.miglayout.com/
+;;
+;; Example:
+;;
+;; (use '[clojure.contrib.miglayout.test :as mlt :only ()])
+;; (dotimes [i 5] (mlt/run-test i))
+;;
+;; scgilardi (gmail)
+;; Created 5 October 2008
+
+(ns
+ ^{:author "Stephen C. Gilardi",
+ :doc "Clojure support for the MiGLayout layout manager
+http://www.miglayout.com/
+
+Example:
+
+ (use '[clojure.contrib.miglayout.test :as mlt :only ()])
+ (dotimes [i 5] (mlt/run-test i))
+
+"}
+ clojure.contrib.miglayout
+ (:import javax.swing.JComponent)
+ (:use clojure.contrib.miglayout.internal))
+
+(defn miglayout
+ "Adds java.awt.Components to a javax.swing.JComponent with constraints
+ formatted for the MiGLayout layout manager.
+
+ Arguments: container [item constraint*]*
+
+ - container: the container for the specified components, its layout
+ manager will be set to a new instance of MigLayout
+
+ - an inline series of items and constraints--each item may be followed
+ by zero or more constraints.
+
+ 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.
+
+ Constraint: string, keyword, vector, map, or set
+
+ - A string specifies one or more constraints each with zero or more
+ 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
+ - A set groups two or more constraints, each a string, keyword,
+ vector, map, or set
+
+ Any items marked with an \"id\" constraint will be included in a map from
+ id to component attached to the container. The map can be retrieved using
+ clojure.contrib.miglayout/components."
+ [^JComponent container & args]
+ (let [item-constraints (apply parse-item-constraints args)
+ {:keys [keywords components]} item-constraints
+ {:keys [layout column row]} keywords]
+ (do-layout container layout column row components)))
+
+(defn components
+ "Returns a map from id (a keyword) to component for all components with
+ an id constraint set"
+ [^JComponent container]
+ (get-components container))
diff --git a/modules/miglayout/src/main/clojure/clojure/contrib/miglayout/internal.clj b/modules/miglayout/src/main/clojure/clojure/contrib/miglayout/internal.clj
new file mode 100644
index 00000000..f6e6431f
--- /dev/null
+++ b/modules/miglayout/src/main/clojure/clojure/contrib/miglayout/internal.clj
@@ -0,0 +1,120 @@
+;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and
+;; distribution terms for this software are covered by the Eclipse Public
+;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can
+;; be found in the file epl-v10.html 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.internal
+;;
+;; Internal functions for 'clojure.contrib.miglayout
+;;
+;; scgilardi (gmail)
+;; Created 13 October 2008
+
+(ns clojure.contrib.miglayout.internal
+ (:import (clojure.lang RT Reflector)
+ java.awt.Component
+ javax.swing.JComponent)
+ (:use (clojure.contrib
+ [core :only (new-by-name)]
+ [except :only (throwf)]
+ [fcase :only (fcase)]
+ [string :only (as-str)])))
+
+(def MigLayout "net.miginfocom.swing.MigLayout")
+(def LayoutCallback "net.miginfocom.layout.LayoutCallback")
+(def ConstraintParser "net.miginfocom.layout.ConstraintParser")
+
+(declare format-constraints)
+
+(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, maps, and/or sets."
+ [c]
+ [[", "]
+ (fcase #(%1 %2) c
+ string? [c]
+ keyword? [c]
+ vector? (interpose " " c)
+ map? (apply concat (interpose [", "] (map #(interpose " " %) c)))
+ set? (apply concat (interpose [", "] (map format-constraints c)))
+ (throwf IllegalArgumentException
+ "unrecognized constraint: %s (%s)" c (class c)))])
+
+(defn format-constraints
+ "Returns a string representing all the constraints for one keyword-item
+ or component formatted for miglayout."
+ [& constraints]
+ (let [formatted
+ (apply str
+ (map as-str
+ (rest (reduce concat []
+ (mapcat format-constraint constraints)))))]
+;; (prn formatted)
+ formatted))
+
+(defn component?
+ "Returns true if x is a java.awt.Component"
+ [x]
+ (instance? Component x))
+
+(defn constraint?
+ "Returns true if x is not a keyword-item or component"
+ [x]
+ (not
+ (or (component? x)
+ (#{:layout :column :row} x))))
+
+(defn parse-item-constraints
+ "Iterates over args and builds a map containing values associated with
+ :keywords and :components. The value for :keywords is a map from keyword
+ items to constraints strings. The value for :components is a vector of
+ vectors each associating a component with its constraints string."
+ [& args]
+ (loop [[item & args] args
+ item-constraints {:keywords {} :components []}]
+ (if item
+ (let [[constraints args] (split-with constraint? args)]
+ (recur args
+ (update-in
+ item-constraints
+ [(if (component? item) :components :keywords)]
+ conj [item (apply format-constraints constraints)])))
+ item-constraints)))
+
+(defn parse-component-constraint
+ "Parses a component constraint string returning a CC object"
+ [constraint]
+ (Reflector/invokeStaticMethod
+ ConstraintParser "parseComponentConstraint" (into-array [constraint])))
+
+(defn add-components
+ "Adds components with constraints to a container"
+ [^JComponent container components]
+ (loop [[[^Component component constraint] & components] components
+ id-map nil]
+ (if component
+ (let [cc (parse-component-constraint constraint)]
+ (.add container component cc)
+ (recur
+ components
+ (if-let [id (.getId cc)]
+ (assoc id-map (keyword id) component)
+ id-map)))
+ (doto container (.putClientProperty ::components id-map)))))
+
+(defn get-components
+ "Returns a map from id to component for all components with an id"
+ [^JComponent container]
+ (.getClientProperty container ::components))
+
+(defn do-layout
+ "Attaches a MigLayout layout manager to container and adds components
+ with constraints"
+ [^JComponent container layout column row components]
+ (doto container
+ (.setLayout (new-by-name MigLayout layout column row))
+ (add-components components)))