Permalink
Browse files

Started working on the behavior code. Helpers for text behavior and m…

…ouse dragging.
  • Loading branch information...
1 parent a1f3dfe commit 164073c788a0bf67ea15533d46c81dc2215ab358 @daveray committed Jun 9, 2011
Showing with 152 additions and 56 deletions.
  1. +73 −0 src/seesaw/behave.clj
  2. +35 −0 src/seesaw/examples/behave.clj
  3. +19 −56 src/seesaw/examples/xyz_panel.clj
  4. +25 −0 test/seesaw/test/behave.clj
View
73 src/seesaw/behave.clj
@@ -0,0 +1,73 @@
+; Copyright (c) Dave Ray, 2011. All rights reserved.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+; which can be found in the file epl-v10.html at the root of this
+; distribution.
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+; You must not remove this notice, or any other, from this software.
+
+(ns seesaw.behave
+ "A collection of basic behaviors that can be dynamically added to widgets.
+ Most cover basic functionality that's missing from swing or just a pain
+ to implement."
+ (:use [seesaw core util]))
+
+(defn when-focused-select-all
+ "A helper function which adds a \"select all when focus gained\" behavior to one
+ or more text widgets or editable comboboxes.
+
+ Like (seesaw.core/listen) returns a function which will remove all event handlers
+ when called.
+
+ Examples:
+
+ (flow-panel :items [
+ \"Enter some text here: \"
+ (doto
+ (text \"All this text will be selected when I get keyboard focus\")
+ when-focused-select-all)])
+
+ See:
+ "
+ [w]
+ (let [to-text #(if (instance? javax.swing.JComboBox %)
+ (.. % getEditor getEditorComponent) %)
+ targets (map #(-> % to-widget to-text) (to-seq w))]
+ (listen targets :focus-gained
+ #(do (println "HERE")(.selectAll (to-widget %))))))
+
+(defn when-mouse-dragged
+ "A helper for handling mouse dragging on a widget. This isn't that complicated,
+ but the default mouse dragged event provided with Swing doesn't give the delta
+ since the last drag event so you end up having to keep track of it. This function
+ takes three options:
+
+ :start event handler called when the drag is started (mouse pressed).
+ :drag A function that takes a mouse event and a [dx dy] vector which is
+ the change in x and y since the last drag event.
+ :finish event handler called when the drag is finished (mouse released).
+
+ Like (seesaw.core/listen) returns a function which will remove all event handlers
+ when called.
+
+ Examples:
+ See (seesaw.examples.xyz-panel)
+ "
+ [w & opts]
+ (let [{:keys [start drag finish]
+ :or { start (fn [e]) drag (fn [e]) finish (fn [e]) }} opts
+ last-point (java.awt.Point.)]
+ (listen w
+ :mouse-pressed
+ (fn [e]
+ (.setLocation last-point (.getPoint e))
+ (start e))
+ :mouse-dragged
+ (fn [e]
+ (let [p (.getPoint e)]
+ (drag e [(- (.x p) (.x last-point)) (- (.y p) (.y last-point))])))
+ :mouse-released
+ finish)))
+
View
35 src/seesaw/examples/behave.clj
@@ -0,0 +1,35 @@
+; Copyright (c) Dave Ray, 2011. All rights reserved.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+; which can be found in the file epl-v10.html at the root of this
+; distribution.
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+; You must not remove this notice, or any other, from this software.
+
+(ns seesaw.examples.behave
+ (:use [seesaw core behave]))
+
+; Examples of stuff in (seesaw.behave).
+
+(defn focus-select []
+ (vertical-panel
+ :border "Demonstrates use of when-focused-select-all"
+ :items [
+ (doto
+ (text "All this will be selected when I get focus")
+ when-focused-select-all)
+ :separator
+ (doto
+ (combobox :editable? true :model ["Same here. Hit tab!" "First" "Second" "Third"])
+ when-focused-select-all)]))
+
+(defn -main [& args]
+ (invoke-later
+ (-> (frame :title "seesaw.behave examples" :content (focus-select))
+ pack!
+ show!)))
+
+(-main)
+
View
75 src/seesaw/examples/xyz_panel.clj
@@ -9,79 +9,42 @@
; You must not remove this notice, or any other, from this software.
(ns seesaw.examples.xyz-panel
- (:use [seesaw core border])
+ (:use [seesaw core border behave])
(:import [javax.swing SwingUtilities]))
-(defn when-mouse-dragged
- "A helper for handling mouse dragging on a widget. This isn't that complicated,
- but the default mouse dragged event provided with Swing doesn't give the delta
- since the last drag event so you end up having to keep track of it. This function
- takes three options:
- :start event handler called when the drag is started (mouse pressed).
- :drag A function that takes a mouse event and a [dx dy] vector which is
- the change in x and y since the last drag event.
- :finish event handler called when the drag is finished (mouse released).
-
- Like (seesaw.core/listen) returns a function which will remove all event handlers
- when called.
-
- Examples:
-
-
- See:
-
- "
- [w & opts]
- (let [{:keys [start drag finish]
- :or { start (fn [e]) drag (fn [e]) finish (fn [e]) }} opts
- last-point (java.awt.Point.)]
- (listen w
- :mouse-pressed
- (fn [e]
- (.setLocation last-point (.getPoint e))
- (start e))
- :mouse-dragged
- (fn [e]
- (let [p (.getPoint e)]
- (drag e [(- (.x p) (.x last-point)) (- (.y p) (.y last-point))])))
- :mouse-released
- finish)))
-
-; Put in some basic dragging support.
-(defn draggable [w]
+; Put in some basic support for moving w around.
+(defn movable [w]
(when-mouse-dragged w
; When the mouse is pressed, move the widget to the front of the z order
- :start (fn [e] (move! w :to-front))
+ :start #(move! % :to-front)
; When the mouse is dragged move the widget
- :drag
- (fn [e delta] (move! w :by delta)))
+ :drag #(move! %1 :by %2))
w)
(defn make-label
[text]
- (config!
- (label
- :text text
- :location [(rand-int 300) (rand-int 300)]
- :border (line-border :thickness 2 :color "#FFFFFF")
- :background "#DDDDDD")
+ (doto (label
+ :text text
+ :location [(rand-int 300) (rand-int 300)]
+ :border (line-border :thickness 2 :color "#FFFFFF")
+ :background "#DDDDDD")
; Set the bounds to its preferred size. Note that this has to be
; done after the label is fully constructed.
- :bounds :preferred))
+ (config! :bounds :preferred)))
(defn make-panel []
(xyz-panel
:id :xyz
:background "#000000"
:items (conj
- (map (comp draggable make-label) ["Agent Cooper" "Big Ed" "Leland Palmer"])
- (draggable
- (config! (border-panel
- :border (line-border :top 15 :color "#0000BB")
- :north (label "I'm a draggable label with a text box!")
- :center (text :text "Hey type some stuff here"))
- :bounds :preferred)))))
+ (map (comp movable make-label) ["Agent Cooper" "Big Ed" "Leland Palmer"])
+ (doto (border-panel
+ :border (line-border :top 15 :color "#0000BB")
+ :north (label "I'm a draggable label with a text box!")
+ :center (text :text "Hey type some stuff here"))
+ (config! :bounds :preferred)
+ movable))))
(defn -main [& args]
(invoke-later
@@ -93,5 +56,5 @@
:north "Demonstration of an xyz-panel with draggable widgets. Try dragging one!"
:center (make-panel))
:width 600 :height 600))))
-;(-main)
+(-main)
View
25 test/seesaw/test/behave.clj
@@ -0,0 +1,25 @@
+; Copyright (c) Dave Ray, 2011. All rights reserved.
+
+; The use and distribution terms for this software are covered by the
+; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+; which can be found in the file epl-v10.html at the root of this
+; distribution.
+; By using this software in any fashion, you are agreeing to be bound by
+; the terms of this license.
+; You must not remove this notice, or any other, from this software.
+
+(ns seesaw.test.behave
+ (:use seesaw.behave)
+ (:use seesaw.core)
+ (:use [lazytest.describe :only (describe it testing)]
+ [lazytest.expect :only (expect)]))
+
+(describe when-focused-select-all
+ (it "causes all text in a text field to be selected when it gains focus"
+ (let [t (text "Hi there")
+ remove-fn (when-focused-select-all t)]
+ ; Simulate focus gained :(
+ (doseq [l (.getFocusListeners t)]
+ (.focusGained l (java.awt.event.FocusEvent. t java.awt.event.FocusEvent/FOCUS_GAINED)))
+ (expect (= "Hi there" (.getSelectedText t))))))
+

0 comments on commit 164073c

Please sign in to comment.