Permalink
Browse files

Moved ToWidget protocol out of core. Circular deps.

  • Loading branch information...
1 parent c1dc30e commit a12308028c145bd92aa71ce5ca7993ff31f558b7 @daveray committed Jun 5, 2011
Showing with 83 additions and 53 deletions.
  1. +2 −52 src/seesaw/core.clj
  2. +1 −1 src/seesaw/examples/to_widget.clj
  3. +65 −0 src/seesaw/to_widget.clj
  4. +15 −0 test/seesaw/test/to_widget.clj
View
@@ -9,7 +9,7 @@
; You must not remove this notice, or any other, from this software.
(ns seesaw.core
- (:use [seesaw util font border color meta]
+ (:use [seesaw util font border color meta to-widget]
[clojure.string :only (capitalize split)])
(:require [seesaw.invoke]
[seesaw.event :as sse]
@@ -146,64 +146,14 @@
;*******************************************************************************
; Widget coercion prototcol
-(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. (make-icon v)))
-
(defn to-widget
"Try to convert the input argument to a widget based on the following rules:
nil -> nil
java.awt.Component -> return argument unchanged
+ java.util.EventObject -> return the event source
java.awt.Dimension -> return Box/createRigidArea
java.swing.Action -> return a button using the action
- java.util.EventObject -> return the event source
:separator -> create a horizontal JSeparator
:fill-h -> Box/createHorizontalGlue
:fill-v -> Box/createVerticalGlue
@@ -9,7 +9,7 @@
; You must not remove this notice, or any other, from this software.
(ns seesaw.examples.to-widget
- (:use [seesaw core border]))
+ (:use [seesaw core border to-widget]))
; This example shows how to implement the ToWidget protocol for a new type.
; Is this cute? Yes. Useful? Hell if I know.
@@ -0,0 +1,65 @@
+; 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)))
+
@@ -0,0 +1,15 @@
+; 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.test.to-widget
+ (:use seesaw.to-widget)
+ (:use [lazytest.describe :only (describe it testing)]
+ [lazytest.expect :only (expect)]))
+

0 comments on commit a123080

Please sign in to comment.