diff 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
line wrap: on
line diff
--- a/src/name/blackcap/clipman/CoerceDialog.kt	Sat Feb 08 23:05:39 2020 -0700
+++ b/src/name/blackcap/clipman/CoerceDialog.kt	Sun Feb 09 19:23:16 2020 -0700
@@ -17,18 +17,18 @@
 import javax.swing.event.DocumentEvent
 import javax.swing.event.DocumentListener
 
-val FONTS =
-    GraphicsEnvironment.getLocalGraphicsEnvironment().availableFontFamilyNames.copyOf().apply {
-        sort()
-    }
-val SIZES =
-    arrayOf(9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 16.0f, 18.0f,
-        24.0f, 36.0f, 48.0f, 64.0f, 72.0f, 96.0f, 144.0f, 288.0f)
+class CoerceDialog: JDialog(frame.v), ActionListener {
+    private val FONTS =
+        GraphicsEnvironment.getLocalGraphicsEnvironment().availableFontFamilyNames.copyOf().apply {
+            sort()
+        }
+    private val SIZES =
+        arrayOf(9.0f, 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 16.0f, 18.0f,
+            24.0f, 36.0f, 48.0f, 64.0f, 72.0f, 96.0f, 144.0f, 288.0f)
 
-class CoerceDialog: JDialog(frame.v), ActionListener {
     /* the proportional font family */
     private val _pFamily = JComboBox<String>(FONTS).apply {
-        selectedItem = settingsDialog.pFamily
+        selectedIndex = getFontIndex(Font.SERIF)
         alignmentX = JComboBox.LEFT_ALIGNMENT
     }
     val pFamily: String
@@ -38,7 +38,7 @@
 
     /* the proportional font size */
     private val _pSize = JComboBox<Float>(SIZES).also {
-        it.selectedItem = settingsDialog.pSize
+        it.selectedItem = PROP_SIZE.toFloat()
         it.alignmentX = JComboBox.LEFT_ALIGNMENT
         it.setEditable(true)
     }
@@ -49,7 +49,7 @@
 
     /* the monospaced font family */
     private val _mFamily = JComboBox<String>(FONTS).apply {
-        selectedItem = settingsDialog.mFamily
+        selectedIndex = getFontIndex(Font.MONOSPACED)
         alignmentX = JComboBox.LEFT_ALIGNMENT
     }
     val mFamily: String
@@ -59,7 +59,7 @@
 
     /* the monospaced font size */
     private val _mSize = JComboBox<Float>(SIZES).also {
-        it.selectedItem = settingsDialog.mSize
+        it.selectedItem = MONO_SIZE.toFloat()
         it.alignmentX = JComboBox.LEFT_ALIGNMENT
         it.setEditable(true)
     }
@@ -182,7 +182,7 @@
                     "Error",
                     JOptionPane.ERROR_MESSAGE)
             } else {
-                if (badSize(_pSize, settingsDialog.pSize, "proportionally-spaced") || badSize(_mSize, settingsDialog.mSize, "monospaced")) {
+                if (badSize(_pSize, PROP_SIZE, "proportionally-spaced") || badSize(_mSize, MONO_SIZE, "monospaced")) {
                     return
                 }
                 PasteboardItem.write(
@@ -194,19 +194,28 @@
         }
     }
 
-    private fun badSize(control: JComboBox<Float>, default: Float, fontType: String): Boolean {
+    private fun badSize(control: JComboBox<Float>, default: Int, fontType: String): Boolean {
         val size = control.selectedItem as? Float
         if (size == null || size < 1.0f) {
             JOptionPane.showMessageDialog(frame.v,
                 "Invalid ${fontType} font size.",
                 "Error",
                 JOptionPane.ERROR_MESSAGE)
-            control.selectedItem = default
+            control.selectedItem = default.toFloat()
             return true
         }
         return false
     }
 
+    private fun getFontIndex(font: String): Int {
+        val found = FONTS.indexOf(font)
+        if (found < 0) {
+            LOGGER.log(Level.WARNING, "font '${font}' not found")
+            return 0
+        }
+        return found
+    }
+
     private fun normalizeFont(font: String): String {
         val lcFont = font.toLowerCase()
         return when (lcFont) {