Skip to content

Commit

Permalink
Setting the text of a widget to a URL, File, Reader, or other, non-st…
Browse files Browse the repository at this point in the history
…ring slurpable things will slurp that text into the widget. Applies to :text property and (text!).
  • Loading branch information
daveray committed Jul 25, 2011
1 parent 798fe37 commit 4f1f99a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
38 changes: 33 additions & 5 deletions src/seesaw/core.clj
Expand Up @@ -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
Expand Down Expand Up @@ -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]))
Expand Down Expand Up @@ -1224,13 +1233,32 @@
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\"))
user=> (text! t \"BYE\")
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")
Expand Down
11 changes: 9 additions & 2 deletions test/seesaw/test/core.clj
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand Down
1 change: 1 addition & 0 deletions test/seesaw/test/core.text.txt
@@ -0,0 +1 @@
Some text in a resource

0 comments on commit 4f1f99a

Please sign in to comment.