/src/seesaw/to_widget.clj
Clojure | 65 lines | 39 code | 15 blank | 11 comment | 4 complexity | e756ea285a545ad0951f0464423a6d36 MD5 | raw file
- ; Copyright (c) Dave Ray, 2011. 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.
- (ns seesaw.to-widget
- (:use [seesaw util icon])
- (:import [java.awt Dimension]
- [javax.swing Box JLabel JButton]))
- (defprotocol ToWidget (to-widget* [v create?]))
- ; A couple macros to make definining the ToWidget protocol a little less
- ; tedious. Mostly just for fun...
- (defmacro ^{:private true} def-widget-coercion [t b & forms]
- `(extend-type
- ~t
- ToWidget
- (~'to-widget* [~(first b) create?#] ~@forms)))
- (defmacro ^{:private true} def-widget-creational-coercion [t b & forms]
- `(extend-type
- ~t
- ToWidget
- (~'to-widget* [~(first b) create?#] (when create?# ~@forms))))
- ; ... for example, a component coerces to itself.
- (def-widget-coercion java.awt.Component [c] c)
- (def-widget-coercion java.util.EventObject
- [v]
- (try-cast java.awt.Component (.getSource v)))
- (def-widget-creational-coercion java.awt.Dimension [v] (Box/createRigidArea v))
- (def-widget-creational-coercion javax.swing.Action [v] (JButton. v))
- (def-widget-creational-coercion clojure.lang.Keyword
- [v]
- (condp = v
- :separator (javax.swing.JSeparator.)
- :fill-h (Box/createHorizontalGlue)
- :fill-v (Box/createVerticalGlue)))
- (def-widget-creational-coercion clojure.lang.IPersistentVector
- [[v0 v1 v2]]
- (cond
- (= :fill-h v0) (Box/createHorizontalStrut v1)
- (= :fill-v v0) (Box/createVerticalStrut v1)
- (= :by v1) (Box/createRigidArea (Dimension. v0 v2))))
- (def-widget-creational-coercion Object
- [v]
- (JLabel. (str v)))
- (def-widget-creational-coercion java.net.URL
- [v]
- (JLabel. (icon v)))