Skip to content
Browse files

Implemented scroll for table :row, :column, and :cell

  • Loading branch information...
1 parent c5b221c commit 283c1689b96b7e739fcb1a8cc94f971054d46455 @daveray committed Jul 23, 2011
Showing with 67 additions and 5 deletions.
  1. +42 −1 src/seesaw/examples/scroll.clj
  2. +24 −4 src/seesaw/scroll.clj
  3. +1 −0 src/seesaw/selection.clj
View
43 src/seesaw/examples/scroll.clj
@@ -53,10 +53,51 @@
(let [jlist (listbox :model (range 0 1000))]
(test-panel jlist [(top jlist) (bottom jlist) (jlist-row jlist)])))
+(defn jtable-op [jtable op-name]
+ (let [arg (text :columns 10)
+ go-action (action :name "Scroll!"
+ :handler (fn [e]
+ (scroll! jtable :to [op-name (Integer/valueOf (text arg))])
+ (selection! jtable (Integer/valueOf (text arg)))))
+ go-button (button :action go-action)]
+ (bind/bind arg
+ (bind/transform #(format "(scroll! v :to [%s %s])" op-name %))
+ (bind/property go-button :text))
+ (text! arg "200")
+ (horizontal-panel :items [arg go-button])))
+
+(defn jtable-op-2 [jtable op-name]
+ (let [arg0 (text :columns 10)
+ arg1 (text :columns 10)
+ go-action (action :name "Scroll!"
+ :handler (fn [e]
+ (scroll! jtable :to [op-name
+ (Integer/valueOf (text arg0))
+ (Integer/valueOf (text arg1))])))
+ go-button (button :action go-action)]
+ (listen #{arg0 arg1} :document
+ (fn [e]
+ (text! go-button (format "(scroll! v :to [%s %s %s])" op-name (text arg0) (text arg1)))))
+ (text! [arg0 arg1] "20")
+ (horizontal-panel :items [arg0 arg1 go-button])))
+
+(defn jtable []
+ (let [columns (map #(-> ( format "c%09d" %) keyword) (range 26))
+ jtable (table :model [:columns columns
+ :rows (repeat 500 (into {} (for [c columns] [c 100])))])]
+ (test-panel
+ (doto jtable
+ (.setAutoResizeMode javax.swing.JTable/AUTO_RESIZE_OFF))
+ [(top jtable) (bottom jtable)
+ (jtable-op jtable :row)
+ (jtable-op jtable :column)
+ (jtable-op-2 jtable :cell)])))
+
(defn app-panel []
(tabbed-panel
:tabs [{:title "General" :content (general)}
- {:title "listbox" :content (jlist)}]))
+ {:title "listbox" :content (jlist)}
+ {:title "table" :content (jtable)}]))
(defn -main [& args]
(invoke-later
View
28 src/seesaw/scroll.clj
@@ -10,7 +10,8 @@
(ns ^{:doc "Functions for dealing with scrolling. Prefer (seesaw.core/scroll!)."
:author "Dave Ray"}
- seesaw.scroll)
+ seesaw.scroll
+ (:use [seesaw.util]))
(defprotocol ^{:private true} ScrollImpl
(get-handlers [this arg]))
@@ -28,14 +29,28 @@
(.getCellBounds target row row))
})
+(def ^{:private true} table-handlers {
+ :row (fn [^javax.swing.JTable target ^Integer row]
+ (.getCellRect target row 0 false))
+ :column (fn [^javax.swing.JTable target ^Integer column]
+ (.getCellRect target 0 column false))
+ :cell (fn [^javax.swing.JTable target ^Integer row ^Integer column]
+ (.getCellRect target row column false))
+})
+
+(defn- lookup-handler [handlers type]
+ (if-let [h (handlers type)]
+ h
+ (throw (IllegalArgumentException. (str "Unknown scroll op " type)))))
+
(defn- ^java.awt.Rectangle to-rect [target v handlers]
(cond
(instance? java.awt.Rectangle v) v
(instance? java.awt.Point v) (java.awt.Rectangle. ^java.awt.Point v)
- (keyword? v) ((handlers v) target)
+ (keyword? v) ((lookup-handler handlers v) target)
(instance? clojure.lang.PersistentVector v)
(let [[type & args] v]
- (apply (handlers type) target args))))
+ (apply (lookup-handler handlers type) target args))))
(defn- scroll-rect-to-visible [^javax.swing.JComponent target rect]
(when rect
@@ -45,9 +60,14 @@
javax.swing.JComponent (get-handlers [this arg] default-handlers)
javax.swing.JList
- (get-handlers [this arg] (merge default-handlers list-handlers)))
+ (get-handlers [this arg] (merge default-handlers list-handlers))
+
+ javax.swing.JTable
+ (get-handlers [this arg] (merge default-handlers table-handlers))
+ )
(defn scroll!* [target action arg]
+ (check-args (not (nil? target)) "target of scroll!* cannot be nil")
(condp = action
:to (scroll-rect-to-visible target (to-rect target arg (get-handlers target arg)))))
View
1 src/seesaw/selection.clj
@@ -108,6 +108,7 @@
(defn selection!
([target values] (selection! target {} values))
([target opts values]
+ (check-args (not (nil? target)) "target of selection! cannot be nil")
(set-selection
target
(if (or (nil? values) (:multi? opts)) values [values]) )

0 comments on commit 283c168

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