diff src/name/blackcap/imageprep/MaxDimSpinner.kt @ 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
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
             }
         })
     }