27
|
1 /*
|
|
2 * Miscellaneous utility stuff.
|
|
3 */
|
|
4 package name.blackcap.clipman
|
|
5
|
|
6 import java.awt.Dimension
|
|
7 import java.awt.Toolkit
|
|
8 import javax.swing.*
|
|
9 import javax.swing.text.JTextComponent
|
|
10
|
|
11 /**
|
31
|
12 * Name of the character set (encoding) we preferentially use for many
|
|
13 * things.
|
|
14 */
|
|
15 val CHARSET_NAME = "UTF-8"
|
|
16
|
|
17 /**
|
27
|
18 * Allows a val to have lateinit functionality. It is an error to attempt
|
|
19 * to retrieve this object's value unless it has been set, and it is an
|
|
20 * error to attempt to set the value of an already-set object.
|
|
21 * @param <T> type of the associated value
|
|
22 */
|
|
23 class LateInit<T> {
|
|
24 private var _v: T? = null
|
|
25
|
|
26 /**
|
|
27 * The value associated with this object. The name of this property is
|
|
28 * deliberately short.
|
|
29 */
|
|
30 var v: T
|
|
31 get() {
|
|
32 if (_v == null) {
|
|
33 throw IllegalStateException("cannot retrieve un-initialized value")
|
|
34 } else {
|
|
35 return _v!!
|
|
36 }
|
|
37 }
|
|
38 @Synchronized set(value) {
|
|
39 if (_v != null) {
|
|
40 throw IllegalStateException("cannot set already-initialized value")
|
|
41 }
|
|
42 _v = value
|
|
43 }
|
|
44 }
|
|
45
|
|
46 /**
|
|
47 * Run something in the Swing thread, asynchronously.
|
|
48 * @param block lambda containing code to run
|
|
49 */
|
|
50 fun inSwingThread(block: () -> Unit) {
|
|
51 SwingUtilities.invokeLater(Runnable(block))
|
|
52 }
|
|
53
|
|
54 /**
|
|
55 * Run something in the Swing thread, synchronously.
|
|
56 * @param block lambda containing code to run
|
|
57 */
|
|
58 fun inSynSwingThread(block: () -> Unit) {
|
|
59 SwingUtilities.invokeAndWait(Runnable(block))
|
|
60 }
|
|
61
|
|
62 /**
|
|
63 * Autosize a JTextComponent to a given width.
|
|
64 * @param width the width
|
|
65 */
|
|
66 fun JTextComponent.autoSize(width: Int): Unit {
|
|
67 val SLOP = 10
|
|
68 val dim = Dimension(width, width)
|
|
69 preferredSize = dim
|
|
70 size = dim
|
|
71 val r = modelToView(document.length)
|
|
72 preferredSize = Dimension(width, r.y + r.height + SLOP)
|
|
73 }
|
|
74
|
|
75 /**
|
|
76 * Make a shortcut for a menu item, using the standard combining key
|
|
77 * (control, command, etc.) for the system we're on.
|
|
78 * @param key KeyEvent constant describing the key
|
|
79 */
|
|
80 fun JMenuItem.makeShortcut(key: Int): Unit {
|
|
81 val SC_KEY_MASK = Toolkit.getDefaultToolkit().menuShortcutKeyMask
|
|
82 setAccelerator(KeyStroke.getKeyStroke(key, SC_KEY_MASK))
|
|
83 }
|
|
84
|
|
85 /**
|
|
86 * Given a MenuElement object, get the item whose text matches the
|
|
87 * specified text.
|
|
88 * @param text to match
|
|
89 * @return first matched element, null if no match found
|
|
90 */
|
|
91 fun MenuElement.getItem(name: String) : JMenuItem? {
|
|
92 subElements.forEach {
|
|
93 val jMenuItem = it.component as? JMenuItem
|
|
94 if (jMenuItem?.text == name) {
|
|
95 return jMenuItem
|
|
96 }
|
|
97 }
|
|
98 return null
|
|
99 }
|
|
100
|