Permalink
Browse files

changed to add file extension to template file name automatically (li…

…ke "hoge.clj") if not specified.
  • Loading branch information...
1 parent 6282b49 commit f68b67c001a9ef99dd33c25c6610c175a9071419 @fukamachi committed Jan 7, 2011
Showing with 14 additions and 6 deletions.
  1. +14 −6 src/L5/file.clj
View
@@ -6,14 +6,17 @@
[java.awt GridLayout]
[java.awt.event ActionListener]))
+(def presen-file-extensions ["clj" "lgo"])
+(def presen-filter (FileNameExtensionFilter.
+ (apply format "Presentation (.%s .%s)" presen-file-extensions)
+ (into-array presen-file-extensions)))
+(def pdf-filter (FileNameExtensionFilter. "PDF (.pdf)" (into-array ["pdf"])))
+
(defn- load-resource [name]
(let [thr (Thread/currentThread)
ldr (.getContextClassLoader thr)]
(.getResourceAsStream ldr name)))
-(def presen-filter (FileNameExtensionFilter. "Presentation (.clj .lgo)" (into-array ["clj" "lgo"])))
-(def pdf-filter (FileNameExtensionFilter. "PDF (.pdf)" (into-array ["pdf"])))
-
(defn- file-dialog [action parent callback filter]
(let [chooser (JFileChooser.)]
(.setCurrentDirectory chooser (.. (File. ".") getAbsoluteFile getParentFile))
@@ -25,8 +28,7 @@
(callback (.. chooser getSelectedFile getPath)))))
(defn choose-dialog [parent callback]
- (file-dialog :open parent callback
- (FileNameExtensionFilter. "Presentation (.clj .lgo)" (into-array ["clj" "lgo"]))))
+ (file-dialog :open parent callback presen-filter))
(defn save-dialog [parent callback filter]
(file-dialog :save parent callback filter))
@@ -42,12 +44,18 @@
(.exec (Runtime/getRuntime)
(format "%s %s" editor (.getAbsolutePath file))))))))
+(defn- ensure-file-ext [s ext]
+ (let [re (re-pattern (apply format "\\.(%s|%s)$" ext))]
+ (if (re-find re s)
+ s
+ (str s "." (first ext)))))
+
(defn- get-create-button [frame]
(let [button (JButton. "Create")
action (proxy [ActionListener] []
(actionPerformed [e]
(save-dialog frame
- #(let [file (File. %)]
+ #(let [file (File. (ensure-file-ext % presen-file-extensions))]
(io/copy (load-resource "skelton.clj") file)
(open-file file))
presen-filter)))]

0 comments on commit f68b67c

Please sign in to comment.