diff options
author | scgilardi <scgilardi@gmail.com> | 2009-05-31 06:02:24 +0000 |
---|---|---|
committer | scgilardi <scgilardi@gmail.com> | 2009-05-31 06:02:24 +0000 |
commit | 8bce6fb2d0a9b66aa1fd145f3dfd62833d8ad330 (patch) | |
tree | d8f5b9eae7ca6aec21ae391a39ca7826b8614fd4 /src/clojure | |
parent | 9c2161b4986244a26989b9f70d6f5592f8c89301 (diff) |
miglayout: add support for retrieving components by id
Diffstat (limited to 'src/clojure')
-rw-r--r-- | src/clojure/contrib/miglayout.clj | 20 | ||||
-rw-r--r-- | src/clojure/contrib/miglayout/internal.clj | 61 | ||||
-rw-r--r-- | src/clojure/contrib/miglayout/test.clj | 19 |
3 files changed, 67 insertions, 33 deletions
diff --git a/src/clojure/contrib/miglayout.clj b/src/clojure/contrib/miglayout.clj index d8f37180..a481d20f 100644 --- a/src/clojure/contrib/miglayout.clj +++ b/src/clojure/contrib/miglayout.clj @@ -31,7 +31,7 @@ Example: "} clojure.contrib.miglayout - (:import java.awt.Container) + (:import javax.swing.JComponent) (:use clojure.contrib.miglayout.internal)) (defn miglayout @@ -61,11 +61,19 @@ Example: - 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" - [#^Container container & args] + 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] - (doto container - (.setLayout (new-instance layout column row)) - (add-components components)))) + (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/src/clojure/contrib/miglayout/internal.clj b/src/clojure/contrib/miglayout/internal.clj index fb6d7257..e06bd2e0 100644 --- a/src/clojure/contrib/miglayout/internal.clj +++ b/src/clojure/contrib/miglayout/internal.clj @@ -14,29 +14,20 @@ ;; Created 13 October 2008 (ns clojure.contrib.miglayout.internal - (:import (java.awt Container Component) - clojure.lang.RT) + (: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)] [java-utils :only (as-str)]))) -(declare format-constraints) - -(defn new-instance - "Returns a new instance of MigLayout with the specified constraints" - [layout column row] - (doto (.newInstance (RT/classForName "net.miginfocom.swing.MigLayout")) - (.setLayoutConstraints layout) - (.setColumnConstraints column) - (.setRowConstraints row))) +(def MigLayout "net.miginfocom.swing.MigLayout") +(def LayoutCallback "net.miginfocom.layout.LayoutCallback") +(def ConstraintParser "net.miginfocom.layout.ConstraintParser") -(defn add-components - "Adds components with constraints to a container" - [#^Container container components] - (doseq [[#^Component component constraints] components] - (.add container component constraints)) - container) +(declare format-constraints) (defn format-constraint "Returns a vector of vectors representing one or more constraints @@ -62,7 +53,7 @@ (map as-str (rest (reduce concat [] (mapcat format-constraint constraints)))))] - ;(prn formatted) +;; (prn formatted) formatted)) (defn component? @@ -93,3 +84,37 @@ [(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))) diff --git a/src/clojure/contrib/miglayout/test.clj b/src/clojure/contrib/miglayout/test.clj index abdc89c4..dec12ba8 100644 --- a/src/clojure/contrib/miglayout/test.clj +++ b/src/clojure/contrib/miglayout/test.clj @@ -22,10 +22,12 @@ (defn run-test [index] - (doto (JFrame. (format "MigLayout Test %d" index)) - (.add ((tests index) (JPanel.))) - (.pack) - (.setVisible true))) + (let [panel ((tests index) (JPanel.))] + (println index (components panel)) + (doto (JFrame. (format "MigLayout Test %d" index)) + (.add panel) + (.pack) + (.setVisible true)))) (defn label "Returns a swing label" @@ -134,11 +136,10 @@ (fn test4 [panel] (miglayout panel - (label "First Name") - (text-field) + (label "First Name") + (text-field) {:id :firstname} (label "Surname") [:gap :unrelated] - (text-field) :wrap + (text-field) {:id :surname} :wrap (label "Address") - (text-field) :span :grow)) - + (text-field) {:id :address} :span :grow)) ]) |