Permalink
Browse files

Being honest about functions with sideeffects. Changed (config) to (c…

…onfig!)
  • Loading branch information...
1 parent 590e6b4 commit 570d604d083d3b7e0acf9cc797f66ea376c5a86c @daveray committed May 1, 2011
View
@@ -84,11 +84,11 @@ All of Seesaw's widget creation functions (`label`, `text`, `horizontal-panel`,
... and many more. See code and tests for details.
-Note that these properties can also be used with the `(config)` function which applies them to an existing widget or widgets:
+Note that these properties can also be used with the `(config!)` function which applies them to an existing widget or widgets:
- (config (select [:#my-widget]) :enabled? false :text "I'm disabled.")
+ (config! (select [:#my-widget]) :enabled? false :text "I'm disabled.")
-`(config)` can be applied to a single widget, or list of widgets, or things that can be turned into widgets.
+`(config!)` can be applied to a single widget, or list of widgets, or things that can be turned into widgets.
### Selectors
@@ -105,7 +105,7 @@ _I'm not totally happy with this and it will probably change. Selecting by id be
The "all" selector is also supported which will match everything in a sub-tree including the root. For example to disable an entire sub-tree:
- (config (select [:*] my-panel) :enabled? false)
+ (config! (select [:*] my-panel) :enabled? false)
At the moment, I'm planning on following the selector conventions established by [Enlive] (https://github.com/cgrand/enlive). See also, the apparently defunct [Java CSS] (http://weblogs.java.net/blog/2008/07/17/introducing-java-css) project to get an idea where this may lead.
@@ -172,11 +172,11 @@ It's typical in Swing apps to use actions for menus, buttons, etc. An action nee
`(action)` also supports an `:icon` property which can be a `javax.swing.Icon`, a `java.net.URL` or something that looks like a file or URL after `(str)` has been applied to it. See `seesaw/action.clj` for an accurate list of options.
-Like widgets, actions can be modified with the `(config)` function:
+Like widgets, actions can be modified with the `(config!)` function:
(def a (action :name "Fire Missiles" :enabled? false))
- (config a :name "Fire Missiles!!!" :enabled? true :handler (fn [e] (println "FIRE")))
+ (config! a :name "Fire Missiles!!!" :enabled? true :handler (fn [e] (println "FIRE")))
### Menus
Here's how you can make a menu bar full of menus:
View
@@ -249,14 +249,14 @@
(config* [target args]
(reapply-options target args default-options)))
-(defn config
+(defn config!
"Applies properties in the argument list to one or more targets. For example:
- (config button1 :enabled? false :text \"I' disabled\")
+ (config! button1 :enabled? false :text \"I' disabled\")
or:
- (config [button1 button2] :enabled? false :text \"We're disabled\")
+ (config! [button1 button2] :enabled? false :text \"We're disabled\")
Targets may be actual widgets, or convertible to widgets with (to-widget).
For example, the target can be an event object.
@@ -846,7 +846,7 @@
{:before fn :after fn} - a map with :before and :after functions which
are called before and after super.paintComponent respectively.
- Note that (config) can be used to change the :paint property at any time.
+ Note that (config!) can be used to change the :paint property at any time.
Here's an example:
@@ -53,11 +53,11 @@
(.drawString "Hello. This is a canvas example" 20 20))))
; Create an action that swaps the paint handler for the canvas.
-; Note that we can use (config) to set the :paint handler just like
+; Note that we can use (config!) to set the :paint handler just like
; properties on other widgets.
(defn switch-paint-action [n paint]
(action :name n
- :handler (fn [e] (config (select [:#canvas]) :paint paint))))
+ :handler (fn [e] (config! (select [:#canvas]) :paint paint))))
(defn app
[]
@@ -7,11 +7,11 @@
; Define a render function. The first arg is the renderer (an instance of
; DefaultListCellRenderer). The second holds the state of the cell we're
; rendering. We use :value to get the model value of the cell. Note
-; that renderer is just a JLabel, so we can call config on it and set
+; that renderer is just a JLabel, so we can call config! on it and set
; properties in the normal way.
(defn render-fn [renderer info]
(let [v (:value info)]
- (apply config renderer
+ (apply config! renderer
(if (even? v)
[:text (str v " is even") :font even-font :foreground "#000033"]
[:text (str v " is odd") :font odd-font :foreground "#aaaaee"]))))
@@ -25,7 +25,7 @@
; Render thing with right names and system icons
(defn render-file-item
[renderer {:keys [value]}]
- (config renderer :text (.getName value)
+ (config! renderer :text (.getName value)
:icon (.getIcon chooser value)))
(defn app []
@@ -45,9 +45,9 @@
(fn [e]
(if-let [dir (-> (selection e) first last)]
(let [files (.listFiles dir)]
- (config (select [:#current-dir]) :text (.getAbsolutePath dir))
- (config (select [:#status]) :text (format "Ready (%d items)" (count files)))
- (config (select [:#list]) :model files))))))
+ (config! (select [:#current-dir]) :text (.getAbsolutePath dir))
+ (config! (select [:#status]) :text (format "Ready (%d items)" (count files)))
+ (config! (select [:#list]) :model files))))))
(defn -main [& args]
(invoke-later (app)))
@@ -118,12 +118,12 @@
(listen (select [:#link])
:mouse-clicked #(alert % "CLICK!")
- :mouse-entered #(config % :foreground Color/BLUE)
- :mouse-exited #(config % :foreground Color/BLACK))
+ :mouse-entered #(config! % :foreground Color/BLUE)
+ :mouse-exited #(config! % :foreground Color/BLACK))
(listen (select [:#check-me]) :selection ; or :item-state-changed
(fn [e]
- (config (select [:#link]) :enabled? (first (selection e)))))
+ (config! (select [:#link]) :enabled? (first (selection e)))))
(listen (select [:#combo]) :selection ; or :item-state-changed
(fn [e] (println (selection e))))
@@ -24,7 +24,7 @@
; A custom renderer so that XML elements are displayed nicely
(defn render-fn [renderer info]
(let [v (:value info)]
- (config renderer
+ (config! renderer
:text (if (map? v)
(format "<%s>" (name (:tag v)))
v))))
@@ -42,7 +42,7 @@
; Listen for selection changes and show them in the label
(listen (select [:#tree]) :selection
(fn [e]
- (config (select [:#sel])
+ (config! (select [:#sel])
:text (str "Selection: " (-> e selection first last))))))
(defn -main [& args]
@@ -20,7 +20,7 @@
(let [expected-font (font :name "ARIAL-BOLD-18")
jlist (javax.swing.JList.)
render-fn (fn [renderer info]
- (config renderer :foreground java.awt.Color/YELLOW
+ (config! renderer :foreground java.awt.Color/YELLOW
:text "hi"
:icon nil
:font expected-font))
@@ -36,7 +36,7 @@
(let [expected-font (font :name "ARIAL-BOLD-18")
jtree (javax.swing.JTree.)
render-fn (fn [renderer info]
- (config renderer :foreground java.awt.Color/YELLOW
+ (config! renderer :foreground java.awt.Color/YELLOW
:text "hi"
:icon nil
:font expected-font))
@@ -40,7 +40,7 @@
(expect "hi" (-> (JLabel.) (apply-default-opts {:id "hi"}) id-for)))
(it "throws IllegalStateException if the widget's id is already set"
(try
- (do (config (label :id :foo) :id :bar) false)
+ (do (config! (label :id :foo) :id :bar) false)
(catch IllegalStateException e true))))
(testing "the :preferred-size option"
(it "set the component's preferred size using to-dimension"
@@ -165,17 +165,17 @@
(expect (= (.getDocument t) (to-document t))))))
-(describe config
+(describe config!
(it "configures the properties given to it on a single target"
(let [p (JPanel.)
- result (config p :foreground Color/RED :background Color/BLUE :enabled? false)]
+ result (config! p :foreground Color/RED :background Color/BLUE :enabled? false)]
(expect (= p result))
(expect (= Color/RED (.getForeground p)))
(expect (= Color/BLUE (.getBackground p)))
(expect (not (.isEnabled p)))))
(it "configures the properties given to it on a multiple targets"
(let [targets [(JPanel.) (JPanel.)]
- result (config targets :foreground Color/RED :background Color/BLUE :enabled? false)]
+ result (config! targets :foreground Color/RED :background Color/BLUE :enabled? false)]
(expect (= targets result))
(expect (= Color/RED (.getForeground (first targets))))
(expect (= Color/BLUE (.getBackground (first targets))))
@@ -185,10 +185,10 @@
(expect (not (.isEnabled (second targets))))))
(it "configures a target with type-specific properties"
(let [t (toggle :text "hi" :selected? false)]
- (expect (.isSelected (config t :selected? true)))))
+ (expect (.isSelected (config! t :selected? true)))))
(it "can configure an action"
(let [a (action :name "foo")]
- (config a :name "bar")
+ (config! a :name "bar")
(expect (= "bar" (.getValue a Action/NAME))))))
(describe flow-panel

0 comments on commit 570d604

Please sign in to comment.