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