Mercurial > cgi-bin > hgweb.cgi > JpegWasher
diff src/name/blackcap/exifwasher/Misc.kt @ 5:dc1f4359659d
Got it compiling.
author | David Barts <n5jrn@me.com> |
---|---|
date | Thu, 09 Apr 2020 18:20:34 -0700 |
parents | 19c381c536ec |
children | 88d02fa97d78 |
line wrap: on
line diff
--- a/src/name/blackcap/exifwasher/Misc.kt Wed Apr 08 21:31:30 2020 -0700 +++ b/src/name/blackcap/exifwasher/Misc.kt Thu Apr 09 18:20:34 2020 -0700 @@ -9,7 +9,7 @@ import javax.swing.* import kotlin.annotation.* import kotlin.properties.ReadWriteProperty -import kotlin.reflect.KProperty +import kotlin.reflect.* /** * Delegate that makes a var that can only be set once. This is commonly @@ -17,22 +17,22 @@ * outer levels but initialized in the Swing event dispatch thread. */ class SetOnce<T: Any>: ReadWriteProperty<Any?,T> { - private var value: T? = null + private var setOnceValue: T? = null override operator fun getValue(thisRef: Any?, property: KProperty<*>): T { - if (value == null) { + if (setOnceValue == null) { throw RuntimeException("${property.name} has not been initialized") } else { - return value!! + return setOnceValue!! } } @Synchronized - override operator fun setValue(thisRef: Any?, property: KProperty<*>, newValue: T): Unit { - if (value != null) { + override operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T): Unit { + if (setOnceValue != null) { throw RuntimeException("${property.name} has already been initialized") } - value = newValue + setOnceValue = value } } @@ -82,14 +82,14 @@ * Change to the standard wait cursor. */ fun Component.useWaitCursor() { - this.cursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)) + this.cursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR) } /** * Return back to the normal cursor(). */ fun Component.useNormalCursor() { - this.cursor = Cursor.defaultCursor + this.cursor = Cursor.getDefaultCursor() } /** @@ -105,10 +105,10 @@ * @param T Type returned by inBackground (Java doInBackground) task. */ class SwingWorkerBuilder<T>: SwingWorker<T,Unit>() { - private var inBackgroundLambda: (SwingWorkerBuilder.() -> T)? = null - private var whenDoneLambda: (SwingWorkerBuilder.() -> Unit)? = null + private var inBackgroundLambda: (SwingWorkerBuilder<T>.() -> T)? = null + private var whenDoneLambda: (SwingWorkerBuilder<T>.() -> Unit)? = null - private fun setOnce<U>(prop: KMutableProperty<(SwingWorkerBuilder.() -> U)?>, value: SwingWorkerBuilder.() -> U) { + private fun <U> setOnce(prop: KMutableProperty0<(SwingWorkerBuilder<T>.() -> U)?>, value: SwingWorkerBuilder<T>.() -> U) { if (prop.get() != null) { throw SwingWorkerException(prop.name.removeSuffix("Lambda") + " already defined!") } @@ -118,38 +118,40 @@ /** * Define the inBackground task. */ - fun inBackground(lambda: SwingWorkerBuilder.() -> T): Unit { + fun inBackground(lambda: SwingWorkerBuilder<T>.() -> T): Unit { setOnce<T>(::inBackgroundLambda, lambda) } /** * Define the whenDone task. */ - fun whenDone(lambda: SwingWorkerBuilder.() -> Unit): Unit { + fun whenDone(lambda: SwingWorkerBuilder<T>.() -> Unit): Unit { setOnce<Unit>(::whenDoneLambda, lambda) } + /** + * Validates we've been properly initialized. + */ + fun validate(): Unit { + if (inBackgroundLambda == null) { + throw SwingWorkerException("inBackground not defined!") + } + } + /* standard overrides for SwingWorker follow */ - override fun doInBackground(): T = inBackgroundLambda?.invoke(this) - - override fun done(): Unit = whenDoneLambda?.invoke(this) + override fun doInBackground(): T = inBackgroundLambda!!.invoke(this) - override fun execute(): Unit { - if (inBackgroundLambda == null) { - throw SwingWorkerException("inBackground not defined!") - } else { - super.execute() - } - } + override fun done(): Unit = whenDoneLambda?.invoke(this) ?: Unit } /** * Provides for an outer swingWorker block to contain the DSL. */ -fun swingWorker<T>(initializer: SwingWorkerBuilder.() -> Unit): Unit { +fun <T> swingWorker(initializer: SwingWorkerBuilder<T>.() -> Unit): Unit { SwingWorkerBuilder<T>().run { initializer() + validate() execute() } }