view src/name/blackcap/imageprep/MaxDimSpinner.kt @ 21:0f3634a0816d

De-uglify SettingsDialog.
author David Barts <n5jrn@me.com>
date Sat, 21 Nov 2020 21:25:05 -0800
parents 71029c9bf7cd
children d3979a2155a8
line wrap: on
line source

/*
 * A standard spinner for inputting the maximum dimension of an image.
 */
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.util.logging.Level
import java.util.logging.Logger

/* maximum allowable maximum dimension */
private val MAXDIM = 16384

/* preferred standard dimensions */
private val STDDIMS = listOf<Int>(1600, 1280, 1024, 800, 640, 512, 400, 320)

class MaxDimSpinner(val default: Int): JSpinner(SpinnerListModel(STDDIMS))
{
    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")
                value = default
                Toolkit.getDefaultToolkit().beep()
            }
        })
    }
}