Browse files

Setting the text of a widget to a URL, File, Reader, or other, non-st…

…ring slurpable things will slurp that text into the widget. Applies to :text property and (text!).
  • Loading branch information...
1 parent 798fe37 commit 4f1f99aa04bf079f52fc4ffb8171aa7ac986bee6 @daveray committed Jul 25, 2011
Showing with 43 additions and 7 deletions.
  1. +33 −5 src/seesaw/core.clj
  2. +9 −2 test/seesaw/test/core.clj
  3. +1 −0 test/seesaw/test/core.text.txt
@@ -16,7 +16,8 @@
:author "Dave Ray"}
(:use [seesaw util meta to-widget make-widget])
- (:require [seesaw color font border invoke timer selection
+ (:require
+ [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? v) (slurp v)
+ :else (str v)))
(extend-protocol Text
(set-text [this v] (set-text (to-widget this) v))
(get-text [this] (get-text (to-widget this)))
- (set-text [this v] (.setText this (str v)))
+ (set-text [this v] (.setText this (convert-text-value v)))
(get-text [this] (.getText this))
- (set-text [this v] (.setText this (str v)))
+ (set-text [this v] (.setText this (convert-text-value v)))
(get-text [this] (.getText this))
- (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)))
(set-text [this v] (set-text (.getDocument this) v))
(get-text [this] (get-text (.getDocument this)))
- (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,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)
user=> (def t (text \"HI\"))
user=> (text! t \"BYE\")
user=> (text t)
+ ; Put the contents of a URL in editor
+ (text! editor ( \"\"))
+ 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")
@@ -10,7 +10,8 @@
(ns seesaw.test.core
(:require [seesaw.selector :as selector]
- [seesaw.cursor :as cursor])
+ [seesaw.cursor :as cursor]
(:use seesaw.core
@@ -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 ( "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"
@@ -0,0 +1 @@
+Some text in a resource

0 comments on commit 4f1f99a

Please sign in to comment.