comparison 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
comparison
equal deleted inserted replaced
21:0f3634a0816d 22:d3979a2155a8
1 /* 1 /*
2 * A standard spinner for inputting the maximum dimension of an image. 2 * A standard combo box for inputting the maximum dimension of an image.
3 * Note that this IS NOT a spinner; this class is so named for historical
4 * reasons.
3 */ 5 */
4 package name.blackcap.imageprep 6 package name.blackcap.imageprep
5 7
6 import java.awt.Dimension
7 import java.awt.Toolkit 8 import java.awt.Toolkit
8 import javax.swing.* 9 import java.awt.event.ActionEvent
9 import javax.swing.event.ChangeEvent 10 import java.awt.event.ActionListener
10 import javax.swing.event.ChangeListener
11 import java.util.logging.Level 11 import java.util.logging.Level
12 import java.util.logging.Logger 12 import java.util.logging.Logger
13 import javax.swing.*
13 14
14 /* maximum allowable maximum dimension */ 15 /* maximum allowable maximum dimension */
15 private val MAXDIM = 16384 16 private val MAXDIM = 16384
16 17
17 /* preferred standard dimensions */ 18 /* preferred standard dimensions */
18 private val STDDIMS = listOf<Int>(1600, 1280, 1024, 800, 640, 512, 400, 320) 19 private val STDDIMS = arrayOf<Int>(1600, 1280, 1024, 800, 640, 512, 400, 320).apply {
20 sort()
21 }
19 22
20 class MaxDimSpinner(val default: Int): JSpinner(SpinnerListModel(STDDIMS)) 23 class MaxDimSpinner(val default: Int): JComboBox<Int>(STDDIMS)
21 { 24 {
25 private var _value: Int = 0
26 var value: Int
27 get() = _value
28 set(v) {
29 if (outOfRange(v))
30 throw IllegalArgumentException("bad max dimension: $v")
31 _value = v
32 val pos = STDDIMS.binarySearch(v)
33 if (pos >= 0)
34 selectedIndex = pos
35 else
36 selectedItem = v
37 }
38
39 private fun outOfRange(v: Int): Boolean = v < 1 || v > MAXDIM
40
22 init { 41 init {
23 editor = JSpinner.ListEditor(this).apply {
24 textField.columns = STDDIMS.map { it.toString().length }.max() ?: 1
25 }
26 value = default 42 value = default
27 maximumSize = preferredSize 43 maximumSize = preferredSize
28 addChangeListener( ChangeListener { 44 setEditable(true)
29 val v = value as? Int ?: (value as? String)?.toIntOrNull() 45 prototypeDisplayValue = MAXDIM
30 if ( v == null || v < 1 || v > MAXDIM ) { 46 addActionListener(ActionListener {
31 LOGGER.log(Level.INFO, "bad max dimension: $value") 47 val v = selectedItem as? Int ?: (selectedItem as? String)?.toIntOrNull()
48 if (v == null || outOfRange(v)) {
49 LOGGER.log(Level.INFO, "bad max dimension: $selectedItem")
50 Toolkit.getDefaultToolkit().beep()
32 value = default 51 value = default
33 Toolkit.getDefaultToolkit().beep() 52 } else {
53 _value = v
34 } 54 }
35 }) 55 })
36 } 56 }
37 } 57 }