Permalink
Browse files

Made a util function for checking for an i18n key.

  • Loading branch information...
1 parent dbbbf79 commit 01a30f2c96a071b88622ada2c83c91a52d8a87fb @daveray committed Dec 7, 2011
View
@@ -12,7 +12,7 @@
:author "Dave Ray"}
seesaw.border
(:use [seesaw.color :only [to-color]]
- [seesaw.util :only [to-insets resource]])
+ [seesaw.util :only [to-insets resource resource-key?]])
(:import [javax.swing BorderFactory]
[javax.swing.border Border]
[java.awt Color]))
@@ -146,12 +146,12 @@
"
([b]
(cond
- (nil? b) nil
- (instance? Border b) b
- (integer? b) (empty-border :thickness b)
- (coll? b) (apply to-border b)
- (and (keyword? b) (namespace b)) (to-border (resource b))
- :else (BorderFactory/createTitledBorder (str b))))
+ (nil? b) nil
+ (instance? Border b) b
+ (integer? b) (empty-border :thickness b)
+ (coll? b) (apply to-border b)
+ (resource-key? b) (to-border (resource b))
+ :else (BorderFactory/createTitledBorder (str b))))
([b & args]
(apply compound-border b args)))
View
@@ -12,7 +12,7 @@
in the core color options."
:author "Dave Ray"}
seesaw.color
- (:use [seesaw.util :only (illegal-argument resource)])
+ (:use [seesaw.util :only (illegal-argument resource resource-key?)])
(:import [java.awt Color]))
(def ^{:private true} color-names {
@@ -206,7 +206,7 @@
http://www.w3.org/TR/css3-color/
"
([s]
- (if (and (keyword? s) (namespace s))
+ (if (resource-key? s)
(color (resource s))
(or (color-names (.toLowerCase (name s))) (decode (name s)))))
([s a] (apply color (assoc (get-rgba (color s)) 3 a)))
View
@@ -16,8 +16,8 @@
:author "Dave Ray"}
seesaw.core
(:use [seesaw.util :only [illegal-argument to-seq check-args
- constant-map resource to-dimension
- to-insets to-url try-cast
+ constant-map resource resource-key?
+ to-dimension to-insets to-url try-cast
cond-doto to-mnemonic-keycode]]
[seesaw.config :only [Configurable config* config!*]]
[seesaw.options :only [ignore-option default-option bean-option resource-option around-option
@@ -622,10 +622,10 @@
(defn- convert-text-value [v]
(cond
- (nil? v) v
- (string? v) v
- (number? v) (str v)
- (and (keyword? v) (namespace v)) (resource v)
+ (nil? v) v
+ (string? v) v
+ (number? v) (str v)
+ (resource-key? v) (resource v)
(satisfies? clojure.java.io/IOFactory v) (slurp v)
; TODO This line is unreachable because the IOFactory protocol is
; extended to Object, i.e. satisfies? above will *always* return
View
@@ -12,7 +12,7 @@
use these implicitly through the :font option."
:author "Dave Ray"}
seesaw.font
- (:use [seesaw.util :only [constant-map resource]])
+ (:use [seesaw.util :only [constant-map resource resource-key?]])
(:import [java.awt Font]))
(def ^{:private true} style-table (constant-map Font :bold :plain :italic))
@@ -56,7 +56,7 @@
[& args]
(if (= 1 (count args))
(let [v (first args)]
- (if (and (keyword? v) (namespace v))
+ (if (resource-key? v)
(Font/decode (resource v))
(Font/decode (str v))))
(let [{:keys [style size from] :as opts} args
View
@@ -11,7 +11,7 @@
(ns ^{:doc "Functions for loading and creating icons."
:author "Dave Ray"}
seesaw.icon
- (:use [seesaw.util :only [resource to-url]])
+ (:use [seesaw.util :only [resource resource-key? to-url]])
(:require [clojure.java.io :as jio])
(:import [javax.swing ImageIcon]))
@@ -36,11 +36,11 @@
[p]
(cond
(nil? p) nil
- (instance? javax.swing.Icon p) p
- (instance? java.awt.Image p) (ImageIcon. ^java.awt.Image p)
- (instance? java.net.URL p) (ImageIcon. ^java.net.URL p)
- (instance? java.io.File p) (ImageIcon. (.getAbsolutePath ^java.io.File p))
- (and (keyword? p) (namespace p)) (icon (resource p))
+ (instance? javax.swing.Icon p) p
+ (instance? java.awt.Image p) (ImageIcon. ^java.awt.Image p)
+ (instance? java.net.URL p) (ImageIcon. ^java.net.URL p)
+ (instance? java.io.File p) (ImageIcon. (.getAbsolutePath ^java.io.File p))
+ (resource-key? p) (icon (resource p))
:else
(if-let [url (jio/resource (str p))]
(icon url)
View
@@ -9,7 +9,7 @@
; You must not remove this notice, or any other, from this software.
(ns seesaw.keystroke
- (:use [seesaw.util :only [illegal-argument resource]])
+ (:use [seesaw.util :only [illegal-argument resource resource-key?]])
(:import [javax.swing KeyStroke]
[java.awt Toolkit]
[java.awt.event InputEvent])
@@ -43,10 +43,10 @@
See http://download.oracle.com/javase/6/docs/api/javax/swing/KeyStroke.html#getKeyStroke(java.lang.String)"
[arg]
(cond
- (nil? arg) nil
+ (nil? arg) nil
(instance? KeyStroke arg) arg
- (char? arg) (KeyStroke/getKeyStroke ^Character arg)
- (and (keyword? arg) (namespace arg)) (keystroke (resource arg))
+ (char? arg) (KeyStroke/getKeyStroke ^Character arg)
+ (resource-key? arg) (keystroke (resource arg))
:else (if-let [ks (KeyStroke/getKeyStroke ^String (preprocess-descriptor (str arg)))]
ks
(illegal-argument "Invalid keystroke descriptor: %s" arg))))
View
@@ -12,7 +12,8 @@
:author "Dave Ray"}
seesaw.options
(:use [seesaw.meta :only [get-meta put-meta!]]
- [seesaw.util :only [camelize illegal-argument resource check-args]]))
+ [seesaw.util :only [camelize illegal-argument check-args
+ resource resource-key?]]))
(defrecord Option [name setter getter])
@@ -92,7 +93,7 @@
(default-option
option-name
(fn [target value]
- {:pre [(keyword? value) (namespace value)]}
+ {:pre [(resource-key? value)]}
(let [nspace (namespace value)
prefix (name value)]
(reapply-options
View
@@ -167,9 +167,14 @@
children
root))
+(defn resource-key?
+ "Returns true if v is a i18n resource key, i.e. a namespaced keyword"
+ [v]
+ (and (keyword? v) (namespace v)))
+
(defn resource
[message]
- (if (and (keyword? message) (namespace message))
+ (if (resource-key? message)
(j18n/resource message)
(str message)))
@@ -188,8 +193,8 @@
http://download.oracle.com/javase/6/docs/api/java/awt/event/KeyEvent.html"
[v]
(cond
- (and (keyword? v) (namespace v)) (to-mnemonic-keycode (resource v))
- (string? v) (to-mnemonic-keycode (.charAt v 0))
- (char? v) (int (Character/toUpperCase ^Character v))
- :else (int v)))
+ (resource-key? v) (to-mnemonic-keycode (resource v))
+ (string? v) (to-mnemonic-keycode (.charAt v 0))
+ (char? v) (int (Character/toUpperCase ^Character v))
+ :else (int v)))
@@ -128,3 +128,9 @@
(it "should convert a lower-case character to an integer key code"
(= (int \X) (to-mnemonic-keycode \x))))
+(describe resource-key?
+ (it "returns true for resource keywords"
+ (resource-key? ::hello))
+ (it "returns false for non-resource keywords"
+ (not (resource-key? :hello))))
+

0 comments on commit 01a30f2

Please sign in to comment.