changeset 5:884f1415a330

Rationalized directory management.
author David Barts <n5jrn@me.com>
date Fri, 17 Jul 2020 17:11:43 -0700
parents 5234e4500d45
children 9129ae110146
files src/name/blackcap/imageprep/Main.kt src/name/blackcap/imageprep/Menus.kt src/name/blackcap/imageprep/Misc.kt src/name/blackcap/imageprep/Settings.kt
diffstat 4 files changed, 24 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/name/blackcap/imageprep/Main.kt	Fri Jul 17 14:51:41 2020 -0700
+++ b/src/name/blackcap/imageprep/Main.kt	Fri Jul 17 17:11:43 2020 -0700
@@ -34,11 +34,13 @@
 }
 
 fun main(args: Array<String>) {
+    /* start up */
     LOGGER.log(Level.INFO, "beginning execution")
     if (OS.type == OS.MAC) {
         System.setProperty("apple.laf.useScreenMenuBar", "true")
     }
 
+    /* parse command line */
     if (Settings.outputToInputDir)
         Settings.outputTo = System.getProperty("user.dir")
     val options = Options().apply {
@@ -56,7 +58,7 @@
         addOption(Option<File>("o", "output", true, "Output directory (default: ${Settings.outputTo})").apply {
             default = File(Settings.outputTo)
             interpolater = {
-                val ret = File(it)
+                val ret = File(tilde(it))
                 if (!ret.exists())
                     throw InvalidArgumentException("'${it}' does not exist")
                 if (!ret.isDirectory())
@@ -79,7 +81,7 @@
     val cmdLine: CommandLine? = try {
         PromptingParser().parse(options, args)
     } catch (e: ParseException) {
-        System.err.println("${Application.MYNAME}: syntax error - ${e.message}")
+        System.err.println("${Application.MYNAME}: Syntax error - ${e.message}")
         System.exit(2)
         null
     }
@@ -89,6 +91,7 @@
         System.exit(0);
     }
 
+    /* launch GUI */
     inSwingThread {
         Application.initialize()
         for (fileName in cmdLine!!.args)
--- a/src/name/blackcap/imageprep/Menus.kt	Fri Jul 17 14:51:41 2020 -0700
+++ b/src/name/blackcap/imageprep/Menus.kt	Fri Jul 17 17:11:43 2020 -0700
@@ -28,6 +28,9 @@
  * the Mac gives us a gratuitous menu bar entry for handling some stuff.
  */
 class MyMenuBar: JMenuBar() {
+    var currentInputDirectory = File(System.getProperty("user.dir"))
+    var currentOutputDirectory = File(Settings.outputTo)
+
     init {
         add(JMenu("File").apply {
             add(JMenuItem("Open & Scaleā€¦").apply {
@@ -75,9 +78,11 @@
 
     private fun doOpen() {
         val chooser = JFileChooser().apply {
+            currentDirectory = currentInputDirectory
             fileFilter = FileNameExtensionFilter("Image Files", *ImageIO.getReaderFileSuffixes())
         }
         if (chooser.showOpenDialog(Application.mainFrame) == JFileChooser.APPROVE_OPTION) {
+            currentInputDirectory = chooser.selectedFile.canonicalFile.parentFile
             RotateDialog.makeDialog(chooser.selectedFile)
         }
     }
@@ -100,18 +105,18 @@
         }
         val outName = splitext(w.file.name).first + Settings.outputSuffix + ".jpg"
         val chooser = JFileChooser().apply {
-            selectedFile = File(
-                if (Settings.outputToInputDir) w.file.parent else Settings.outputTo,
-                outName)
+            currentDirectory = currentOutputDirectory
+            selectedFile = File(currentOutputDirectory, outName)
         }
         if (chooser.showSaveDialog(Application.mainFrame) != JFileChooser.APPROVE_OPTION) {
             return
         }
+        currentOutputDirectory = chooser.selectedFile.canonicalFile.parentFile
         val (name, ext) = splitext(chooser.selectedFile.name)
         val file = if (ext.toLowerCase() in setOf(".jpg", ".jpeg")) {
             chooser.selectedFile
         } else {
-            File(chooser.selectedFile.parent, name + ".jpg")
+            File(currentOutputDirectory, name + ".jpg")
         }
         if (file.exists() && JOptionPane.showConfirmDialog(w, "File ${file.name} already exists. Overwrite?", "Confirm Overwrite", JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
             return
--- a/src/name/blackcap/imageprep/Misc.kt	Fri Jul 17 14:51:41 2020 -0700
+++ b/src/name/blackcap/imageprep/Misc.kt	Fri Jul 17 17:11:43 2020 -0700
@@ -246,3 +246,13 @@
     JOptionPane.showMessageDialog(parent,
         dmsg, "Error", JOptionPane.ERROR_MESSAGE)
 }
+
+private val homeDir = System.getProperty("user.home")
+fun tilde(s: String?): String {
+    if (s.isNullOrEmpty())
+        return homeDir
+    if (s.startsWith("~/") || s.startsWith("~\\"))
+        return File(homeDir, s.substring(2).trimStart(s[1])).toString()
+    else
+        return s
+}
--- a/src/name/blackcap/imageprep/Settings.kt	Fri Jul 17 14:51:41 2020 -0700
+++ b/src/name/blackcap/imageprep/Settings.kt	Fri Jul 17 17:11:43 2020 -0700
@@ -9,22 +9,12 @@
 private val _PROPS = PROPERTIES
 
 object Settings {
-    private val homeDir = System.getProperty("user.home")
     var maxDimension = _PROPS.getProperty("maxDimension").toInt()
     var outputQuality = _PROPS.getProperty("outputQuality").toInt()
     var outputSuffix = _PROPS.getProperty("outputSuffix")
     var outputTo = tilde(_PROPS.getProperty("outputTo"))
     var outputToInputDir = strToBool(_PROPS.getProperty("outputToInputDir"))
 
-    private fun tilde(s: String?): String {
-        if (s.isNullOrEmpty())
-            return homeDir
-        if (s.startsWith("~/") || s.startsWith("~\\"))
-            return File(homeDir, s.substring(2).trimStart(s[1])).toString()
-        else
-            return s
-    }
-
     private fun strToBool(s: String): Boolean {
         if (s.isNullOrEmpty())
             return false