annotate src/name/blackcap/clipman/Misc.kt @ 42:99220aa136d9

Add default.properties.
author David Barts <n5jrn@me.com>
date Sat, 08 Feb 2020 23:05:39 -0700
parents 33fbe3a78d84
children 19d9da731c43
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
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 /**
31
0c6c18a733b7 Compiles, new menu still a mess.
David Barts <n5jrn@me.com>
parents: 27
diff changeset
13 * 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
14 * things.
0c6c18a733b7 Compiles, new menu still a mess.
David Barts <n5jrn@me.com>
parents: 27
diff changeset
15 */
0c6c18a733b7 Compiles, new menu still a mess.
David Barts <n5jrn@me.com>
parents: 27
diff changeset
16 val CHARSET_NAME = "UTF-8"
41
33fbe3a78d84 Got the settings stuff compiling (untested).
David Barts <n5jrn@me.com>
parents: 31
diff changeset
17 val CHARSET = Charset.forName(CHARSET_NAME)
31
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 /**
27
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
20 * 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
21 * 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
22 * 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
23 * @param &lt;T&gt; type of the associated value
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
24 */
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
25 class LateInit<T> {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
26 private var _v: T? = null
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
27
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
28 /**
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
29 * 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
30 * deliberately short.
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 var v: T
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
33 get() {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
34 if (_v == null) {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
35 throw IllegalStateException("cannot retrieve un-initialized value")
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
36 } else {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
37 return _v!!
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 @Synchronized set(value) {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
41 if (_v != null) {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
42 throw IllegalStateException("cannot set already-initialized value")
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 _v = value
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
45 }
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, asynchronously.
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 inSwingThread(block: () -> Unit) {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
53 SwingUtilities.invokeLater(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 * Run something in the Swing thread, synchronously.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
58 * @param block lambda containing code to run
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 inSynSwingThread(block: () -> Unit) {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
61 SwingUtilities.invokeAndWait(Runnable(block))
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 /**
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
65 * Autosize a JTextComponent to a given width.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
66 * @param width the width
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 fun JTextComponent.autoSize(width: Int): Unit {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
69 val SLOP = 10
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
70 val dim = Dimension(width, width)
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
71 preferredSize = dim
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
72 size = dim
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
73 val r = modelToView(document.length)
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
74 preferredSize = Dimension(width, r.y + r.height + SLOP)
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
75 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
76
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 * 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
79 * (control, command, etc.) for the system we're on.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
80 * @param key KeyEvent constant describing the key
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
81 */
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
82 fun JMenuItem.makeShortcut(key: Int): Unit {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
83 val SC_KEY_MASK = Toolkit.getDefaultToolkit().menuShortcutKeyMask
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
84 setAccelerator(KeyStroke.getKeyStroke(key, SC_KEY_MASK))
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
85 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
86
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
87 /**
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
88 * 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
89 * specified text.
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
90 * @param text to match
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
91 * @return first matched element, null if no match found
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 fun MenuElement.getItem(name: String) : JMenuItem? {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
94 subElements.forEach {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
95 val jMenuItem = it.component as? JMenuItem
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
96 if (jMenuItem?.text == name) {
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
97 return jMenuItem
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
98 }
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 return null
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
101 }
8aa2dfac27eb Big reorg; compiled but untested.
David Barts <n5jrn@me.com>
parents:
diff changeset
102