Mercurial > cgi-bin > hgweb.cgi > JpegWasher
annotate src/name/blackcap/exifwasher/Misc.kt @ 56:224572069e36
Always define an editor kit, so we always get HTML mode.
author | davidb |
---|---|
date | Thu, 07 May 2020 17:12:25 -0700 |
parents | f5faf70c7d10 |
children |
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.* |
54 | 16 import javax.swing.border.Border |
8
88d02fa97d78
Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
17 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
|
18 import kotlin.annotation.* |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
19 import kotlin.properties.ReadWriteProperty |
5 | 20 import kotlin.reflect.* |
3
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 /** |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
23 * 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
|
24 * 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
|
25 * 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
|
26 */ |
21
539c3641c539
Input type to SetOnceImpl cannot be nullable.
David Barts <n5jrn@me.com>
parents:
20
diff
changeset
|
27 class SetOnceImpl<T: Any>: ReadWriteProperty<Any?,T> { |
5 | 28 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
|
29 |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
30 override operator fun getValue(thisRef: Any?, property: KProperty<*>): T { |
5 | 31 if (setOnceValue == null) { |
3
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
32 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
|
33 } else { |
5 | 34 return setOnceValue!! |
3
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 |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
38 @Synchronized |
5 | 39 override operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T): Unit { |
40 if (setOnceValue != null) { | |
3
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
41 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
|
42 } |
5 | 43 setOnceValue = value |
3
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 } |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
46 |
21
539c3641c539
Input type to SetOnceImpl cannot be nullable.
David Barts <n5jrn@me.com>
parents:
20
diff
changeset
|
47 fun <T: Any> setOnce(): SetOnceImpl<T> = SetOnceImpl<T>() |
20 | 48 |
3
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
49 /** |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
50 * 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
|
51 * @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
|
52 */ |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
53 fun inSwingThread(block: () -> Unit) { |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
54 SwingUtilities.invokeLater(Runnable(block)) |
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 /** |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
58 * 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
|
59 * @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
|
60 */ |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
61 fun inSynSwingThread(block: () -> Unit) { |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
62 SwingUtilities.invokeAndWait(Runnable(block)) |
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 /** |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
66 * 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
|
67 * (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
|
68 * @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
|
69 */ |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
70 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
|
71 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
|
72 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
|
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 /** |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
76 * 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
|
77 * specified text. |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
78 * @param text to match |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
79 * @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
|
80 */ |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
81 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
|
82 subElements.forEach { |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
83 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
|
84 if (jMenuItem?.text == name) { |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
85 return jMenuItem |
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 } |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
88 return null |
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 /** |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
92 * 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
|
93 */ |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
94 fun Component.useWaitCursor() { |
5 | 95 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
|
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 /** |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
99 * 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
|
100 */ |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
101 fun Component.useNormalCursor() { |
5 | 102 this.cursor = Cursor.getDefaultCursor() |
3
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 /** |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
106 * 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
|
107 */ |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
108 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
|
109 |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
110 /** |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
111 * 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
|
112 * 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
|
113 * to execute when complete. |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
114 * |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
115 * @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
|
116 */ |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
117 class SwingWorkerBuilder<T>: SwingWorker<T,Unit>() { |
5 | 118 private var inBackgroundLambda: (SwingWorkerBuilder<T>.() -> T)? = null |
119 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
|
120 |
5 | 121 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
|
122 if (prop.get() != null) { |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
123 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
|
124 } |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
125 prop.set(value) |
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 /** |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
129 * Define the inBackground task. |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
130 */ |
5 | 131 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
|
132 setOnce<T>(::inBackgroundLambda, lambda) |
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 /** |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
136 * Define the whenDone task. |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
137 */ |
5 | 138 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
|
139 setOnce<Unit>(::whenDoneLambda, lambda) |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
140 } |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
141 |
5 | 142 /** |
143 * Validates we've been properly initialized. | |
144 */ | |
145 fun validate(): Unit { | |
146 if (inBackgroundLambda == null) { | |
147 throw SwingWorkerException("inBackground not defined!") | |
148 } | |
149 } | |
150 | |
3
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
151 /* standard overrides for SwingWorker follow */ |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
152 |
5 | 153 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
|
154 |
5 | 155 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
|
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 /** |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
159 * 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
|
160 */ |
5 | 161 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
|
162 SwingWorkerBuilder<T>().run { |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
163 initializer() |
5 | 164 validate() |
3
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
165 execute() |
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 /** |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
170 * 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
|
171 */ |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
172 fun JDialog.close() { |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
173 setVisible(false) |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
174 dispose() |
19c381c536ec
Code to make it a proper Mac GUI app. Untested!
David Barts <n5jrn@me.com>
parents:
diff
changeset
|
175 } |
8
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 /** |
88d02fa97d78
Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
178 * Set column width of a table. |
88d02fa97d78
Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
179 */ |
88d02fa97d78
Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
180 fun JTable.setColWidth(col: Int, width: Int, string: String?) { |
22 | 181 val FUZZ = 16 |
8
88d02fa97d78
Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
182 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
|
183 width |
8
88d02fa97d78
Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
184 } else { |
9
0a106e9b91b4
Fix table layout; fix "select all for deletion" feature.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
185 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
|
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 /** |
88d02fa97d78
Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
190 * Set overall width of a table. |
88d02fa97d78
Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
191 */ |
88d02fa97d78
Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
192 fun JTable.setOverallWidth() { |
88d02fa97d78
Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
193 val tcm = columnModel |
9
0a106e9b91b4
Fix table layout; fix "select all for deletion" feature.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
194 val limit = tcm.columnCount - 1 |
8
88d02fa97d78
Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
195 var total = 0 |
9
0a106e9b91b4
Fix table layout; fix "select all for deletion" feature.
David Barts <n5jrn@me.com>
parents:
8
diff
changeset
|
196 for (i in 0 .. limit) { |
8
88d02fa97d78
Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
197 total += tcm.getColumn(i).preferredWidth |
88d02fa97d78
Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
198 } |
88d02fa97d78
Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
199 preferredSize = Dimension(total, preferredSize.height) |
88d02fa97d78
Got WasherDialog table laying out somewhat sanely.
David Barts <n5jrn@me.com>
parents:
5
diff
changeset
|
200 } |
19
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 /** |
39b977021ea1
Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents:
9
diff
changeset
|
203 * 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
|
204 * 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
|
205 */ |
39b977021ea1
Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents:
9
diff
changeset
|
206 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
|
207 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
|
208 val pos = e.point |
39b977021ea1
Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents:
9
diff
changeset
|
209 val col = columnAtPoint(pos) |
39b977021ea1
Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents:
9
diff
changeset
|
210 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
|
211 return null |
39b977021ea1
Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents:
9
diff
changeset
|
212 } |
39b977021ea1
Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents:
9
diff
changeset
|
213 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
|
214 if (contents == null) { |
39b977021ea1
Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents:
9
diff
changeset
|
215 return null |
39b977021ea1
Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents:
9
diff
changeset
|
216 } |
39b977021ea1
Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents:
9
diff
changeset
|
217 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
|
218 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
|
219 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
|
220 } |
39b977021ea1
Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents:
9
diff
changeset
|
221 } |
39b977021ea1
Tool tips in case key and type cols truncate.
David Barts <n5jrn@me.com>
parents:
9
diff
changeset
|
222 |
54 | 223 /** |
224 * Add a border to a JComponent. The new border is in addition to (and outside | |
225 * of) whatever existing standard border the component had. | |
226 */ | |
227 fun JComponent.addBorder(b: Border) { | |
55 | 228 if (border == null) |
229 border = b | |
230 else | |
231 border = BorderFactory.createCompoundBorder(b, border) | |
54 | 232 } |