changeset 19:5fa5d15b4a7b

Attempt to make it a std. app. Builds, sorta runs, needs more work.
author David Barts <n5jrn@me.com>
date Fri, 20 Nov 2020 21:38:06 -0800
parents 43884786343e
children 71029c9bf7cd
files src/name/blackcap/imageprep/Main.kt src/name/blackcap/imageprep/MainFrame.kt src/name/blackcap/imageprep/Menus.kt src/name/blackcap/imageprep/Osdep.kt.mac.osdep src/name/blackcap/imageprep/RotateDialog.kt src/name/blackcap/imageprep/Settings.kt
diffstat 6 files changed, 32 insertions(+), 85 deletions(-) [+]
line wrap: on
line diff
--- a/src/name/blackcap/imageprep/Main.kt	Fri Nov 20 21:37:37 2020 -0800
+++ b/src/name/blackcap/imageprep/Main.kt	Fri Nov 20 21:38:06 2020 -0800
@@ -22,10 +22,12 @@
     /* global UI objects */
     var mainFrame: MainFrame by setOnce()
     var helpDialog: HelpDialog by setOnce()
+    var settingsDialog: SettingsDialog by setOnce()
 
     fun initialize() {
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
         mainFrame = MainFrame() /* must be created first */
+        settingsDialog = SettingsDialog()
         mainFrame.jMenuBar = MyMenuBar()
         setMacMenus() /* always safe to call; no-op if not a Mac */
         helpDialog = HelpDialog()
@@ -40,61 +42,8 @@
         System.setProperty("apple.laf.useScreenMenuBar", "true")
     }
 
-    /* parse command line */
-    if (Settings.outputToInputDir)
-        Settings.outputTo = System.getProperty("user.dir")
-    val options = Options().apply {
-        addOption(Option<Unit>("?", "help", false, "Print this help message."))
-        addOption(Option<Int>("m", "maxdim", true, "Maximum dimension (default: ${Settings.maxDimension})").apply {
-            default = Settings.maxDimension
-            interpolater = {
-                val ret = try { it.toInt() } catch (e: NumberFormatException) { -1 }
-                if (ret < 1)
-                    throw InvalidArgumentException("Dimension must be a positive integer.")
-                Settings.maxDimension = ret
-                ret
-            }
-        })
-        addOption(Option<File>("o", "output", true, "Output directory (default: ${Settings.outputTo})").apply {
-            default = File(Settings.outputTo)
-            interpolater = {
-                val ret = File(tilde(it))
-                if (!ret.exists())
-                    throw InvalidArgumentException("'${it}' does not exist")
-                if (!ret.isDirectory())
-                    throw InvalidArgumentException("'${it}' is not a directory")
-                Settings.outputTo = ret.canonicalPath
-                ret
-            }
-        })
-        addOption(Option<Int>("q", "quality", true, "JPEG quality for output (0-100, default ${Settings.outputQuality})").apply {
-            default = Settings.outputQuality
-            interpolater = {
-                val ret = try { it.toInt() } catch (e: NumberFormatException) { -1 }
-                if (ret < 0 || ret > 100)
-                    throw InvalidArgumentException("Quality must be an integer from 0 to 100, inclusive.")
-                Settings.outputQuality = ret
-                ret
-            }
-        })
-    }
-    val cmdLine: CommandLine? = try {
-        PromptingParser().parse(options, args)
-    } catch (e: ParseException) {
-        System.err.println("${Application.MYNAME}: Syntax error - ${e.message}")
-        System.exit(2)
-        null
-    }
-    if (cmdLine!!.hasOption("help")) {
-        val usage = Application.MYNAME + " [--maxdim=integer] [--output=directory] [--quality=integer] file [...]"
-        HelpFormatter().printHelp(usage, options, false);
-        System.exit(0);
-    }
-
     /* launch GUI */
     inSwingThread {
         Application.initialize()
-        for (fileName in cmdLine!!.args)
-            RotateDialog.makeDialog(File(fileName))
     }
 }
--- a/src/name/blackcap/imageprep/MainFrame.kt	Fri Nov 20 21:37:37 2020 -0800
+++ b/src/name/blackcap/imageprep/MainFrame.kt	Fri Nov 20 21:38:06 2020 -0800
@@ -57,7 +57,7 @@
                 return false
             }
             for (file in files) {
-                RotateDialog.makeDialog(file)
+                RotateDialog.makeDialog(file, Application.settingsDialog.maxDimension)
             }
             return true
         }
