diff src/name/blackcap/exifwasher/SettingsDialog.kt @ 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 40911898ed23
line wrap: on
line diff
--- 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)
+}