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