--- a/src/name/blackcap/imageprep/Menus.kt	Fri Nov 20 21:37:37 2020 -0800
+++ b/src/name/blackcap/imageprep/Menus.kt	Fri Nov 20 21:38:06 2020 -0800
@@ -29,7 +29,7 @@
  */
 class MyMenuBar: JMenuBar() {
     private var currentInputDirectory = File(System.getProperty("user.dir"))
-    private var currentOutputDirectory = File(Settings.outputTo)
+    private var currentOutputDirectory = File(Application.settingsDialog.outputTo)
 
     init {
         add(JMenu("File").apply {
@@ -45,6 +45,12 @@
                 makeShortcut(KeyEvent.VK_S)
             })
             if (OS.type != OS.MAC) {
+                add(JMenuItem("Preferences…").apply {
+                    addActionListener(ActionListener {
+                        Application.settingsDialog.setVisible(true)
+                    })
+                    makeShortcut(KeyEvent.VK_COMMA)
+                })
                 add(JMenuItem("Quit").apply {
                     addActionListener(ActionListener {
                         LOGGER.log(Level.INFO, "execution complete")
@@ -77,13 +83,20 @@
     }
 
     private fun doOpen() {
+        val maxDim = MaxDimSpinner(Application.settingsDialog.maxDimension)
+        val acc = JPanel().apply {
+            layout = BoxLayout(this, BoxLayout.X_AXIS)
+            add(JLabel("Max. dimension:"))
+            add(maxDim)
+        }
         val chooser = JFileChooser().apply {
+            accessory = acc
             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)
+            RotateDialog.makeDialog(chooser.selectedFile, maxDim.value as Int)
         }
     }
 
@@ -108,8 +121,15 @@
             Toolkit.getDefaultToolkit().beep()
             return
         }
-        val outName = splitext(w.file.name).first + Settings.outputSuffix + ".jpg"
+        val outQual = OutQualSpinner(Application.settingsDialog.outputQuality)
+        val acc = JPanel().apply {
+            layout = BoxLayout(this, BoxLayout.X_AXIS)
+            add(JLabel("Quality:"))
+            add(outQual)
+        }
+        val outName = splitext(w.file.name).first + Application.settingsDialog.outputSuffix + ".jpg"
         val chooser = JFileChooser().apply {
+            accessory = acc
             currentDirectory = currentOutputDirectory
             selectedFile = File(currentOutputDirectory, outName)
         }
@@ -150,7 +170,7 @@
                         try {
                             val iwp = writer.getDefaultWriteParam().apply {
                                 setCompressionMode(ImageWriteParam.MODE_EXPLICIT)
-                                setCompressionQuality(Settings.outputQuality.toFloat() / 100.0f)
+                                setCompressionQuality((outQual.value as Int).toFloat() / 100.0f)
                             }
                             writer.setOutput(it)
                             writer.write(null, IIOImage(w.image, null, null), iwp)
--- a/src/name/blackcap/imageprep/Osdep.kt.mac.osdep	Fri Nov 20 21:37:37 2020 -0800
+++ b/src/name/blackcap/imageprep/Osdep.kt.mac.osdep	Fri Nov 20 21:38:06 2020 -0800
@@ -10,6 +10,8 @@
 fun setMacMenus() {
     com.apple.eawt.Application.getApplication().run {
         setAboutHandler(AboutHandler { showAboutDialog() })
+        setPreferencesHandler(
+            PreferencesHandler { Application.settingsDialog.setVisible(true) })
         setQuitStrategy(QuitStrategy.CLOSE_ALL_WINDOWS)
     }
 }
--- a/src/name/blackcap/imageprep/RotateDialog.kt	Fri Nov 20 21:37:37 2020 -0800
+++ b/src/name/blackcap/imageprep/RotateDialog.kt	Fri Nov 20 21:38:06 2020 -0800
@@ -140,7 +140,7 @@
          *
          * @param input java.io.File to read for image.
          */
-        fun makeDialog(input: File): Unit {
+        fun makeDialog(input: File, maxDim: Int): Unit {
             Application.mainFrame.useWaitCursor()
             swingWorker<Pair<BufferedImage?, IOException?>> {
                 inBackground {
@@ -148,7 +148,7 @@
                         val imageIn = ImageIO.read(input) /* IOException */
                         if (imageIn == null)
                             throw IOException("Unsupported file type.")
-                        val ratio = Settings.maxDimension.toDouble() / max(imageIn.width, imageIn.height).toDouble()
+                        val ratio = maxDim.toDouble() / max(imageIn.width, imageIn.height).toDouble()
                         if (ratio >= 1.0) {
                             Pair(null, null)
                         } else {
@@ -180,7 +180,7 @@
                     else
                         JOptionPane.showMessageDialog(Application.mainFrame,
                             "Image is too small to be scaled.",
-                            "Warning", JOptionPane.WARNING_MESSAGE)
+                            "Error", JOptionPane.ERROR_MESSAGE)
                 }
             }
         }
--- a/src/name/blackcap/imageprep/Settings.kt	Fri Nov 20 21:37:37 2020 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-/*
- * Our settings. Basically just some parsed properties.
- */
-package name.blackcap.imageprep
-
-import java.io.File
-
-/* work around name shadowing */
-private val _PROPS = PROPERTIES
-
-object Settings {
-    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 strToBool(s: String): Boolean {
-        if (s.isNullOrEmpty())
-            return false
-        val c1 = s[0].toLowerCase()
-        return c1 == 't' || c1 == 'y'
-    }
-}