Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions src/seesaw/chooser.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

; 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
; 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.
Expand All @@ -12,17 +12,17 @@
:author "Dave Ray"}
seesaw.chooser
(:use [seesaw.color :only [to-color]]
[seesaw.options :only [default-option bean-option apply-options
[seesaw.options :only [default-option bean-option apply-options
option-map option-provider]]
[seesaw.util :only [illegal-argument]])
(:import (javax.swing.filechooser FileFilter FileNameExtensionFilter)
[javax.swing JFileChooser]))

(defn file-filter
"Create a FileFilter.

Arguments:

description - description of this filter, will show up in the
filter-selection box when opening a file choosing dialog.

Expand Down Expand Up @@ -55,7 +55,7 @@
(doseq [f filters]
(.addChoosableFileFilter chooser
(cond
(instance? FileFilter f)
(instance? FileFilter f)
f

(and (sequential? f) (sequential? (second f)))
Expand All @@ -67,31 +67,35 @@
:else
(illegal-argument "not a valid filter: %s" f)))))

(def ^{:private true} file-chooser-options
(def ^{:private true} file-chooser-options
(option-map
(default-option :dir
(fn [^JFileChooser chooser dir]
(.setCurrentDirectory chooser (if (instance? java.io.File dir) dir
(fn [^JFileChooser chooser dir]
(.setCurrentDirectory chooser (if (instance? java.io.File dir) dir
(java.io.File. (str dir))))))
(bean-option [:multi? :multi-selection-enabled] JFileChooser boolean)
(bean-option [:selection-mode :file-selection-mode] JFileChooser file-selection-modes)
(default-option :filters set-file-filters)
(bean-option [:all-files? :accept-all-file-filter-used] JFileChooser boolean)))
(bean-option [:all-files? :accept-all-file-filter-used] JFileChooser boolean)
(default-option :selected-file
(fn [^JFileChooser chooser selected-file]
(.setSelectedFile chooser (if (instance? java.io.File selected-file) selected-file
(java.io.File. (str selected-file))))))))

(option-provider JFileChooser file-chooser-options)

(def ^{:private true} last-dir (atom nil))

(defn- show-file-chooser [^JFileChooser chooser parent type]
(case type
:open (.showOpenDialog chooser parent)
:open (.showOpenDialog chooser parent)
:save (.showSaveDialog chooser parent)
(.showDialog chooser parent (str type))))

(defn- configure-file-chooser [^JFileChooser chooser opts]
(apply-options chooser opts)
(when (and @last-dir (not (:dir opts)))
(.setCurrentDirectory chooser @last-dir))
(when (and @last-dir (not (or (:dir opts) (:selected-file opts)))
(.setCurrentDirectory chooser @last-dir)))
chooser)

(defn- remember-chooser-dir [^JFileChooser chooser]
Expand Down Expand Up @@ -147,6 +151,8 @@
:cancel-fn Function which will be called with the JFileChooser on user abort of the dialog.
Its result will be returned. Default: returns nil.

:selected-file The default name of file or java.io.File

Examples:

; ask & return single file
Expand All @@ -164,11 +170,11 @@

Returns result of SUCCESS-FN (default: either java.io.File or seq of java.io.File iff multi? set to true)
in case of the user selecting a file, or result of CANCEL-FN otherwise.

See http://download.oracle.com/javase/6/docs/api/javax/swing/JFileChooser.html
"
[& args]
(let [[parent & {:keys [type remember-directory? success-fn cancel-fn]
(let [[parent & {:keys [type remember-directory? success-fn cancel-fn selected-file]
:or {type :open
remember-directory? true
success-fn (fn [fc files] files)
Expand Down Expand Up @@ -201,14 +207,14 @@

(defn choose-color
"Choose a color with a color chooser dialog. The optional first argument is the
parent component for the dialog. The rest of the args is a list of key/value
parent component for the dialog. The rest of the args is a list of key/value
pairs:

:color The initial selected color (see seesaw.color/to-color)
:title The dialog's title

Returns the selected color or nil if canceled.

See:
http://download.oracle.com/javase/6/docs/api/javax/swing/JColorChooser.html
"
Expand Down
40 changes: 40 additions & 0 deletions test/seesaw/test/examples/chooser.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
; 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.examples.chooser
(:use [seesaw core chooser]
seesaw.test.examples.example)
(:require [seesaw.dev :as dev]))

(defn make-frame [content]
(frame :title "Seesaw File Chooser Example"
:size [300 :by 100]
:content content))

(defn content []
(border-panel
:hgap 5 :vgap 5 :border 5
:north (label :id :selected-file :text "File wasn't selected yet")
:center (button :id :choose :text "Choose file")))

(defn update-label [label filename]
(config! label :text (str "Selected file: " filename)))

(defn add-behavior [f]
(let [{:keys [selected-file choose]} (group-by-id f)]
(listen choose :action
(fn [_] (choose-file
:dir "./"
:selected-file "project.clj"
:success-fn (fn [e f] (update-label selected-file (.getAbsolutePath f)))))))
f)

(defexample []
(dev/debug!)
(-> (make-frame (content)) add-behavior))
1 change: 1 addition & 0 deletions test/seesaw/test/examples/launcher.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'button-group
'canvas
'cell-renderers
'chooser
'clock
'custom-border
'custom-dialog
Expand Down