comparison src/name/blackcap/clipman/CoerceDialog.kt @ 43:339e2da5bf83

Simplified the settings. Compiles, not tested.
author David Barts <n5jrn@me.com>
date Sun, 09 Feb 2020 19:23:16 -0700
parents 33fbe3a78d84
children 19d9da731c43
comparison
equal deleted inserted replaced
42:99220aa136d9 43:339e2da5bf83
15 import java.util.logging.Logger 15 import java.util.logging.Logger
16 import javax.swing.* 16 import javax.swing.*
17 import javax.swing.event.DocumentEvent 17 import javax.swing.event.DocumentEvent
18 import javax.swing.event.DocumentListener 18 import javax.swing.event.DocumentListener
19 19
20 val FONTS =
21 GraphicsEnvironment.getLocalGraphicsEnvironment().availableFontFamilyNames.copyOf().apply {
22 sort()
23 }
24 val SIZES =
25 arrayOf(9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 16.0f, 18.0f,
26 24.0f, 36.0f, 48.0f, 64.0f, 72.0f, 96.0f, 144.0f, 288.0f)
27
28 class CoerceDialog: JDialog(frame.v), ActionListener { 20 class CoerceDialog: JDialog(frame.v), ActionListener {
21 private val FONTS =
22 GraphicsEnvironment.getLocalGraphicsEnvironment().availableFontFamilyNames.copyOf().apply {
23 sort()
24 }
25 private val SIZES =
26 arrayOf(9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 16.0f, 18.0f,
27 24.0f, 36.0f, 48.0f, 64.0f, 72.0f, 96.0f, 144.0f, 288.0f)
28
29 /* the proportional font family */ 29 /* the proportional font family */
30 private val _pFamily = JComboBox<String>(FONTS).apply { 30 private val _pFamily = JComboBox<String>(FONTS).apply {
31 selectedItem = settingsDialog.pFamily 31 selectedIndex = getFontIndex(Font.SERIF)
32 alignmentX = JComboBox.LEFT_ALIGNMENT 32 alignmentX = JComboBox.LEFT_ALIGNMENT
33 } 33 }
34 val pFamily: String 34 val pFamily: String
35 get() { 35 get() {
36 return _pFamily.selectedItem as String 36 return _pFamily.selectedItem as String
37 } 37 }
38 38
39 /* the proportional font size */ 39 /* the proportional font size */
40 private val _pSize = JComboBox<Float>(SIZES).also { 40 private val _pSize = JComboBox<Float>(SIZES).also {
41 it.selectedItem = settingsDialog.pSize 41 it.selectedItem = PROP_SIZE.toFloat()
42 it.alignmentX = JComboBox.LEFT_ALIGNMENT 42 it.alignmentX = JComboBox.LEFT_ALIGNMENT
43 it.setEditable(true) 43 it.setEditable(true)
44 } 44 }
45 val pSize: Float 45 val pSize: Float
46 get() { 46 get() {
47 return _pSize.selectedItem as Float 47 return _pSize.selectedItem as Float
48 } 48 }
49 49
50 /* the monospaced font family */ 50 /* the monospaced font family */
51 private val _mFamily = JComboBox<String>(FONTS).apply { 51 private val _mFamily = JComboBox<String>(FONTS).apply {
52 selectedItem = settingsDialog.mFamily 52 selectedIndex = getFontIndex(Font.MONOSPACED)
53 alignmentX = JComboBox.LEFT_ALIGNMENT 53 alignmentX = JComboBox.LEFT_ALIGNMENT
54 } 54 }
55 val mFamily: String 55 val mFamily: String
56 get() { 56 get() {
57 return _mFamily.selectedItem as String 57 return _mFamily.selectedItem as String
58 } 58 }
59 59
60 /* the monospaced font size */ 60 /* the monospaced font size */
61 private val _mSize = JComboBox<Float>(SIZES).also { 61 private val _mSize = JComboBox<Float>(SIZES).also {
62 it.selectedItem = settingsDialog.mSize 62 it.selectedItem = MONO_SIZE.toFloat()
63 it.alignmentX = JComboBox.LEFT_ALIGNMENT 63 it.alignmentX = JComboBox.LEFT_ALIGNMENT
64 it.setEditable(true) 64 it.setEditable(true)
65 } 65 }
66 val mSize: Float 66 val mSize: Float
67 get() { 67 get() {
180 JOptionPane.showMessageDialog(frame.v, 180 JOptionPane.showMessageDialog(frame.v,
181 "Only styled texts may be coerced.", 181 "Only styled texts may be coerced.",
182 "Error", 182 "Error",
183 JOptionPane.ERROR_MESSAGE) 183 JOptionPane.ERROR_MESSAGE)
184 } else { 184 } else {
185 if (badSize(_pSize, settingsDialog.pSize, "proportionally-spaced") || badSize(_mSize, settingsDialog.mSize, "monospaced")) { 185 if (badSize(_pSize, PROP_SIZE, "proportionally-spaced") || badSize(_mSize, MONO_SIZE, "monospaced")) {
186 return 186 return
187 } 187 }
188 PasteboardItem.write( 188 PasteboardItem.write(
189 PasteboardItem.HTML( 189 PasteboardItem.HTML(
190 plain, 190 plain,
192 normalizeFont(mFamily), mSize))) 192 normalizeFont(mFamily), mSize)))
193 } 193 }
194 } 194 }
195 } 195 }
196 196
197 private fun badSize(control: JComboBox<Float>, default: Float, fontType: String): Boolean { 197 private fun badSize(control: JComboBox<Float>, default: Int, fontType: String): Boolean {
198 val size = control.selectedItem as? Float 198 val size = control.selectedItem as? Float
199 if (size == null || size < 1.0f) { 199 if (size == null || size < 1.0f) {
200 JOptionPane.showMessageDialog(frame.v, 200 JOptionPane.showMessageDialog(frame.v,
201 "Invalid ${fontType} font size.", 201 "Invalid ${fontType} font size.",
202 "Error", 202 "Error",
203 JOptionPane.ERROR_MESSAGE) 203 JOptionPane.ERROR_MESSAGE)
204 control.selectedItem = default 204 control.selectedItem = default.toFloat()
205 return true 205 return true
206 } 206 }
207 return false 207 return false
208 }
209
210 private fun getFontIndex(font: String): Int {
211 val found = FONTS.indexOf(font)
212 if (found < 0) {
213 LOGGER.log(Level.WARNING, "font '${font}' not found")
214 return 0
215 }
216 return found
208 } 217 }
209 218
210 private fun normalizeFont(font: String): String { 219 private fun normalizeFont(font: String): String {
211 val lcFont = font.toLowerCase() 220 val lcFont = font.toLowerCase()
212 return when (lcFont) { 221 return when (lcFont) {