diff --git a/src/seesaw/core.clj b/src/seesaw/core.clj index f924110a..a0847cba 100644 --- a/src/seesaw/core.clj +++ b/src/seesaw/core.clj @@ -16,7 +16,8 @@ :author "Dave Ray"} seesaw.core (:use [seesaw util meta to-widget make-widget]) - (:require [seesaw color font border invoke timer selection + (:require clojure.java.io + [seesaw color font border invoke timer selection event selector icon action cells table graphics cursor scroll]) (:import [javax.swing SwingConstants UIManager ScrollPaneConstants @@ -563,28 +564,36 @@ javax.swing.JLabel (set-icon [this v] (.setIcon this (make-icon v))) javax.swing.AbstractButton (set-icon [this v] (.setIcon this (make-icon v)))) + (defprotocol ^{:private true} Text (set-text [this v]) (get-text [this])) +(defn- convert-text-value [v] + (cond + (nil? v) v + (string? v) v + (satisfies? clojure.java.io/IOFactory v) (slurp v) + :else (str v))) + (extend-protocol Text Object (set-text [this v] (set-text (to-widget this) v)) (get-text [this] (get-text (to-widget this))) javax.swing.JLabel - (set-text [this v] (.setText this (str v))) + (set-text [this v] (.setText this (convert-text-value v))) (get-text [this] (.getText this)) javax.swing.AbstractButton - (set-text [this v] (.setText this (str v))) + (set-text [this v] (.setText this (convert-text-value v))) (get-text [this] (.getText this)) javax.swing.text.AbstractDocument - (set-text [this v] (.replace this 0 (.getLength this) (str v) nil)) + (set-text [this v] (.replace this 0 (.getLength this) (convert-text-value v) nil)) (get-text [this] (.getText this 0 (.getLength this))) javax.swing.event.DocumentEvent (set-text [this v] (set-text (.getDocument this) v)) (get-text [this] (get-text (.getDocument this))) javax.swing.text.JTextComponent - (set-text [this v] (.setText this (str v))) + (set-text [this v] (.setText this (convert-text-value v))) (get-text [this] (.getText this))) (defprotocol ^{:private true} SetAction (set-action [this v])) @@ -1224,6 +1233,19 @@ turned into a widget or document, or a list of such things. value is the new text value to be applied. Returns targets. + target may be one of: + + A widget + A widget-able thing like an event + A Document + A DocumentEvent + + The resulting text in the widget depends on the type of value: + + A string - the string + A URL, File, or anything \"slurpable\" - the slurped value + Anythign else - (str value) + Example: user=> (def t (text \"HI\")) @@ -1231,6 +1253,12 @@ user=> (text t) \"BYE\" + ; Put the contents of a URL in editor + (text! editor (java.net.URL. \"http://google.com\")) + + Notes: + + This applies to the :text property of new text widgets and config! as well. " [targets value] (check-args (not (nil? targets)) "First arg must not be nil") diff --git a/test/seesaw/test/core.clj b/test/seesaw/test/core.clj index 2e9033c6..b8608b3f 100644 --- a/test/seesaw/test/core.clj +++ b/test/seesaw/test/core.clj @@ -10,7 +10,8 @@ (ns seesaw.test.core (:require [seesaw.selector :as selector] - [seesaw.cursor :as cursor]) + [seesaw.cursor :as cursor] + clojure.java.io) (:use seesaw.core seesaw.font seesaw.graphics @@ -459,7 +460,7 @@ _ (.insertString d 0 "HI" nil) r (text! d "BYE!")] (expect (= d r)) - (expect (= "BYE!" (text d)))))) + (expect (= "BYE!" (text d))))) (it "should set the text of a single text widget argument" (= "BYE" (text (text! (text "HI") "BYE")))) (it "should set the text of a single button argument" @@ -470,6 +471,12 @@ (expect (= [a b] result)) (expect (= "YUM" (text a))) (expect (= "YUM" (text b))))) + (it "should set the text of a widget to the contents of a non-string 'slurpable'" + (let [t (text :multi-line? true)] + (text! t (clojure.java.io/resource "seesaw/test/core.text.txt")) + ; Be careful editing the test file with vim. It will silently add + ; a trailing newline on save. + (expect (= "Some text in a resource" (text t)))))) (describe text (it "should throw IllegalArgumentException if argument is nil" diff --git a/test/seesaw/test/core.text.txt b/test/seesaw/test/core.text.txt new file mode 100644 index 00000000..7685fa5b --- /dev/null +++ b/test/seesaw/test/core.text.txt @@ -0,0 +1 @@ +Some text in a resource \ No newline at end of file