annotate src/name/blackcap/exifwasher/Misc.kt @ 25:5a5a7de27eb4

ExifWasher -> JpegWasher
author David Barts <n5jrn@me.com>
date Thu, 16 Apr 2020 19:41:29 -0700
parents cd2ca4727b7f
children 40911898ed23
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
19
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
12 import java.awt.Point
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
13 import java.awt.Toolkit
19
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
14 import java.awt.event.MouseEvent
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
15 import javax.swing.*
8
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
16 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
17 import kotlin.annotation.*
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
18 import kotlin.properties.ReadWriteProperty
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
19 import kotlin.reflect.*
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
20
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
21 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
22 * 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
23 * 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
24 * 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
25 */
21
539c3641c539 Input type to SetOnceImpl cannot be nullable.
David Barts <n5jrn@me.com>
parents: 20
diff changeset
26 class SetOnceImpl<T: Any>: ReadWriteProperty<Any?,T> {
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
27 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
28
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
29 override operator fun getValue(thisRef: Any?, property: KProperty<*>): T {
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
30 if (setOnceValue == null) {
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
31 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
32 } else {
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
33 return setOnceValue!!
3
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 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
36
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
37 @Synchronized
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
38 override operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T): Unit {
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
39 if (setOnceValue != null) {
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
40 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
41 }
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
42 setOnceValue = value
3
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
21
539c3641c539 Input type to SetOnceImpl cannot be nullable.
David Barts <n5jrn@me.com>
parents: 20
diff changeset
46 fun <T: Any> setOnce(): SetOnceImpl<T> = SetOnceImpl<T>()
20
965435b85a69 Get rid of some stuttering.
David Barts <n5jrn@me.com>
parents: 19
diff changeset
47
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
48 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
49 * 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
50 * @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
51 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
52 fun inSwingThread(block: () -> Unit) {
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
53 SwingUtilities.invokeLater(Runnable(block))
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
54 }
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 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
57 * 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
58 * @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
59 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
60 fun inSynSwingThread(block: () -> Unit) {
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
61 SwingUtilities.invokeAndWait(Runnable(block))
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
62 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
63
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 * 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
66 * (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
67 * @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
68 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
69 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
70 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
71 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
72 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
73
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
74 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
75 * 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
76 * specified text.
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
77 * @param text to match
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
78 * @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
79 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
80 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
81 subElements.forEach {
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
82 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
83 if (jMenuItem?.text == name) {
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
84 return jMenuItem
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 return null
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
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
90 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
91 * 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
92 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
93 fun Component.useWaitCursor() {
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
94 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
95 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
96
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
97 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
98 * 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
99 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
100 fun Component.useNormalCursor() {
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
101 this.cursor = Cursor.getDefaultCursor()
3
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
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 * 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
106 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
107 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
108
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 * 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
111 * 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
112 * to execute when complete.
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
113 *
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
114 * @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
115 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
116 class SwingWorkerBuilder<T>: SwingWorker<T,Unit>() {
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
117 private var inBackgroundLambda: (SwingWorkerBuilder<T>.() -> T)? = null
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
118 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
119
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
120 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
121 if (prop.get() != null) {
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
122 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
123 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
124 prop.set(value)
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
125 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
126
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
127 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
128 * Define the inBackground task.
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
129 */
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
130 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
131 setOnce<T>(::inBackgroundLambda, lambda)
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
132 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
133
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
134 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
135 * Define the whenDone task.
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 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
138 setOnce<Unit>(::whenDoneLambda, lambda)
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
139 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
140
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
141 /**
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
142 * Validates we've been properly initialized.
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 fun validate(): Unit {
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
145 if (inBackgroundLambda == null) {
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
146 throw SwingWorkerException("inBackground not defined!")
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
147 }
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
148 }
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
149
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
150 /* standard overrides for SwingWorker follow */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
151
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
152 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
153
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
154 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
155 }
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
156
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
157 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
158 * 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
159 */
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
160 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
161 SwingWorkerBuilder<T>().run {
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
162 initializer()
5
dc1f4359659d Got it compiling.
David Barts <n5jrn@me.com>
parents: 3
diff changeset
163 validate()
3
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
164 execute()
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
165 }
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
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
168 /**
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
169 * 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
170 */
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
171 fun JDialog.close() {
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
172 setVisible(false)
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
173 dispose()
19c381c536ec Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff changeset
174 }
8
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
175
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
176 /**
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
177 * Set column width of a table.
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
178 */
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
179 fun JTable.setColWidth(col: Int, width: Int, string: String?) {
22
cd2ca4727b7f Builds under Windows.
davidb
parents: 21
diff changeset
180 val FUZZ = 16
8
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
181 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
182 width
8
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
183 } else {
9
0a106e9b91b4 Fix table layout; fix "select all for deletion" feature.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
184 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
185 }
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
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
188 /**
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
189 * Set overall width of a table.
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
190 */
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
191 fun JTable.setOverallWidth() {
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
192 val tcm = columnModel
9
0a106e9b91b4 Fix table layout; fix "select all for deletion" feature.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
193 val limit = tcm.columnCount - 1
8
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
194 var total = 0
9
0a106e9b91b4 Fix table layout; fix "select all for deletion" feature.
David Barts <n5jrn@me.com>
parents: 8
diff changeset
195 for (i in 0 .. limit) {
8
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
196 total += tcm.getColumn(i).preferredWidth
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
197 }
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
198 preferredSize = Dimension(total, preferredSize.height)
88d02fa97d78 Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents: 5
diff changeset
199 }
19
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
200
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
201 /**
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
202 * A JTable for displaying metadata. Columns that might get harmfully
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
203 * truncated have tooltips when truncation happens.
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
204 */
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
205 class JExifTable(rowData: Array<Array<out Any?>>, colNames: Array<out Any?>): JTable(rowData, colNames) {
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
206 override fun getToolTipText(e: MouseEvent): String? {
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
207 val pos = e.point
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
208 val col = columnAtPoint(pos)
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
209 if (!setOf("Key", "Type").contains(getColumnName(col))) {
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
210 return null
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
211 }
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
212 val contents = getValueAt(rowAtPoint(pos), col) as String?
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
213 if (contents == null) {
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
214 return null
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
215 }
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
216 val needed = graphics.fontMetrics.stringWidth(contents)
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
217 val actual = columnModel.getColumn(col).width
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
218 return if (needed > actual) contents else null
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
219 }
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
220 }
39b977021ea1 Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents: 9
diff changeset
221