From 50f718bd9f16a262c19bf7f4054881ce84abc19b Mon Sep 17 00:00:00 2001 From: Jan Ferko Date: Tue, 2 Dec 2014 00:25:20 +0100 Subject: [PATCH 1/3] #156 Add suggest-name option to file chooser --- src/seesaw/chooser.clj | 85 +++++++++++++++++++++++++++++------------- 1 file changed, 59 insertions(+), 26 deletions(-) diff --git a/src/seesaw/chooser.clj b/src/seesaw/chooser.clj index 8baa2d72..192c009f 100644 --- a/src/seesaw/chooser.clj +++ b/src/seesaw/chooser.clj @@ -72,11 +72,15 @@ (default-option :dir (fn [^JFileChooser chooser dir] (.setCurrentDirectory chooser (if (instance? java.io.File dir) dir - (java.io.File. (str 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 :suggest-name + (fn [^JFileChooser chooser suggest-name] + (.setSelectedFile chooser (java.io.File. (str suggest-name))))))) + (option-provider JFileChooser file-chooser-options) @@ -118,14 +122,25 @@ :selection-mode The file selection mode: :files-only, :dirs-only and :files-and-dirs. Defaults to :files-only :filters A seq of either: - - A seq that contains a filter name and a seq of - extensions as strings for that filter. - - A seq that contains a filter name and a function - to be used as accept function. (see file-filter) - A FileFilter. (see file-filter) + a seq that contains a filter name and a seq of + extensions as strings for that filter; + + a seq that contains a filter name and a function + to be used as accept function (see file-filter); + + a FileFilter (see file-filter). + + The filters appear in the dialog's filter selection in the same + order as in the seq. + :all-files? If true, a filter matching all file extensions and files + without an extension will appear in the filter selection + of the dialog additionally to the filters specified + through :filters. The filter usually appears last in the + selection. If this is not desired set this option to + false and include an equivalent filter manually at the + desired position as shown in the examples below. Defaults + to true. :remember-directory? Flag specifying whether to remember the directory for future file-input invocations in case of successful exit. Default: true. @@ -136,15 +151,19 @@ :cancel-fn Function which will be called with the JFileChooser on user abort of the dialog. Its result will be returned. Default: returns nil. + :suggest-name The default name of file. + Examples: ; ask & return single file (choose-file) - ; ask & return including a filter for image files - (choose-file :filters [[\"Images\" [\"png\" \"jpeg\"] - [\"Folders\" #(.isDirectory %)] - (file-filter \"All files\" (constantly true))]]) + ; ask & return including a filter for image files and an \"all files\" + ; filter appearing at the beginning + (choose-file :all-files? false + :filters [(file-filter \"All files\" (constantly true)) + [\"Images\" [\"png\" \"jpeg\"]] + [\"Folders\" #(.isDirectory %)]]) ; ask & return absolute file path as string (choose-file :success-fn (fn [fc file] (.getAbsolutePath file))) @@ -155,23 +174,37 @@ See http://download.oracle.com/javase/6/docs/api/javax/swing/JFileChooser.html " [& args] - (let [[parent & {:keys [type remember-directory? success-fn cancel-fn] - :or {type :open + (let [[parent & {:keys [type remember-directory? success-fn cancel-fn suggest-name] + :or {type :open remember-directory? true success-fn (fn [fc files] files) - cancel-fn (fn [fc])} + cancel-fn (fn [fc]) + suggest-name ""} :as opts}] (if (keyword? (first args)) (cons nil args) args) parent (if (keyword? parent) nil parent) - ^JFileChooser chooser (configure-file-chooser (JFileChooser.) (dissoc opts :type :remember-directory? :success-fn :cancel-fn)) - multi? (.isMultiSelectionEnabled chooser) - result (show-file-chooser chooser parent type)] - (cond - (= result JFileChooser/APPROVE_OPTION) - (do - (when remember-directory? - (remember-chooser-dir chooser)) - (success-fn chooser (if multi? (.getSelectedFiles chooser) (.getSelectedFile chooser)))) - :else (cancel-fn chooser)))) + ^JFileChooser chooser (configure-file-chooser + (JFileChooser.) + (dissoc + opts + :type + :remember-directory? + :success-fn + :cancel-fn))] + (when-let [[filter _] (seq (.getChoosableFileFilters chooser))] + (.setFileFilter chooser filter)) + (let [result (show-file-chooser chooser parent type) + multi? (.isMultiSelectionEnabled chooser)] + (cond + (= result JFileChooser/APPROVE_OPTION) + (do + (when remember-directory? + (remember-chooser-dir chooser)) + (success-fn + chooser + (if multi? + (.getSelectedFiles chooser) + (.getSelectedFile chooser)))) + :else (cancel-fn chooser))))) (defn choose-color "Choose a color with a color chooser dialog. The optional first argument is the From 80f6a1608e94c3e95f1fec460c31bab3885b3b83 Mon Sep 17 00:00:00 2001 From: Jan Ferko Date: Wed, 3 Dec 2014 14:50:42 +0100 Subject: [PATCH 2/3] Change option suggest-name name to selected-file and allow file as selected-file --- src/seesaw/chooser.clj | 51 +++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/src/seesaw/chooser.clj b/src/seesaw/chooser.clj index 192c009f..00aa905f 100644 --- a/src/seesaw/chooser.clj +++ b/src/seesaw/chooser.clj @@ -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. @@ -12,7 +12,7 @@ :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) @@ -20,9 +20,9 @@ (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. @@ -55,7 +55,7 @@ (doseq [f filters] (.addChoosableFileFilter chooser (cond - (instance? FileFilter f) + (instance? FileFilter f) f (and (sequential? f) (sequential? (second f))) @@ -67,20 +67,20 @@ :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 - (java.io.File. (str 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) - (default-option :suggest-name - (fn [^JFileChooser chooser suggest-name] - (.setSelectedFile chooser (java.io.File. (str suggest-name))))))) - + (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) @@ -88,14 +88,14 @@ (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] @@ -151,7 +151,7 @@ :cancel-fn Function which will be called with the JFileChooser on user abort of the dialog. Its result will be returned. Default: returns nil. - :suggest-name The default name of file. + :selected-file The default name of file or java.io.File Examples: @@ -170,16 +170,15 @@ 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 suggest-name] - :or {type :open + (let [[parent & {:keys [type remember-directory? success-fn cancel-fn selected-file] + :or {type :open remember-directory? true success-fn (fn [fc files] files) - cancel-fn (fn [fc]) - suggest-name ""} + cancel-fn (fn [fc])} :as opts}] (if (keyword? (first args)) (cons nil args) args) parent (if (keyword? parent) nil parent) ^JFileChooser chooser (configure-file-chooser @@ -208,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 " From 15dec6a0ef3f48cd8407734c150ed184b2bef674 Mon Sep 17 00:00:00 2001 From: Jan Ferko Date: Wed, 3 Dec 2014 14:51:23 +0100 Subject: [PATCH 3/3] Add file chooser examples --- test/seesaw/test/examples/chooser.clj | 40 ++++++++++++++++++++++++++ test/seesaw/test/examples/launcher.clj | 1 + 2 files changed, 41 insertions(+) create mode 100644 test/seesaw/test/examples/chooser.clj diff --git a/test/seesaw/test/examples/chooser.clj b/test/seesaw/test/examples/chooser.clj new file mode 100644 index 00000000..3bb900bd --- /dev/null +++ b/test/seesaw/test/examples/chooser.clj @@ -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)) diff --git a/test/seesaw/test/examples/launcher.clj b/test/seesaw/test/examples/launcher.clj index 595a4e62..5ec34347 100644 --- a/test/seesaw/test/examples/launcher.clj +++ b/test/seesaw/test/examples/launcher.clj @@ -22,6 +22,7 @@ 'button-group 'canvas 'cell-renderers + 'chooser 'clock 'custom-border 'custom-dialog