Skip to content

Commit

Permalink
Extended selection functions to choosing the currently active tab in …
Browse files Browse the repository at this point in the history
…a tabbed panel.
  • Loading branch information
daveray committed Mar 5, 2012
1 parent 4dae6a4 commit 06b07a6
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/seesaw/core.clj
Expand Up @@ -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
"
Expand Down
26 changes: 25 additions & 1 deletion src/seesaw/selection.clj
Expand Up @@ -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
Expand Down Expand Up @@ -104,6 +105,29 @@
target
(.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]
Expand Down
45 changes: 45 additions & 0 deletions test/seesaw/test/selection.clj
Expand Up @@ -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.))))
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit 06b07a6

Please sign in to comment.