Mercurial > lasercutter
view src/clojure/contrib/miglayout/internal.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 ;; Copyright (c) Stephen C. Gilardi. All rights reserved. The use and2 ;; distribution terms for this software are covered by the Eclipse Public3 ;; License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) which can4 ;; be found in the file epl-v10.html at the root of this distribution. By5 ;; using this software in any fashion, you are agreeing to be bound by the6 ;; terms of this license. You must not remove this notice, or any other,7 ;; from this software.8 ;;9 ;; clojure.contrib.miglayout.internal10 ;;11 ;; Internal functions for 'clojure.contrib.miglayout12 ;;13 ;; scgilardi (gmail)14 ;; Created 13 October 200816 (ns clojure.contrib.miglayout.internal17 (:import (clojure.lang RT Reflector)18 java.awt.Component19 javax.swing.JComponent)20 (:use (clojure.contrib21 [core :only (new-by-name)]22 [except :only (throwf)]23 [fcase :only (fcase)]24 [string :only (as-str)])))26 (def MigLayout "net.miginfocom.swing.MigLayout")27 (def LayoutCallback "net.miginfocom.layout.LayoutCallback")28 (def ConstraintParser "net.miginfocom.layout.ConstraintParser")30 (declare format-constraints)32 (defn format-constraint33 "Returns a vector of vectors representing one or more constraints34 separated by commas. Constraints may be specified in Clojure using35 strings, keywords, vectors, maps, and/or sets."36 [c]37 [[", "]38 (fcase #(%1 %2) c39 string? [c]40 keyword? [c]41 vector? (interpose " " c)42 map? (apply concat (interpose [", "] (map #(interpose " " %) c)))43 set? (apply concat (interpose [", "] (map format-constraints c)))44 (throwf IllegalArgumentException45 "unrecognized constraint: %s (%s)" c (class c)))])47 (defn format-constraints48 "Returns a string representing all the constraints for one keyword-item49 or component formatted for miglayout."50 [& constraints]51 (let [formatted52 (apply str53 (map as-str54 (rest (reduce concat []55 (mapcat format-constraint constraints)))))]56 ;; (prn formatted)57 formatted))59 (defn component?60 "Returns true if x is a java.awt.Component"61 [x]62 (instance? Component x))64 (defn constraint?65 "Returns true if x is not a keyword-item or component"66 [x]67 (not68 (or (component? x)69 (#{:layout :column :row} x))))71 (defn parse-item-constraints72 "Iterates over args and builds a map containing values associated with73 :keywords and :components. The value for :keywords is a map from keyword74 items to constraints strings. The value for :components is a vector of75 vectors each associating a component with its constraints string."76 [& args]77 (loop [[item & args] args78 item-constraints {:keywords {} :components []}]79 (if item80 (let [[constraints args] (split-with constraint? args)]81 (recur args82 (update-in83 item-constraints84 [(if (component? item) :components :keywords)]85 conj [item (apply format-constraints constraints)])))86 item-constraints)))88 (defn parse-component-constraint89 "Parses a component constraint string returning a CC object"90 [constraint]91 (Reflector/invokeStaticMethod92 ConstraintParser "parseComponentConstraint" (into-array [constraint])))94 (defn add-components95 "Adds components with constraints to a container"96 [^JComponent container components]97 (loop [[[^Component component constraint] & components] components98 id-map nil]99 (if component100 (let [cc (parse-component-constraint constraint)]101 (.add container component cc)102 (recur103 components104 (if-let [id (.getId cc)]105 (assoc id-map (keyword id) component)106 id-map)))107 (doto container (.putClientProperty ::components id-map)))))109 (defn get-components110 "Returns a map from id to component for all components with an id"111 [^JComponent container]112 (.getClientProperty container ::components))114 (defn do-layout115 "Attaches a MigLayout layout manager to container and adds components116 with constraints"117 [^JComponent container layout column row components]118 (doto container119 (.setLayout (new-by-name MigLayout layout column row))120 (add-components components)))