Permalink
Browse files

fix for issue #113. If row index is out of bounds, value-at returns nil.

  • Loading branch information...
1 parent 0ffe6f9 commit eaf8800f6b6247a04dea3021107e21e882f8437f @daveray committed Dec 19, 2012
Showing with 23 additions and 9 deletions.
  1. +12 −8 src/seesaw/table.clj
  2. +11 −1 test/seesaw/test/table.clj
View
20 src/seesaw/table.clj
@@ -143,19 +143,23 @@
(defn- single-value-at
[^javax.swing.table.TableModel model col-key-map row]
- (let [full-row (get-full-value model row)]
- (merge
- full-row
- (reduce
- (fn [result k] (assoc result k (.getValueAt model row (col-key-map k))))
- {}
- (keys col-key-map)))))
+ (if (and (>= row 0) (< row (.getRowCount model)))
+ (let [full-row (get-full-value model row)]
+ (merge
+ full-row
+ (reduce
+ (fn [result k] (assoc result k (.getValueAt model row (col-key-map k))))
+ {}
+ (keys col-key-map))))
+ nil))
(defn value-at
"Retrieve one or more rows from a table or table model. target is a JTable or TableModel.
rows is either a single integer row index, or a sequence of row indices. In the first case
a single map of row values is returns. Otherwise, returns a sequence of maps.
+ If a row index is out of bounds, returns nil.
+
Notes:
If target was not created with (table-model), the returned map(s) are indexed
@@ -226,7 +230,7 @@
target)
([target row value & more]
(when more
- (apply update-at! target more))
+ (apply update-at! target more))
(update-at! target row value)))
(defn insert-at!
View
12 test/seesaw/test/table.clj
@@ -79,7 +79,17 @@
(expect (= {"A" nil "B" nil "C" nil } (value-at t 0)))))
(it "gets the value of a sequence of row indices as a list of maps"
(let [t (table-model :columns [:a :b] :rows [["a0" "b0"] ["a1" "b1"]])]
- (expect (= [{:a "a0" :b "b0" } {:a "a1" :b "b1" }] (value-at t [0 1]))))))
+ (expect (= [{:a "a0" :b "b0" } {:a "a1" :b "b1" }] (value-at t [0 1])))))
+ (it "returns nil for an out of bounds row index"
+ (let [t (table-model :columns [{:key :a :text "a"} {:key :b :text "b"}]
+ :rows [[:a "bee" :b "cee"] [:a "tree" :b "four"]])]
+ (expect (nil? (value-at t 9)))))
+ (it "survives an out-of-bounds value-at call"
+ (let [t (table-model :columns [{:key :a :text "a"} {:key :b :text "b"}]
+ :rows [{:a "bee" :b "cee"} {:a "tree" :b "four"}])]
+ (expect (= {:a "bee" :b "cee"} (value-at t 0)))
+ (try (value-at t 9) (catch Exception e))
+ (expect (= {:a "bee" :b "cee"} (value-at t 0))))))
(describe update-at!
(it "updates a row with the same format as :rows option of (table-model)"

0 comments on commit eaf8800

Please sign in to comment.