annotate src/name/blackcap/clipman/Misc.kt @ 27:8aa2dfac27eb

Big reorg; compiled but untested.
author David Barts <n5jrn@me.com>
date Wed, 29 Jan 2020 10:50:07 -0800
parents
children 0c6c18a733b7
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
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
8 import javax.swing.*
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
9 import javax.swing.text.JTextComponent
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
10
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
11 /**
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
12 * Allows a val to have lateinit functionality. It is an error to attempt
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
13 * to retrieve this object's value unless it has been set, and it is an
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
14 * error to attempt to set the value of an already-set object.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
15 * @param &lt;T&gt; type of the associated value
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
16 */
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
17 class LateInit<T> {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
18 private var _v: T? = null
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
19
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
20 /**
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
21 * The value associated with this object. The name of this property is
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
22 * deliberately short.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
23 */
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 var v: T
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 get() {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
26 if (_v == null) {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
27 throw IllegalStateException("cannot retrieve un-initialized value")
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
28 } else {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
29 return _v!!
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
30 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
31 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
32 @Synchronized set(value) {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
33 if (_v != null) {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
34 throw IllegalStateException("cannot set already-initialized value")
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
35 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
36 _v = value
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 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
39
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
40 /**
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
41 * Run something in the Swing thread, asynchronously.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 * @param block lambda containing code to run
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
43 */
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
44 fun inSwingThread(block: () -> Unit) {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 SwingUtilities.invokeLater(Runnable(block))
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 * Run something in the Swing thread, synchronously.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
50 * @param block lambda containing code to run
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
51 */
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
52 fun inSynSwingThread(block: () -> Unit) {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 SwingUtilities.invokeAndWait(Runnable(block))
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
54 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
55
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
56 /**
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
57 * Autosize a JTextComponent to a given width.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 * @param width the width
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
59 */
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
60 fun JTextComponent.autoSize(width: Int): Unit {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 val SLOP = 10
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
62 val dim = Dimension(width, width)
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
63 preferredSize = dim
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
64 size = dim
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 val r = modelToView(document.length)
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
66 preferredSize = Dimension(width, r.y + r.height + SLOP)
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
67 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
68
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 * 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
71 * (control, command, etc.) for the system we're on.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 * @param key KeyEvent constant describing the key
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 */
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
74 fun JMenuItem.makeShortcut(key: Int): Unit {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
75 val SC_KEY_MASK = Toolkit.getDefaultToolkit().menuShortcutKeyMask
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
76 setAccelerator(KeyStroke.getKeyStroke(key, SC_KEY_MASK))
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
77 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
78
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
79 /**
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
80 * 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
81 * specified text.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 * @param text to match
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
83 * @return first matched element, null if no match found
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 fun MenuElement.getItem(name: String) : JMenuItem? {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
86 subElements.forEach {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
87 val jMenuItem = it.component as? JMenuItem
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
88 if (jMenuItem?.text == name) {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
89 return jMenuItem
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
90 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
91 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
92 return null
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