Mercurial > cgi-bin > hgweb.cgi > ImagePrep
annotate src/name/blackcap/imageprep/MaxDimSpinner.kt @ 31:99a0eb385c9a default tip
Work around annoying Swing glitch.
author | David Barts <n5jrn@me.com> |
---|---|
date | Sat, 20 Aug 2022 09:19:49 -0700 |
parents | d3979a2155a8 |
children |
rev | line source |
---|---|
20 | 1 /* |
22
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
2 * A standard combo box for inputting the maximum dimension of an image. |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
3 * Note that this IS NOT a spinner; this class is so named for historical |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
4 * reasons. |
20 | 5 */ |
6 package name.blackcap.imageprep | |
7 | |
8 import java.awt.Toolkit | |
22
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
9 import java.awt.event.ActionEvent |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
10 import java.awt.event.ActionListener |
20 | 11 import java.util.logging.Level |
12 import java.util.logging.Logger | |
22
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
13 import javax.swing.* |
20 | 14 |
15 /* maximum allowable maximum dimension */ | |
16 private val MAXDIM = 16384 | |
17 | |
18 /* preferred standard dimensions */ | |
22
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
19 private val STDDIMS = arrayOf<Int>(1600, 1280, 1024, 800, 640, 512, 400, 320).apply { |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
20 sort() |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
21 } |
20 | 22 |
22
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
23 class MaxDimSpinner(val default: Int): JComboBox<Int>(STDDIMS) |
20 | 24 { |
22
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
25 private var _value: Int = 0 |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
26 var value: Int |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
27 get() = _value |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
28 set(v) { |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
29 if (outOfRange(v)) |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
30 throw IllegalArgumentException("bad max dimension: $v") |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
31 _value = v |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
32 val pos = STDDIMS.binarySearch(v) |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
33 if (pos >= 0) |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
34 selectedIndex = pos |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
35 else |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
36 selectedItem = v |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
37 } |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
38 |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
39 private fun outOfRange(v: Int): Boolean = v < 1 || v > MAXDIM |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
40 |
20 | 41 init { |
42 value = default | |
21 | 43 maximumSize = preferredSize |
22
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
44 setEditable(true) |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
45 prototypeDisplayValue = MAXDIM |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
46 addActionListener(ActionListener { |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
47 val v = selectedItem as? Int ?: (selectedItem as? String)?.toIntOrNull() |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
48 if (v == null || outOfRange(v)) { |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
49 LOGGER.log(Level.INFO, "bad max dimension: $selectedItem") |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
50 Toolkit.getDefaultToolkit().beep() |
20 | 51 value = default |
22
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
52 } else { |
d3979a2155a8
Fix out qual and max dim controls.
David Barts <n5jrn@me.com>
parents:
21
diff
changeset
|
53 _value = v |
20 | 54 } |
55 }) | |
56 } | |
57 } |