changeset 43:339e2da5bf83

Simplified the settings. Compiles, not tested.
author David Barts <n5jrn@me.com>
date Sun, 09 Feb 2020 19:23:16 -0700
parents 99220aa136d9
children d14298ef8b0a
files src/name/blackcap/clipman/CoerceDialog.kt src/name/blackcap/clipman/Main.kt src/name/blackcap/clipman/SettingsDialog.kt src/name/blackcap/clipman/default.properties
diffstat 4 files changed, 32 insertions(+), 153 deletions(-) [+]
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) {
--- a/src/name/blackcap/clipman/Main.kt	Sat Feb 08 23:05:39 2020 -0700
+++ b/src/name/blackcap/clipman/Main.kt	Sun Feb 09 19:23:16 2020 -0700
@@ -18,7 +18,6 @@
 import javax.swing.*
 import javax.swing.text.html.StyleSheet
 import kotlin.concurrent.thread
-import kotlin.math.roundToInt
 import org.jsoup.Jsoup
 import org.jsoup.nodes.*
 
@@ -76,7 +75,7 @@
                         PasteboardItemView("Plain text", ClipText(contents).apply {
                             contentType = "text/plain"
                             text = plain
-                            font = Font(settingsDialog.mFamily, Font.PLAIN, settingsDialog.mSize.roundToInt())
+                            font = Font(Font.MONOSPACED, Font.PLAIN, MONO_SIZE)
                             resize()
                         })
                     } else {
@@ -109,10 +108,8 @@
 
     private fun preproc(html: String): Pair<String, StyleSheet> {
         val sty = StyleSheet().apply {
-            addRule("body { font-family: \"%s\"; font-size: %.2f; }".format(
-                settingsDialog.pFamily, settingsDialog.pSize))
-            addRule("code, kbd, pre, samp, tt { font-family: \"%s\"; font-size: %.2f}; }".format(
-                settingsDialog.mFamily, settingsDialog.mSize))
+            addRule("body { font-family: serif; font-size: ${PROP_SIZE}; }")
+            addRule("code, kbd, pre, samp, tt { font-family: monospace; font-size: ${MONO_SIZE}; }")
         }
         val scrubbed = Jsoup.parse(html).run {
             select("style").forEach {
--- a/src/name/blackcap/clipman/SettingsDialog.kt	Sat Feb 08 23:05:39 2020 -0700
+++ b/src/name/blackcap/clipman/SettingsDialog.kt	Sun Feb 09 19:23:16 2020 -0700
@@ -3,12 +3,6 @@
  */
 package name.blackcap.clipman
 
-import java.awt.Color
-import java.awt.Container
-import java.awt.Dimension
-import java.awt.Font
-import java.awt.GraphicsEnvironment
-import java.awt.Toolkit
 import java.awt.event.ActionEvent
 import java.awt.event.ActionListener
 import java.io.BufferedWriter
@@ -25,55 +19,12 @@
 import kotlin.math.log10
 import kotlin.math.pow
 import kotlin.math.roundToInt
-import kotlin.text.toFloat
 import kotlin.text.toInt
 
 /* work around name shadowing */
 private val _PROPS = PROPERTIES
 
 class SettingsDialog: JDialog(frame.v), ActionListener, ChangeListener {
-    /* the proportional font family */
-    private val _pFamily = JComboBox<String>(FONTS).apply {
-        selectedItem = _PROPS.getString("prop.family")
-        alignmentX = JComboBox.LEFT_ALIGNMENT
-    }
-    val pFamily: String
-    get() {
-        return _pFamily.selectedItem as String
-    }
-
-    /* the proportional font size */
-    private val _pSize = JComboBox<Float>(SIZES).also {
-        it.selectedItem = _PROPS.getFloat("prop.size")
-        it.alignmentX = JComboBox.LEFT_ALIGNMENT
-        it.setEditable(true)
-    }
-    val pSize: Float
-    get() {
-        return _pSize.selectedItem as Float
-    }
-
-    /* the monospaced font family */
-    private val _mFamily = JComboBox<String>(FONTS).apply {
-        selectedItem = _PROPS.getString("mono.family")
-        alignmentX = JComboBox.LEFT_ALIGNMENT
-    }
-    val mFamily: String
-    get() {
-        return _mFamily.selectedItem as String
-    }
-
-    /* the monospaced font size */
-    private val _mSize = JComboBox<Float>(SIZES).also {
-        it.selectedItem = _PROPS.getFloat("mono.size")
-        it.alignmentX = JComboBox.LEFT_ALIGNMENT
-        it.setEditable(true)
-    }
-    val mSize: Float
-    get() {
-        return _mSize.selectedItem as Float
-    }
-
     /* max queue length */
     private val _qLength = _PROPS.getInt("queue.length")
     private val _qlSlider = JSlider(10000, 30000, spinToSlide(_qLength)).also {
@@ -109,59 +60,12 @@
         it.addActionListener(this)
     }
 
-    private val _rad = JButton("Restore All Defaults").also {
-        it.actionCommand = "Restore"
-        it.addActionListener(this)
-    }
-
     /* initializer */
     init {
         title = "Preferences"
         contentPane.apply {
             add(Box(BoxLayout.Y_AXIS).apply {
                 add(Box(BoxLayout.Y_AXIS).apply {
-                    border = BorderFactory.createEmptyBorder(BW2, BW2, BW, BW2)
-                    alignmentX = Box.CENTER_ALIGNMENT
-                    add(leftLabel("Default proportionally-spaced font:"))
-                    add(Box.createVerticalStrut(BW))
-                    add(Box(BoxLayout.X_AXIS).apply {
-                        alignmentX = Box.LEFT_ALIGNMENT
-                        add(Box.createGlue())
-                        add(Box(BoxLayout.Y_AXIS).apply {
-                            add(leftLabel("Family:"))
-                            add(_pFamily)
-                        })
-                        add(Box.createGlue())
-                        add(Box(BoxLayout.Y_AXIS).apply {
-                            add(leftLabel("Size:"))
-                            add(_pSize)
-                        })
-                        add(Box.createGlue())
-                    })
-                })
-                add(JSeparator())
-                add(Box(BoxLayout.Y_AXIS).apply {
-                    alignmentX = Box.CENTER_ALIGNMENT
-                    border = BorderFactory.createEmptyBorder(BW, BW2, BW, BW2)
-                    add(leftLabel("Default monospaced font:"))
-                    add(Box.createVerticalStrut(BW))
-                    add(Box(BoxLayout.X_AXIS).apply {
-                        alignmentX = Box.LEFT_ALIGNMENT
-                        add(Box.createGlue())
-                        add(Box(BoxLayout.Y_AXIS).apply {
-                            add(leftLabel("Family:"))
-                            add(_mFamily)
-                        })
-                        add(Box.createGlue())
-                        add(Box(BoxLayout.Y_AXIS).apply {
-                            add(leftLabel("Size:"))
-                            add(_mSize)
-                        })
-                        add(Box.createGlue())
-                    })
-                })
-                add(JSeparator())
-                add(Box(BoxLayout.Y_AXIS).apply {
                     alignmentX = Box.CENTER_ALIGNMENT
                     border = BorderFactory.createEmptyBorder(BW, BW2, BW, BW2)
                     add(leftLabel("Maximum queue size:"))
@@ -180,8 +84,6 @@
                     alignmentX = Box.CENTER_ALIGNMENT
                     border = BorderFactory.createEmptyBorder(BW, BW2, BW, BW2)
                     add(Box.createGlue())
-                    add(_rad)
-                    add(Box.createGlue())
                     add(_cancel)
                     add(Box.createGlue())
                     add(_ok)
@@ -201,8 +103,10 @@
                 queue.v.maxSize = qLength
                 setVisible(false)
             }
-            "Cancel" -> setVisible(false)
-            "Restore" -> revertProperties()
+            "Cancel" -> {
+                revertValues()
+                setVisible(false)
+            }
         }
     }
 
@@ -222,29 +126,8 @@
         alignmentX = JLabel.LEFT_ALIGNMENT
     }
 
-    private fun badSize(control: JComboBox<Float>, default: Float, 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
-            return true
-        }
-        return false
-    }
-
-    private fun revertProperties()
+    private fun revertValues()
     {
-        val params = arrayOf("mono.family", "mono.size", "prop.family", "prop.size", "queue.length")
-        for (param in params) {
-            _PROPS.put(param, DPROPERTIES.get(param))
-        }
-        _mFamily.selectedItem = _PROPS.getString("mono.family")
-        _mSize.selectedItem = _PROPS.getFloat("mono.size")
-        _pFamily.selectedItem = _PROPS.getString("prop.family")
-        _pSize.selectedItem = _PROPS.getFloat("prop.size")
         val ql = _PROPS.getInt("queue.length")
         _qlSpinner.value = ql
         _qlSlider.value = spinToSlide(ql)
@@ -254,10 +137,6 @@
     {
         try {
             BufferedWriter(OutputStreamWriter(FileOutputStream(PROP_FILE), CHARSET)).use {
-                _PROPS.put("mono.family", mFamily)
-                _PROPS.put("mono.size", mSize.toString())
-                _PROPS.put("prop.family", pFamily)
-                _PROPS.put("prop.size", pSize.toString())
                 _PROPS.put("queue.length", qLength.toString())
                 _PROPS.store(it, null)
             }
@@ -280,5 +159,3 @@
 fun Properties.getString(key: String): String = get(key) as String
 
 fun Properties.getInt(key: String): Int = getString(key).toInt()
-
-fun Properties.getFloat(key: String): Float = getString(key).toFloat()
--- a/src/name/blackcap/clipman/default.properties	Sat Feb 08 23:05:39 2020 -0700
+++ b/src/name/blackcap/clipman/default.properties	Sun Feb 09 19:23:16 2020 -0700
@@ -1,6 +1,2 @@
 # This is an ISO-8859-1 file.
-mono.family=Monospaced
-mono.size=14.0
-prop.family=Serif
-prop.size=16.0
 queue.length=10