changeset 22:d3979a2155a8

Fix out qual and max dim controls.
author David Barts <n5jrn@me.com>
date Mon, 23 Nov 2020 15:45:04 -0800
parents 0f3634a0816d
children 92afaa27f40a
files src/name/blackcap/imageprep/MaxDimSpinner.kt src/name/blackcap/imageprep/Menus.kt src/name/blackcap/imageprep/OutQualSpinner.kt src/name/blackcap/imageprep/SettingsDialog.kt
diffstat 4 files changed, 47 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/src/name/blackcap/imageprep/MaxDimSpinner.kt	Sat Nov 21 21:25:05 2020 -0800
+++ b/src/name/blackcap/imageprep/MaxDimSpinner.kt	Mon Nov 23 15:45:04 2020 -0800
@@ -1,36 +1,56 @@
 /*
- * A standard spinner for inputting the maximum dimension of an image.
+ * A standard combo box for inputting the maximum dimension of an image.
+ * Note that this IS NOT a spinner; this class is so named for historical
+ * reasons.
  */
 package name.blackcap.imageprep
 
-import java.awt.Dimension
 import java.awt.Toolkit
-import javax.swing.*
-import javax.swing.event.ChangeEvent
-import javax.swing.event.ChangeListener
+import java.awt.event.ActionEvent
+import java.awt.event.ActionListener
 import java.util.logging.Level
 import java.util.logging.Logger
+import javax.swing.*
 
 /* maximum allowable maximum dimension */
 private val MAXDIM = 16384
 
 /* preferred standard dimensions */
-private val STDDIMS = listOf<Int>(1600, 1280, 1024, 800, 640, 512, 400, 320)
+private val STDDIMS = arrayOf<Int>(1600, 1280, 1024, 800, 640, 512, 400, 320).apply {
+    sort()
+}
 
-class MaxDimSpinner(val default: Int): JSpinner(SpinnerListModel(STDDIMS))
+class MaxDimSpinner(val default: Int): JComboBox<Int>(STDDIMS)
 {
+    private var _value: Int = 0
+    var value: Int
+    get() = _value
+    set(v) {
+        if (outOfRange(v))
+            throw IllegalArgumentException("bad max dimension: $v")
+        _value = v
+        val pos = STDDIMS.binarySearch(v)
+        if (pos >= 0)
+            selectedIndex = pos
+        else
+            selectedItem = v
+    }
+
+    private fun outOfRange(v: Int): Boolean = v < 1 || v > MAXDIM
+
     init {
-        editor = JSpinner.ListEditor(this).apply {
-            textField.columns = STDDIMS.map { it.toString().length }.max() ?: 1
-        }
         value = default
         maximumSize = preferredSize
-        addChangeListener( ChangeListener {
-            val v = value as? Int ?: (value as? String)?.toIntOrNull()
-            if ( v == null || v < 1 || v > MAXDIM ) {
-                LOGGER.log(Level.INFO, "bad max dimension: $value")
+        setEditable(true)
+        prototypeDisplayValue = MAXDIM
+        addActionListener(ActionListener {
+            val v = selectedItem as? Int ?: (selectedItem as? String)?.toIntOrNull()
+            if (v == null || outOfRange(v)) {
+                LOGGER.log(Level.INFO, "bad max dimension: $selectedItem")
+                Toolkit.getDefaultToolkit().beep()
                 value = default
-                Toolkit.getDefaultToolkit().beep()
+            } else {
+                _value = v
             }
         })
     }
--- a/src/name/blackcap/imageprep/Menus.kt	Sat Nov 21 21:25:05 2020 -0800
+++ b/src/name/blackcap/imageprep/Menus.kt	Mon Nov 23 15:45:04 2020 -0800
@@ -83,10 +83,13 @@
     }
 
     private fun doOpen() {
-        val maxDim = MaxDimSpinner(Application.settingsDialog.maxDimension)
+        val maxDim = MaxDimSpinner(Application.settingsDialog.maxDimension).apply {
+            alignmentX = LEFT_ALIGNMENT
+        }
         val acc = JPanel().apply {
             layout = BoxLayout(this, BoxLayout.Y_AXIS)
-            add(JLabel("Max. dimension:"))
+            add(Box.createGlue())
+            add(JLabel("Max. dimension:").apply { alignmentX = LEFT_ALIGNMENT })
             add(maxDim)
         }
         val chooser = JFileChooser().apply {
@@ -121,10 +124,13 @@
             Toolkit.getDefaultToolkit().beep()
             return
         }
-        val outQual = OutQualSpinner(Application.settingsDialog.outputQuality)
+        val outQual = OutQualSpinner(Application.settingsDialog.outputQuality).apply {
+            alignmentX = LEFT_ALIGNMENT
+        }
         val acc = JPanel().apply {
             layout = BoxLayout(this, BoxLayout.Y_AXIS)
-            add(JLabel("Quality:"))
+            add(Box.createGlue())
+            add(JLabel("Quality:").apply { alignmentX = LEFT_ALIGNMENT })
             add(outQual)
         }
         val outName = splitext(w.file.name).first + Application.settingsDialog.outputSuffix + ".jpg"
--- a/src/name/blackcap/imageprep/OutQualSpinner.kt	Sat Nov 21 21:25:05 2020 -0800
+++ b/src/name/blackcap/imageprep/OutQualSpinner.kt	Mon Nov 23 15:45:04 2020 -0800
@@ -3,7 +3,6 @@
  */
 package name.blackcap.imageprep
 
-import java.awt.Dimension
 import java.awt.Toolkit
 import javax.swing.*
 import javax.swing.event.ChangeEvent
--- a/src/name/blackcap/imageprep/SettingsDialog.kt	Sat Nov 21 21:25:05 2020 -0800
+++ b/src/name/blackcap/imageprep/SettingsDialog.kt	Mon Nov 23 15:45:04 2020 -0800
@@ -11,16 +11,10 @@
 import java.io.FileOutputStream
 import java.io.IOException
 import java.io.OutputStreamWriter
-import java.util.Hashtable
 import java.util.Properties
 import java.util.logging.Level
 import java.util.logging.Logger
 import javax.swing.*
-import javax.swing.event.ChangeEvent
-import javax.swing.event.ChangeListener
-import kotlin.math.log10
-import kotlin.math.pow
-import kotlin.math.roundToInt
 import kotlin.text.toInt
 
 /* work around name shadowing */
@@ -130,10 +124,10 @@
             add(Box(BoxLayout.X_AXIS).apply {
                 alignmentX = LEFT_ALIGNMENT
                 border = BorderFactory.createEmptyBorder(BW2, BW2, BW, BW2)
-                add(leftLabel("Maximum dimension: "))
+                add(leftLabel("Default maximum dimension: "))
                 add(_maxDimension)
                 add(Box.createGlue())
-                add(leftLabel("Output quality: "))
+                add(leftLabel("Default output quality: "))
                 add(_outputQuality)
             })
             add(Box(BoxLayout.X_AXIS).apply {
@@ -165,7 +159,6 @@
                 add(Box.createGlue())
             })
         }
-        rootPane.setDefaultButton(_ok)
         pack()
         setResizable(false)
     }