Skip to content

Commit

Permalink
Generalized MB's :resource option handling so it can be used for subs…
Browse files Browse the repository at this point in the history
…ets of properties on anything.
  • Loading branch information
daveray committed Sep 3, 2011
1 parent 2d735e6 commit 29b6dad
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
18 changes: 2 additions & 16 deletions src/seesaw/action.clj
Expand Up @@ -23,21 +23,6 @@
(default-option name
(fn [^Action target value] (.putValue target key ((or set-conv identity) value))))))

(declare action-options)

(defn- configure-action-from-resource
[target value]
{:pre [(keyword? value) (namespace value)]}
(let [nspace (namespace value)
prefix (name value)]
(reapply-options
target (mapcat (fn [k]
(let [prop (keyword nspace (str prefix "." k))]
(when-let [v (translate prop)]
[(keyword k) v])))
["name" "command" "tip" "icon" "key" "mnemonic"])
action-options)))

; store the handler function in a property on the action.
(def ^{:private true} action-handler-property "seesaw-action-handler")
(def ^{:private true} action-options {
Expand All @@ -53,7 +38,8 @@
(let [v (if (char? v) (int (Character/toUpperCase (char v))) (int v))]
(.putValue a Action/MNEMONIC_KEY v))))
:handler (default-option :handler #(put-meta! %1 action-handler-property %2))
:resource (default-option :resource configure-action-from-resource)
:resource (resource-option :resource [:name :command :tip :icon :key :mnemonic])
;:resource (default-option :resource configure-action-from-resource)
})

(defn action
Expand Down
28 changes: 28 additions & 0 deletions src/seesaw/options.clj
Expand Up @@ -74,6 +74,34 @@
[name]
(default-option name (fn [_ _]) (fn [_ _])))

(declare reapply-options)

(defn resource-option
"Defines an option that takes a j18n namespace-qualified keyword as a
value. The keyword is used as a prefix for the set of properties in
the given key list. This allows subsets of widget options to be configured
from a resource bundle.
Example:
; The :resource property looks in a resource bundle for
; prefix.text, prefix.foreground, etc.
(resource-option :resource [:text :foreground :background])
"
[option-name keys]
(default-option
option-name
(fn [target value]
{:pre [(keyword? value) (namespace value)]}
(let [nspace (namespace value)
prefix (name value)]
(reapply-options
target (mapcat (fn [k]
(let [prop (keyword nspace (str prefix "." k))]
(when-let [v (translate prop)]
[(keyword k) v])))
(map name keys))
nil)))))

(defn- apply-option
[target ^Option opt v]
(if-let [setter (.setter opt)]
Expand Down
19 changes: 17 additions & 2 deletions test/seesaw/test/options.clj
Expand Up @@ -9,10 +9,13 @@
; You must not remove this notice, or any other, from this software.

(ns seesaw.test.options
(:use seesaw.options)
(:use [lazytest.describe :only (describe it testing)]
(:require [j18n.core :as j18n])
(:use seesaw.options
[lazytest.describe :only (describe it testing)]
[lazytest.expect :only (expect)]))

(j18n/defbundle MyBundle)

(describe apply-options
(it "throws IllegalArgumentException if properties aren't even"
(try
Expand Down Expand Up @@ -42,3 +45,15 @@
(it "uses the getter of an option to retrieve a value"
(= "hi" (get-option-value (javax.swing.JPanel.) :text {:text (default-option :text nil (constantly "hi"))}))))

(describe resource-option
(it "has a setter that applies options using values from resource bundle"
(do
(j18n/init-bundles!)
(let [l (apply-options (javax.swing.JLabel.)
[:resource ::resource-option]
{:resource (resource-option :resource [:text :name])
:text (bean-option :text javax.swing.JLabel)
:name (bean-option :name javax.swing.JLabel) })]
(expect (= "expected text" (.getText l)))
(expect (= "expected name" (.getName l)))))))

3 changes: 3 additions & 0 deletions test/seesaw/test/options.properties
@@ -0,0 +1,3 @@
# See options.clj
resource-option.text=expected\ text
resource-option.name=expected\ name

0 comments on commit 29b6dad

Please sign in to comment.