annotate src/name/blackcap/exifwasher/Misc.kt @ 10:9bbe6d803eba

Fixed ShowDialog.
author David Barts <n5jrn@me.com>
date Fri, 10 Apr 2020 19:29:58 -0700
parents 0a106e9b91b4
children 39b977021ea1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
1 /*
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
2 * Miscellaneous utility stuff.
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
3 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
4 package name.blackcap.exifwasher
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
5
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
6 import java.awt.Component
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
7 import java.awt.Cursor
8
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
8 import java.awt.Dimension
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
9 import java.awt.Font
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
10 import java.awt.FontMetrics
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
11 import java.awt.Graphics
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
12 import java.awt.Toolkit
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
13 import javax.swing.*
8
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
14 import javax.swing.table.TableColumnModel
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
15 import kotlin.annotation.*
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
16 import kotlin.properties.ReadWriteProperty
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
17 import kotlin.reflect.*
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
18
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
19 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
20 * Delegate that makes a var that can only be set once. This is commonly
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
21 * needed in Swing, because some vars inevitably need to be declared at
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
22 * outer levels but initialized in the Swing event dispatch thread.
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
23 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
24 class SetOnce<T: Any>: ReadWriteProperty<Any?,T> {
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
25 private var setOnceValue: T? = null
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
26
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
27 override operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
28 if (setOnceValue == null) {
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
29 throw RuntimeException("${property.name} has not been initialized")
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
30 } else {
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
31 return setOnceValue!!
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
32 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
33 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
34
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
35 @Synchronized
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
36 override operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T): Unit {
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
37 if (setOnceValue != null) {
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
38 throw RuntimeException("${property.name} has already been initialized")
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
39 }
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
40 setOnceValue = value
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
41 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
42 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
43
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
44 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
45 * Run something in the Swing thread, asynchronously.
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
46 * @param block lambda containing code to run
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
47 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
48 fun inSwingThread(block: () -> Unit) {
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
49 SwingUtilities.invokeLater(Runnable(block))
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
50 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
51
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
52 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
53 * Run something in the Swing thread, synchronously.
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
54 * @param block lambda containing code to run
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
55 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
56 fun inSynSwingThread(block: () -> Unit) {
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
57 SwingUtilities.invokeAndWait(Runnable(block))
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
58 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
59
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
60 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
61 * Make a shortcut for a menu item, using the standard combining key
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
62 * (control, command, etc.) for the system we're on.
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
63 * @param key KeyEvent constant describing the key
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
64 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
65 fun JMenuItem.makeShortcut(key: Int): Unit {
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
66 val SC_KEY_MASK = Toolkit.getDefaultToolkit().menuShortcutKeyMask
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
67 setAccelerator(KeyStroke.getKeyStroke(key, SC_KEY_MASK))
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
68 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
69
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
70 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
71 * Given a MenuElement object, get the item whose text matches the
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
72 * specified text.
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
73 * @param text to match
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
74 * @return first matched element, null if no match found
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
75 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
76 fun MenuElement.getItem(name: String) : JMenuItem? {
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
77 subElements.forEach {
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
78 val jMenuItem = it.component as? JMenuItem
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
79 if (jMenuItem?.text == name) {
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
80 return jMenuItem
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
81 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
82 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
83 return null
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
84 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
85
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
86 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
87 * Change to the standard wait cursor.
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
88 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
89 fun Component.useWaitCursor() {
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
90 this.cursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
91 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
92
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
93 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
94 * Return back to the normal cursor().
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
95 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
96 fun Component.useNormalCursor() {
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
97 this.cursor = Cursor.getDefaultCursor()
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
98 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
99
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
100 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
101 * Thrown if the programmer botches something in our DSL.
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
102 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
103 class SwingWorkerException(message: String): Exception(message) { }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
104
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
105 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
106 * A simplified SwingWorker DSL. It does not support intermediate
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
107 * results. Just lets one define a background task and something
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
108 * to execute when complete.
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
109 *
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
110 * @param T Type returned by inBackground (Java doInBackground) task.
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
111 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
112 class SwingWorkerBuilder<T>: SwingWorker<T,Unit>() {
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
113 private var inBackgroundLambda: (SwingWorkerBuilder<T>.() -> T)? = null
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
114 private var whenDoneLambda: (SwingWorkerBuilder<T>.() -> Unit)? = null
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
115
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
116 private fun <U> setOnce(prop: KMutableProperty0<(SwingWorkerBuilder<T>.() -> U)?>, value: SwingWorkerBuilder<T>.() -> U) {
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
117 if (prop.get() != null) {
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
118 throw SwingWorkerException(prop.name.removeSuffix("Lambda") + " already defined!")
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
119 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
120 prop.set(value)
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
121 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
122
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
123 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
124 * Define the inBackground task.
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
125 */
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
126 fun inBackground(lambda: SwingWorkerBuilder<T>.() -> T): Unit {
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
127 setOnce<T>(::inBackgroundLambda, lambda)
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
128 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
129
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
130 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
131 * Define the whenDone task.
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
132 */
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
133 fun whenDone(lambda: SwingWorkerBuilder<T>.() -> Unit): Unit {
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
134 setOnce<Unit>(::whenDoneLambda, lambda)
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
135 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
136
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
137 /**
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
138 * Validates we've been properly initialized.
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
139 */
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
140 fun validate(): Unit {
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
141 if (inBackgroundLambda == null) {
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
142 throw SwingWorkerException("inBackground not defined!")
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
143 }
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
144 }
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
145
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
146 /* standard overrides for SwingWorker follow */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
147
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
148 override fun doInBackground(): T = inBackgroundLambda!!.invoke(this)
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
149
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
150 override fun done(): Unit = whenDoneLambda?.invoke(this) ?: Unit
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
151 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
152
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
153 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
154 * Provides for an outer swingWorker block to contain the DSL.
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
155 */
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
156 fun <T> swingWorker(initializer: SwingWorkerBuilder<T>.() -> Unit): Unit {
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
157 SwingWorkerBuilder<T>().run {
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
158 initializer()
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
159 validate()
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
160 execute()
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
161 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
162 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
163
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
164 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
165 * Close a dialog (don't just hide it).
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
166 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
167 fun JDialog.close() {
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
168 setVisible(false)
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
169 dispose()
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
170 }
8
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
171
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
172 /**
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
173 * Set column width of a table.
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
174 */
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
175 fun JTable.setColWidth(col: Int, width: Int, string: String?) {
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
176 val FUZZ = 4
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
177 columnModel.getColumn(col).preferredWidth = if (string.isNullOrEmpty()) {
9
0a106e9b91b4 Fix table layout; fix "select all for deletion" feature.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
178 width
8
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
179 } else {
9
0a106e9b91b4 Fix table layout; fix "select all for deletion" feature.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
180 maxOf(width, graphics.fontMetrics.stringWidth(string) + FUZZ)
8
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
181 }
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
182 }
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
183
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
184 /**
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
185 * Set overall width of a table.
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
186 */
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
187 fun JTable.setOverallWidth() {
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
188 val tcm = columnModel
9
0a106e9b91b4 Fix table layout; fix "select all for deletion" feature.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
189 val limit = tcm.columnCount - 1
8
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
190 var total = 0
9
0a106e9b91b4 Fix table layout; fix "select all for deletion" feature.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
191 for (i in 0 .. limit) {
8
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
192 total += tcm.getColumn(i).preferredWidth
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
193 }
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
194 preferredSize = Dimension(total, preferredSize.height)
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
195 }