annotate src/name/blackcap/clipman/Misc.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 3f8409470fdf
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
1 /*
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
2 * Miscellaneous utility stuff.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
3 */
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
4 package name.blackcap.clipman
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
5
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
6 import java.awt.Dimension
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
7 import java.awt.Toolkit
41
33fbe3a78d84 Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents: 31
diff changeset
8 import java.nio.charset.Charset
27
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
9 import javax.swing.*
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
10 import javax.swing.text.JTextComponent
47
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
11 import kotlin.annotation.*
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
12 import kotlin.properties.ReadWriteProperty
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
13 import kotlin.reflect.*
27
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
14
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
15 /**
31
0c6c18a733b7 Compiles, new menu still a mess.
David Barts <n5jrn@me.com>
parents: 27
diff changeset
16 * Name of the character set (encoding) we preferentially use for many
0c6c18a733b7 Compiles, new menu still a mess.
David Barts <n5jrn@me.com>
parents: 27
diff changeset
17 * things.
0c6c18a733b7 Compiles, new menu still a mess.
David Barts <n5jrn@me.com>
parents: 27
diff changeset
18 */
0c6c18a733b7 Compiles, new menu still a mess.
David Barts <n5jrn@me.com>
parents: 27
diff changeset
19 val CHARSET_NAME = "UTF-8"
41
33fbe3a78d84 Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents: 31
diff changeset
20 val CHARSET = Charset.forName(CHARSET_NAME)
31
0c6c18a733b7 Compiles, new menu still a mess.
David Barts <n5jrn@me.com>
parents: 27
diff changeset
21
0c6c18a733b7 Compiles, new menu still a mess.
David Barts <n5jrn@me.com>
parents: 27
diff changeset
22 /**
47
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
23 * Delegate that makes a var that can only be set once. This is commonly
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
24 * needed in Swing, because some vars inevitably need to be declared at
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
25 * outer levels but initialized in the Swing event dispatch thread.
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
26 *
27
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
27 * @param &lt;T&gt; type of the associated value
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
28 */
50
3f8409470fdf Input type to SetOnceImpl cannot be nullable.
David Barts <n5jrn@me.com>
parents: 47
diff changeset
29 class SetOnceImpl<T: Any>: ReadWriteProperty<Any?,T> {
47
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
30 private var setOnceValue: T? = null
27
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
31
47
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
32 override operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
33 if (setOnceValue == null) {
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
34 throw RuntimeException("${property.name} has not been initialized")
27
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
35 } else {
47
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
36 return setOnceValue!!
27
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
37 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
38 }
47
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
39
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
40 @Synchronized
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
41 override operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T): Unit {
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
42 if (setOnceValue != null) {
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
43 throw RuntimeException("${property.name} has already been initialized")
27
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
44 }
47
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
45 setOnceValue = value
27
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
46 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
47 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
48
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
49 /**
47
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
50 * Normal way to create a setOnce var:
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
51 * var something: SomeType by setOnce()
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
52 */
50
3f8409470fdf Input type to SetOnceImpl cannot be nullable.
David Barts <n5jrn@me.com>
parents: 47
diff changeset
53 fun <T: Any> setOnce(): SetOnceImpl<T> = SetOnceImpl<T>()
47
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
54
19d9da731c43 Recoded; cleaned up root namespace, removed race conditions.
David Barts <n5jrn@me.com>
parents: 41
diff changeset
55 /**
27
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
56 * Run something in the Swing thread, asynchronously.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 * @param block lambda containing code to run
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 */
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
59 fun inSwingThread(block: () -> Unit) {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
60 SwingUtilities.invokeLater(Runnable(block))
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
62
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 /**
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 * Run something in the Swing thread, synchronously.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 * @param block lambda containing code to run
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
66 */
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
67 fun inSynSwingThread(block: () -> Unit) {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
68 SwingUtilities.invokeAndWait(Runnable(block))
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
70
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 /**
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 * Autosize a JTextComponent to a given width.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 * @param width the width
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
74 */
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
75 fun JTextComponent.autoSize(width: Int): Unit {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
76 val SLOP = 10
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
77 val dim = Dimension(width, width)
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
78 preferredSize = dim
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 size = dim
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
80 val r = modelToView(document.length)
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
81 preferredSize = Dimension(width, r.y + r.height + SLOP)
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
83
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
84 /**
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
85 * Make a shortcut for a menu item, using the standard combining key
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
86 * (control, command, etc.) for the system we're on.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
87 * @param key KeyEvent constant describing the key
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
88 */
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
89 fun JMenuItem.makeShortcut(key: Int): Unit {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
90 val SC_KEY_MASK = Toolkit.getDefaultToolkit().menuShortcutKeyMask
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
91 setAccelerator(KeyStroke.getKeyStroke(key, SC_KEY_MASK))
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
92 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
93
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
94 /**
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
95 * Given a MenuElement object, get the item whose text matches the
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
96 * specified text.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
97 * @param text to match
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
98 * @return first matched element, null if no match found
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
99 */
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
100 fun MenuElement.getItem(name: String) : JMenuItem? {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
101 subElements.forEach {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
102 val jMenuItem = it.component as? JMenuItem
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
103 if (jMenuItem?.text == name) {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
104 return jMenuItem
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
105 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
106 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
107 return null
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
108 }