diff src/name/blackcap/clipman/CoerceDialog.kt @ 56:22725d4d7849

An attempt to get it to troff-ize styled text.
author David Barts <n5jrn@me.com>
date Sat, 19 Mar 2022 23:04:40 -0700
parents 19d9da731c43
children 88056f373a94
line wrap: on
line diff
--- a/src/name/blackcap/clipman/CoerceDialog.kt	Tue Apr 13 10:34:51 2021 -0700
+++ b/src/name/blackcap/clipman/CoerceDialog.kt	Sat Mar 19 23:04:40 2022 -0700
@@ -162,36 +162,28 @@
 
     private fun coerce() {
         val selected = Application.queue.getSelected()
+        if (!suitedForCoercing(selected)) {
+            return
+        }
         if (selected == null) {
-            JOptionPane.showMessageDialog(Application.frame,
-                "No item selected.",
-                "Error",
-                JOptionPane.ERROR_MESSAGE)
-        } else {
-            val (plain, html) = when (selected.contents) {
-                is PasteboardItem.Plain ->
-                    Pair(selected.contents.plain, null)
-                is PasteboardItem.HTML ->
-                    Pair(selected.contents.plain, selected.contents.html)
-                is PasteboardItem.RTF ->
-                    Pair(selected.contents.plain, selected.contents.html)
-            }
-            if (html == null) {
-                JOptionPane.showMessageDialog(Application.frame,
-                    "Only styled texts may be coerced.",
-                    "Error",
-                    JOptionPane.ERROR_MESSAGE)
-            } else {
-                if (badSize(_pSize, PROP_SIZE, "proportionally-spaced") || badSize(_mSize, MONO_SIZE, "monospaced")) {
-                    return
-                }
-                PasteboardItem.write(
-                    PasteboardItem.HTML(
-                        plain,
-                        coerceHTML(html, normalizeFont(pFamily), pSize,
-                            normalizeFont(mFamily), mSize)))
-            }
+            return /* redundant, but makes kotlinc happy */
+        }
+        if (badSize(_pSize, PROP_SIZE, "proportionally-spaced") || badSize(_mSize, MONO_SIZE, "monospaced")) {
+            return
         }
+        val (plain, html) = when (selected.contents) {
+            is PasteboardItem.Plain ->
+                Pair(selected.contents.plain, null)
+            is PasteboardItem.HTML ->
+                Pair(selected.contents.plain, selected.contents.html)
+            is PasteboardItem.RTF ->
+                Pair(selected.contents.plain, selected.contents.html)
+        }
+        PasteboardItem.write(
+            PasteboardItem.HTML(
+                plain,
+                coerceHTML(html!!, normalizeFont(pFamily), pSize,
+                    normalizeFont(mFamily), mSize)))
     }
 
     private fun badSize(control: JComboBox<Float>, default: Int, fontType: String): Boolean {
@@ -226,3 +218,25 @@
         }
     }
 }
+
+/**
+ * See if the selected pasteboard item is suitable for coercing. If not,
+ * issue an error dialog.
+ */
+fun suitedForCoercing(selected: QueueItem?): Boolean {
+    if (selected == null) {
+        JOptionPane.showMessageDialog(Application.frame,
+            "No item selected.",
+            "Error",
+            JOptionPane.ERROR_MESSAGE)
+        return false
+    }
+    if (selected.contents is PasteboardItem.Plain) {
+        JOptionPane.showMessageDialog(Application.frame,
+            "Only styled texts may be coerced.",
+            "Error",
+            JOptionPane.ERROR_MESSAGE)
+        return false
+    }
+    return true
+}