PageRenderTime 27ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/src/seesaw/to_widget.clj

https://github.com/KevinKelley/seesaw
Clojure | 65 lines | 39 code | 15 blank | 11 comment | 4 complexity | e756ea285a545ad0951f0464423a6d36 MD5 | raw file
  1. ; Copyright (c) Dave Ray, 2011. All rights reserved.
  2. ; The use and distribution terms for this software are covered by the
  3. ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
  4. ; which can be found in the file epl-v10.html at the root of this
  5. ; distribution.
  6. ; By using this software in any fashion, you are agreeing to be bound by
  7. ; the terms of this license.
  8. ; You must not remove this notice, or any other, from this software.
  9. (ns seesaw.to-widget
  10. (:use [seesaw util icon])
  11. (:import [java.awt Dimension]
  12. [javax.swing Box JLabel JButton]))
  13. (defprotocol ToWidget (to-widget* [v create?]))
  14. ; A couple macros to make definining the ToWidget protocol a little less
  15. ; tedious. Mostly just for fun...
  16. (defmacro ^{:private true} def-widget-coercion [t b & forms]
  17. `(extend-type
  18. ~t
  19. ToWidget
  20. (~'to-widget* [~(first b) create?#] ~@forms)))
  21. (defmacro ^{:private true} def-widget-creational-coercion [t b & forms]
  22. `(extend-type
  23. ~t
  24. ToWidget
  25. (~'to-widget* [~(first b) create?#] (when create?# ~@forms))))
  26. ; ... for example, a component coerces to itself.
  27. (def-widget-coercion java.awt.Component [c] c)
  28. (def-widget-coercion java.util.EventObject
  29. [v]
  30. (try-cast java.awt.Component (.getSource v)))
  31. (def-widget-creational-coercion java.awt.Dimension [v] (Box/createRigidArea v))
  32. (def-widget-creational-coercion javax.swing.Action [v] (JButton. v))
  33. (def-widget-creational-coercion clojure.lang.Keyword
  34. [v]
  35. (condp = v
  36. :separator (javax.swing.JSeparator.)
  37. :fill-h (Box/createHorizontalGlue)
  38. :fill-v (Box/createVerticalGlue)))
  39. (def-widget-creational-coercion clojure.lang.IPersistentVector
  40. [[v0 v1 v2]]
  41. (cond
  42. (= :fill-h v0) (Box/createHorizontalStrut v1)
  43. (= :fill-v v0) (Box/createVerticalStrut v1)
  44. (= :by v1) (Box/createRigidArea (Dimension. v0 v2))))
  45. (def-widget-creational-coercion Object
  46. [v]
  47. (JLabel. (str v)))
  48. (def-widget-creational-coercion java.net.URL
  49. [v]
  50. (JLabel. (icon v)))