Skip to content
Browse files

Extended selection functions to choosing the currently active tab in …

…a tabbed panel.
  • Loading branch information...
1 parent 4dae6a4 commit 06b07a6ed6ae1f5e5e09c0c5e073adbdb9b31fed @daveray committed Mar 4, 2012
Showing with 83 additions and 1 deletion.
  1. +13 −0 src/seesaw/core.clj
  2. +25 −1 src/seesaw/selection.clj
  3. +45 −0 test/seesaw/test/selection.clj
View
13 src/seesaw/core.clj
@@ -2429,6 +2429,19 @@
Notes:
+ The currently selected tab can be retrieved with the (selection) function.
+ It returns a map similar to the tab descriptor with keys :title, :content,
+ and :index.
+
+ Similarly, a tab can be programmatically selected with the
+ (selection!) function, by passing one of the following values:
+
+ * A number - The index of the tab to select
+ * A string - The title of the tab to select
+ * A to-widget-able - The content of the tab to select
+ * A map as returned by (selection) with at least an :index, :title, or
+ :content key.
+
See:
http://download.oracle.com/javase/6/docs/api/javax/swing/JTabbedPane.html
"
View
26 src/seesaw/selection.clj
@@ -9,7 +9,8 @@
; You must not remove this notice, or any other, from this software.
(ns seesaw.selection
- (:use [seesaw.util :only [check-args]]))
+ (:use [seesaw.util :only [check-args]])
+ (:require [seesaw.to-widget]))
;TODO put this somewhere
; I think this is generally useful, but the main reason for its existence is
@@ -105,6 +106,29 @@
(.clearSelection target))))
(extend-protocol Selection
+ javax.swing.JTabbedPane
+ (get-selection [this]
+ (let [i (.getSelectedIndex this)]
+ (if (neg? i)
+ nil
+ [{:content (.getComponentAt this i)
+ :title (.getTitleAt this i)
+ :index i}])))
+ (set-selection [this [v]]
+ (cond
+ (nil? v) nil
+ (string? v)
+ (set-selection this [(.indexOfTab this ^String v)])
+ (and (number? v) (>= v 0))
+ (.setSelectedIndex this (int v))
+ (map? v)
+ (set-selection this [(or (:index v) (:title v) (:content v))])
+ (instance? java.awt.Component v)
+ (.setSelectedComponent this ^java.awt.Component v)
+ :else
+ (set-selection this [(seesaw.to-widget/to-widget* v)]))))
+
+(extend-protocol Selection
javax.swing.text.JTextComponent
(get-selection [target]
(let [start (.getSelectionStart target)
View
45 test/seesaw/test/selection.clj
@@ -83,6 +83,19 @@
(.select t 2 4)
(expect (= [2 4] (selection t))))))
+ (testing "when given a JTabbedPane"
+ (it "returns nil when there are no tabs"
+ (nil? (selection (javax.swing.JTabbedPane.))))
+ (it "returns {:index i :title \"the title\" :content widget} for the selected tab"
+ (let [a (sc/label :text "A")
+ b (sc/label :text "B")
+ tp (sc/tabbed-panel :tabs [{:title "A" :content a}
+ {:title "B" :content b}])]
+ (.setSelectedIndex tp 1)
+ (expect (= {:title "B" :content b :index 1} (selection tp)))
+ (.setSelectedIndex tp 0)
+ (expect (= {:title "A" :content a :index 0} (selection tp))))))
+
(testing "when given a JTable"
(it "returns nil when no rows are selected"
(nil? (selection (javax.swing.JTable.))))
@@ -171,6 +184,38 @@
(selection! t [4 9])
(expect (= [4 9] (selection t))))))
+ (testing "when given a JTabbedPane"
+ (it "selects a tab by title when given a string"
+ (let [tp (sc/tabbed-panel :tabs [{:title "A" :content "A"}
+ {:title "B" :content "B"}])]
+ (expect (= 0 (.getSelectedIndex tp)))
+ (selection! tp "B")
+ (expect (= 1 (.getSelectedIndex tp)))))
+ (it "selects a tab by index when given a number"
+ (let [tp (sc/tabbed-panel :tabs [{:title "A" :content "A"}
+ {:title "B" :content "B"}])]
+ (expect (= 0 (.getSelectedIndex tp)))
+ (selection! tp 1)
+ (expect (= 1 (.getSelectedIndex tp)))))
+ (it "selects a tab by content when given a widget"
+ (let [b (sc/label :text "B")
+ tp (sc/tabbed-panel :tabs [{:title "A" :content "A"}
+ {:title "B" :content b}])]
+ (selection! tp b)
+ (expect (= 1 (.getSelectedIndex tp)))))
+ (it "selects a tab by map keys"
+ (let [b (sc/label :text "B")
+ tp (sc/tabbed-panel :tabs [{:title "A" :content "A"}
+ {:title "B" :content b}])]
+ (selection! tp {:index 1})
+ (expect (= 1 (.getSelectedIndex tp)))
+
+ (selection! tp {:title "A"})
+ (expect (= 0 (.getSelectedIndex tp)))
+
+ (selection! tp {:content b})
+ (expect (= 1 (.getSelectedIndex tp))))))
+
(testing "when given a JTable and an argument"
(it "Clears the row selection when the argument is nil"
(let [jtable (javax.swing.JTable. 5 3)]

0 comments on commit 06b07a6

Please sign in to comment.
Something went wrong with that request. Please try again.