Skip to content

Commit

Permalink
Cleaned up eventhook business. Replaced with multi-method that's much…
Browse files Browse the repository at this point in the history
… cleaner.
  • Loading branch information
daveray committed Nov 20, 2011
1 parent 3f0e872 commit 89d7a65
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
34 changes: 26 additions & 8 deletions src/seesaw/event.clj
Expand Up @@ -372,14 +372,32 @@
[]
targets))

(defprotocol EventHook
(add-listener [this event-name event-handler]))
(defmulti listen-for-named-event
"*experimental and subject to change*
(extend-protocol EventHook
Object
(add-listener
[this event-name event-handler]
nil))
A multi-method that allows the set of events in the (listen) to be extended or
for an existing event to be extended to a new type. Basically performs
double-dispatch on the type of the target and the name of the event.
This multi-method is an extension point, but is not meant to be called directly
by client code.
Register the given event handler on this for the given event
name which is a keyword like :selection, etc. If the handler
is registered, returns a zero-arg function that undoes the
listener. Otherwise, must return nil indicating that no listener
was registered, i.e. this doesn't support the given event.
TODO try using this to implement all of the event system rather than the mess
above.
See:
(seesaw.swingx/color-selection-button) for an example.
"
(fn [this event-name event-fn] [(type this) event-name]))

; Default impl just returns nil indicating no special handling for the event.
(defmethod listen-for-named-event :default [this event-name event-fn] nil)

(defn- single-target-listen-impl
"Takes:
Expand Down Expand Up @@ -413,7 +431,7 @@
([targets raw-event-name event-fn]
(apply concat
(for [target targets]
(if-let [hook-result (add-listener target raw-event-name event-fn)]
(if-let [hook-result (listen-for-named-event target raw-event-name event-fn)]
[hook-result]
(single-target-listen-impl target raw-event-name event-fn)))))

Expand Down
14 changes: 7 additions & 7 deletions src/seesaw/swingx.clj
Expand Up @@ -19,7 +19,7 @@
(:use [seesaw.util :only [to-uri resource constant-map illegal-argument]]
[seesaw.icon :only [icon]]
[seesaw.selection :only [Selection ViewModelIndexConversion]]
[seesaw.event :only [EventHook listen-to-property]]
[seesaw.event :only [listen-for-named-event listen-to-property]]
[seesaw.core :only [construct to-widget
abstract-panel
border-panel-options
Expand Down Expand Up @@ -457,17 +457,17 @@
args
color-selection-button-options))

; Extend selection and selection event stuff for color button.

(extend-protocol Selection
org.jdesktop.swingx.JXColorSelectionButton
(get-selection [this] [(config this :selection)])
(set-selection [this [v]] (config! this :selection v)))

(extend-protocol EventHook
org.jdesktop.swingx.JXColorSelectionButton
(add-listener [this event-name event-fn]
(condp = event-name
:selection (listen-to-property this "background" event-fn)
nil)))
(defmethod listen-for-named-event
[org.jdesktop.swingx.JXColorSelectionButton :selection]
[this event-name event-fn]
(listen-to-property this "background" event-fn))

;*******************************************************************************
; Header
Expand Down
13 changes: 13 additions & 0 deletions test/seesaw/test/event.clj
Expand Up @@ -304,3 +304,16 @@
:tree-will-collapse #(reset! will-collapse %))
(expect (not (or @will-expand @will-collapse))))))

(describe listen-to-property
(it "registers a property change listener"
(let [b (javax.swing.JButton.)
called (atom nil)
remove-fn (listen-to-property b "text"
(fn [e] (reset! called e)))]
(.setText b "HI")
(expect @called)
(reset! called nil)
(remove-fn)
(.setText b "BYE")
(expect (nil? @called)))))

0 comments on commit 89d7a65

Please sign in to comment.