Permalink
Browse files

Generalized MB's :resource option handling so it can be used for subs…

…ets of properties on anything.
  • Loading branch information...
1 parent 2d735e6 commit 29b6dad53803e54b73f222fac10272e5de53a2ca @daveray committed Sep 3, 2011
Showing with 50 additions and 18 deletions.
  1. +2 −16 src/seesaw/action.clj
  2. +28 −0 src/seesaw/options.clj
  3. +17 −2 test/seesaw/test/options.clj
  4. +3 −0 test/seesaw/test/options.properties
View
@@ -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 {
@@ -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
View
@@ -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)]
@@ -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
@@ -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)))))))
+
@@ -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.