changeset 6:aafc9c127c7b

Fix many bugs; get settings (apparently) working.
author David Barts <n5jrn@me.com>
date Thu, 09 Apr 2020 22:29:48 -0700
parents dc1f4359659d
children 65d14d44bc3f
files src/name/blackcap/exifwasher/Main.kt src/name/blackcap/exifwasher/MainFrame.kt src/name/blackcap/exifwasher/Menus.kt src/name/blackcap/exifwasher/Osdep.kt.mac.osdep src/name/blackcap/exifwasher/SettingsDialog.kt src/name/blackcap/exifwasher/Whitelist.kt
diffstat 6 files changed, 63 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/src/name/blackcap/exifwasher/Main.kt	Thu Apr 09 18:20:34 2020 -0700
+++ b/src/name/blackcap/exifwasher/Main.kt	Thu Apr 09 22:29:48 2020 -0700
@@ -18,6 +18,8 @@
     fun initialize() {
         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
         mainFrame = MainFrame() /* must be created first */
+        mainFrame.jMenuBar = MyMenuBar()
+        setMacMenus() /* always safe to call; no-op if not a Mac */
         settingsDialog = SettingsDialog()
         mainFrame.setVisible(true)
     }
--- a/src/name/blackcap/exifwasher/MainFrame.kt	Thu Apr 09 18:20:34 2020 -0700
+++ b/src/name/blackcap/exifwasher/MainFrame.kt	Thu Apr 09 22:29:48 2020 -0700
@@ -64,7 +64,7 @@
 
     init {
         contentPane.add(
-            JLabel("Drag image files into this window or choose File… Open from menu.").apply {
+            JLabel("Drag image files into this window or choose File… Wash from menu.").apply {
                 horizontalAlignment = JLabel.CENTER
                 verticalAlignment = JLabel.CENTER
             })
--- a/src/name/blackcap/exifwasher/Menus.kt	Thu Apr 09 18:20:34 2020 -0700
+++ b/src/name/blackcap/exifwasher/Menus.kt	Thu Apr 09 22:29:48 2020 -0700
@@ -69,8 +69,8 @@
  */
 fun showAboutDialog() {
     JOptionPane.showMessageDialog(Application.mainFrame,
-        "ExifWasher—Privacy for your photos.\n"
-        + "© MMXX, David W. Barts",
+        "\nExifWasher—Privacy for your photos.\n"
+        + "\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0© MMXX, David W. Barts\n",
         "About ExifWasher",
         JOptionPane.PLAIN_MESSAGE)
 }
--- a/src/name/blackcap/exifwasher/Osdep.kt.mac.osdep	Thu Apr 09 18:20:34 2020 -0700
+++ b/src/name/blackcap/exifwasher/Osdep.kt.mac.osdep	Thu Apr 09 22:29:48 2020 -0700
@@ -4,11 +4,10 @@
 package name.blackcap.exifwasher
 
 import com.apple.eawt.AboutHandler
-import com.apple.eawt.Application
 import com.apple.eawt.PreferencesHandler
 
 fun setMacMenus() {
-    Application.getApplication().run {
+    com.apple.eawt.Application.getApplication().run {
         setAboutHandler(AboutHandler { showAboutDialog() })
         setPreferencesHandler(
             PreferencesHandler { Application.settingsDialog.setVisible(true) })
--- a/src/name/blackcap/exifwasher/SettingsDialog.kt	Thu Apr 09 18:20:34 2020 -0700
+++ b/src/name/blackcap/exifwasher/SettingsDialog.kt	Thu Apr 09 22:29:48 2020 -0700
@@ -3,6 +3,7 @@
  */
 package name.blackcap.exifwasher
 
+import java.awt.Dimension
 import java.awt.Toolkit
 import java.awt.event.ActionEvent
 import java.awt.event.ActionListener
@@ -30,8 +31,9 @@
     protected val BW2 = BW * 2
 
     /* where to send output, if not outputToInputDir */
-    protected var _outputTo = _PROPS.getProperty("outputTo")
-    protected var _dOutputTo = DPROPERTIES.getProperty("outputTo")
+    private var homeDir = System.getProperty("user.home")
+    protected var _outputTo = _PROPS.getNotEmpty("outputTo", homeDir)
+    protected var _dOutputTo = DPROPERTIES.getNotEmpty("outputTo", homeDir)
     val outputTo: String
     get() = _outputTo
 
@@ -51,12 +53,14 @@
     protected val _dWhitelist = Whitelist.parse(DPROPERTIES.getProperty("whitelist", ""))
 
     /* radio buttons to choose output directory policy */
-    protected val outputToButton = JRadioButton("Output to:", !outputToInputDir).apply {
-        addActionListener(ActionListener { setOutputOpts(isSelected()) })
+    protected val outputToButton = JRadioButton("Output to:", !outputToInputDir).also {
+        it.addActionListener(ActionListener { _ -> setOutputOpts(it.isSelected()) })
+        it.noTaller()
     }
     protected val outputToInputButton = JRadioButton(
-      "Output to directory containing input file.", outputToInputDir).apply {
-        addActionListener(ActionListener { setOutputOpts(!isSelected()) })
+      "Output to directory containing input file.", outputToInputDir).also {
+        it.addActionListener(ActionListener { _ -> setOutputOpts(!it.isSelected()) })
+        it.noTaller()
     }
     protected val buttonGroup = ButtonGroup().apply {
         add(outputToButton)
@@ -68,8 +72,9 @@
     }
 
     /* displays the OutputTo directory */
-    protected val outputToText = JTextField(outputTo, 50).apply {
+    protected val outputToText = JTextField(outputTo, 40).apply {
         setEditable(false)
+        noTaller()
     }
 
     /* pops up to change the above directory */
@@ -90,25 +95,28 @@
     }
 
     /* bottom buttons to restore defaults, cancel, save */
-    protected val restoreButton = JButton("Restore All Defaults").apply {
-        addActionListener(ActionListener {
+    protected val restoreButton = JButton("Restore All Defaults").also {
+        it.addActionListener(ActionListener {
             restore(_dOutputToInputDir, _dOutputTo, _dWhitelist)
         })
     }
-    protected val cancelButton = JButton("Cancel").apply {
-        addActionListener(ActionListener {
+    protected val cancelButton = JButton("Cancel").also {
+        it.addActionListener(ActionListener {
             setVisible(false)
             restore(outputToInputDir, outputTo, whitelist)
         })
     }
-    protected val saveButton = JButton("Save").apply {
-        addActionListener(ActionListener {
+    protected val saveButton = JButton("Save").also {
+        it.addActionListener(ActionListener {
             setVisible(false)
             writeProperties()
         })
     }
 
     protected fun writeProperties() {
+        _whitelist = Whitelist().apply {
+            wlSelectorModel.toList().forEach { add(it) }
+        }
         _PROPS.run {
             setProperty("outputTo", outputTo)
             setProperty("outputToInputDir", outputToInputDir.toString())
@@ -133,6 +141,7 @@
         outputToText.text = output
         outputToInputButton.setSelected(outputToInput)
         wlSelectorModel.reset(wl.toList())
+        validate()
     }
 
     /* so we can present a list of strings that is always sorted */
@@ -212,15 +221,15 @@
             border = BorderFactory.createEmptyBorder(BW, BW, BW, BW)
         }
 
-        private val cancelButton = JButton("Cancel").apply {
-            addActionListener(ActionListener {
+        private val cancelButton = JButton("Cancel").also {
+            it.addActionListener(ActionListener {
                 toAdd.text = ""
                 setVisible(false)
             })
         }
 
-        private val addButton = JButton("Add").apply {
-            addActionListener(ActionListener {
+        private val addButton = JButton("Add").also {
+            it.addActionListener(ActionListener {
                 val newItem = toAdd.text?.trim()
                 if (newItem.isNullOrEmpty()) {
                     Toolkit.getDefaultToolkit().beep()
@@ -255,7 +264,7 @@
     /* the JList that holds our whitelist */
     protected val wlSelectorModel = WhiteListModel(whitelist.toList())
     protected val wlSelector: JList<String> = JList<String>().apply {
-        visibleRowCount = -1
+        visibleRowCount = 6
         model = wlSelectorModel
         clearSelection()
         addListSelectionListener(ListSelectionListener {
@@ -264,28 +273,21 @@
     }
 
     /* buttons for managing the whitelist */
-    protected val wlAddButton = JButton("Add").apply {
-        addActionListener(ActionListener { wlAddDialog.setVisible(true) })
+    protected val wlAddButton = JButton("Add").also {
+        it.addActionListener(ActionListener { wlAddDialog.setVisible(true) })
     }
-    protected val wlDeleteButton = JButton("Delete").apply {
-        addActionListener(ActionListener {
+    protected val wlDeleteButton = JButton("Delete").also {
+        it.addActionListener(ActionListener {
             wlSelector.selectedIndices.forEach { wlSelectorModel.removeAt(it) }
             setEnabled(false)
         })
-        setEnabled(false)
+        it.setEnabled(false)
     }
 
     /* the dialog that the Add button pops up */
     protected val wlAddDialog = WLAddDialog(this)
 
     init {
-        val home = System.getProperty("user.dir")
-        if (_outputTo.isNullOrEmpty()) {
-            _outputTo = home
-        }
-        if (_dOutputTo.isNullOrEmpty()) {
-            _dOutputTo = home
-        }
         title = "Settings"
         contentPane.apply {
             layout = BoxLayout(this, BoxLayout.Y_AXIS)
@@ -293,24 +295,35 @@
                 addTab("Folders", JPanel().apply {
                     border = BorderFactory.createEmptyBorder(BW, BW, BW, BW)
                     layout = BoxLayout(this, BoxLayout.Y_AXIS)
-                    add(outputToInputButton.apply {
+                    // yes, a one-item box. POS mis-aligns things if only
+                    // some stuff is boxed, border widths be damned. sigh.
+                    add(Box(BoxLayout.X_AXIS).apply {
                         alignmentX = LEFT_ALIGNMENT
-                        border = BorderFactory.createEmptyBorder(BW, BW, BW, BW)
+                        border = BorderFactory.createEmptyBorder(BW, BW, 0, BW)
+                        add(outputToInputButton)
+                        add(Box.createHorizontalGlue())
+                        pack()
+                        noTaller()
                     })
                     add(Box(BoxLayout.X_AXIS).apply {
                         alignmentX = LEFT_ALIGNMENT
-                        border = BorderFactory.createEmptyBorder(BW, BW, BW, BW)
+                        border = BorderFactory.createEmptyBorder(BW, BW, 0, BW)
                         add(outputToButton)
                         add(Box.createHorizontalStrut(BW2))
                         add(outputToText)
                         add(Box.createHorizontalGlue())
+                        pack()
+                        noTaller()
                     })
                     add(Box(BoxLayout.X_AXIS).apply {
                         alignmentX = LEFT_ALIGNMENT
                         border = BorderFactory.createEmptyBorder(0, BW, BW, BW)
                         add(Box.createHorizontalGlue())
                         add(changeOutputTo)
+                        pack()
+                        noTaller()
                     })
+                    add(Box.createVerticalGlue())
                 })
                 addTab("Whitelist", JPanel().apply {
                     border = BorderFactory.createEmptyBorder(BW, BW, BW, BW)
@@ -321,6 +334,7 @@
                         verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS
                         horizontalScrollBarPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
                     })
+                    add(Box.createVerticalGlue())
                     add(Box(BoxLayout.X_AXIS).apply {
                         alignmentX = CENTER_ALIGNMENT
                         border = BorderFactory.createEmptyBorder(BW, BW, BW, BW)
@@ -345,3 +359,12 @@
         minimumSize = size
     }
 }
+
+fun Properties.getNotEmpty(key: String, default: String): String {
+    val ret = getProperty(key)
+    return if (ret.isNullOrEmpty()) default else ret
+}
+
+fun JComponent.noTaller() {
+    maximumSize = Dimension(maximumSize.width, preferredSize.height)
+}
--- a/src/name/blackcap/exifwasher/Whitelist.kt	Thu Apr 09 18:20:34 2020 -0700
+++ b/src/name/blackcap/exifwasher/Whitelist.kt	Thu Apr 09 22:29:48 2020 -0700
@@ -29,7 +29,7 @@
 
     fun contains(s: String) = entire.contains(s) || prefixes.find { s.startsWith(it) } != null
 
-    fun toList(): List<String> = prefixes + entire
+    fun toList(): List<String> = prefixes.map { it + "*" } + entire
 
     override fun toString(): String = toList().joinToString(",")