From f5b22f37a4d44feaf32a948b183f3b9975cc8f00 Mon Sep 17 00:00:00 2001 From: comp500 Date: Sun, 13 Dec 2020 16:12:44 +0000 Subject: [PATCH] Refactor GUI code, remove bad SwingWorker junk --- .../infra/packwiz/installer/DownloadTask.kt | 4 +- .../link/infra/packwiz/installer/Main.kt | 21 ++-- .../infra/packwiz/installer/UpdateManager.kt | 28 +++--- .../metadata/hash/HashingSourceHasher.kt | 2 +- .../request/handlers/RequestHandlerZip.kt | 2 +- .../packwiz/installer/ui/IUserInterface.kt | 14 +-- .../packwiz/installer/ui/InputStateHandler.kt | 21 ---- .../ui/SwingWorkerButWithPublicPublish.kt | 13 --- .../installer/ui/{ => cli}/CLIHandler.kt | 19 ++-- .../ui/{ => data}/ExceptionDetails.kt | 2 +- .../installer/ui/{ => data}/IOptionDetails.kt | 2 +- .../ui/{ => data}/InstallProgress.kt | 2 +- .../ui/{ => gui}/ExceptionListWindow.kt | 6 +- .../installer/ui/{ => gui}/InstallWindow.kt | 98 +++++++------------ .../ui/{ => gui}/OptionTempHandler.kt | 4 +- .../ui/{ => gui}/OptionsSelectWindow.kt | 5 +- 16 files changed, 94 insertions(+), 149 deletions(-) delete mode 100644 src/main/kotlin/link/infra/packwiz/installer/ui/InputStateHandler.kt delete mode 100644 src/main/kotlin/link/infra/packwiz/installer/ui/SwingWorkerButWithPublicPublish.kt rename src/main/kotlin/link/infra/packwiz/installer/ui/{ => cli}/CLIHandler.kt (75%) rename src/main/kotlin/link/infra/packwiz/installer/ui/{ => data}/ExceptionDetails.kt (63%) rename src/main/kotlin/link/infra/packwiz/installer/ui/{ => data}/IOptionDetails.kt (69%) rename src/main/kotlin/link/infra/packwiz/installer/ui/{ => data}/InstallProgress.kt (87%) rename src/main/kotlin/link/infra/packwiz/installer/ui/{ => gui}/ExceptionListWindow.kt (94%) rename src/main/kotlin/link/infra/packwiz/installer/ui/{ => gui}/InstallWindow.kt (70%) rename src/main/kotlin/link/infra/packwiz/installer/ui/{ => gui}/OptionTempHandler.kt (78%) rename src/main/kotlin/link/infra/packwiz/installer/ui/{ => gui}/OptionsSelectWindow.kt (96%) diff --git a/src/main/kotlin/link/infra/packwiz/installer/DownloadTask.kt b/src/main/kotlin/link/infra/packwiz/installer/DownloadTask.kt index b158d4a..23c2408 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/DownloadTask.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/DownloadTask.kt @@ -6,8 +6,8 @@ import link.infra.packwiz.installer.metadata.SpaceSafeURI import link.infra.packwiz.installer.metadata.hash.Hash import link.infra.packwiz.installer.metadata.hash.HashUtils.getHash import link.infra.packwiz.installer.metadata.hash.HashUtils.getHasher -import link.infra.packwiz.installer.ui.ExceptionDetails -import link.infra.packwiz.installer.ui.IOptionDetails +import link.infra.packwiz.installer.ui.data.ExceptionDetails +import link.infra.packwiz.installer.ui.data.IOptionDetails import okio.Buffer import okio.HashingSink import okio.buffer diff --git a/src/main/kotlin/link/infra/packwiz/installer/Main.kt b/src/main/kotlin/link/infra/packwiz/installer/Main.kt index 5a89ea4..c50dc71 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/Main.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/Main.kt @@ -3,9 +3,8 @@ package link.infra.packwiz.installer import link.infra.packwiz.installer.metadata.SpaceSafeURI -import link.infra.packwiz.installer.ui.CLIHandler -import link.infra.packwiz.installer.ui.InputStateHandler -import link.infra.packwiz.installer.ui.InstallWindow +import link.infra.packwiz.installer.ui.cli.CLIHandler +import link.infra.packwiz.installer.ui.gui.InstallWindow import org.apache.commons.cli.DefaultParser import org.apache.commons.cli.Options import org.apache.commons.cli.ParseException @@ -19,7 +18,7 @@ import kotlin.system.exitProcess @Suppress("unused") class Main(args: Array) { // Don't attempt to start a GUI if we are headless - var guiEnabled = !GraphicsEnvironment.isHeadless() + private var guiEnabled = !GraphicsEnvironment.isHeadless() private fun startup(args: Array) { val options = Options() @@ -59,8 +58,7 @@ class Main(args: Array) { cmd.getOptionValue("title")?.also(ui::setTitle) - val inputStateHandler = InputStateHandler() - ui.show(inputStateHandler) + ui.show() val uOptions = UpdateManager.Options().apply { side = cmd.getOptionValue("side")?.let((UpdateManager.Options.Side)::from) ?: side @@ -76,18 +74,13 @@ class Main(args: Array) { } // Start update process! - // TODO: start in SwingWorker? try { - ui.executeManager { - try { - UpdateManager(uOptions, ui, inputStateHandler) - } catch (e: Exception) { // TODO: better error message? - ui.handleExceptionAndExit(e) - } - } + UpdateManager(uOptions, ui) } catch (e: Exception) { // TODO: better error message? ui.handleExceptionAndExit(e) } + println("Finished successfully!") + ui.dispose() } companion object { diff --git a/src/main/kotlin/link/infra/packwiz/installer/UpdateManager.kt b/src/main/kotlin/link/infra/packwiz/installer/UpdateManager.kt index 8c11da2..d3c8245 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/UpdateManager.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/UpdateManager.kt @@ -18,8 +18,7 @@ import link.infra.packwiz.installer.request.HandlerManager.getNewLoc import link.infra.packwiz.installer.ui.IUserInterface import link.infra.packwiz.installer.ui.IUserInterface.CancellationResult import link.infra.packwiz.installer.ui.IUserInterface.ExceptionListResult -import link.infra.packwiz.installer.ui.InputStateHandler -import link.infra.packwiz.installer.ui.InstallProgress +import link.infra.packwiz.installer.ui.data.InstallProgress import okio.buffer import java.io.FileNotFoundException import java.io.FileReader @@ -34,7 +33,7 @@ import java.util.concurrent.ExecutorCompletionService import java.util.concurrent.Executors import kotlin.system.exitProcess -class UpdateManager internal constructor(private val opts: Options, val ui: IUserInterface, private val stateHandler: InputStateHandler) { +class UpdateManager internal constructor(private val opts: Options, val ui: IUserInterface) { private var cancelled = false private var cancelledStartGame = false private var errorsOccurred = false @@ -119,7 +118,7 @@ class UpdateManager internal constructor(private val opts: Options, val ui: IUse return } - if (stateHandler.cancelButton) { + if (ui.cancelButtonPressed) { showCancellationDialog() handleCancellation() } @@ -142,7 +141,7 @@ class UpdateManager internal constructor(private val opts: Options, val ui: IUse } } - if (stateHandler.cancelButton) { + if (ui.cancelButtonPressed) { showCancellationDialog() handleCancellation() } @@ -178,14 +177,14 @@ class UpdateManager internal constructor(private val opts: Options, val ui: IUse if (manifest.packFileHash?.let { packFileSource.hashIsEqual(it) } == true && invalidatedUris.isEmpty()) { println("Modpack is already up to date!") // todo: --force? - if (!stateHandler.optionsButton) { + if (!ui.optionsButtonPressed) { return } } println("Modpack name: " + pf.name) - if (stateHandler.cancelButton) { + if (ui.cancelButtonPressed) { showCancellationDialog() handleCancellation() } @@ -234,7 +233,7 @@ class UpdateManager internal constructor(private val opts: Options, val ui: IUse private fun processIndex(indexUri: SpaceSafeURI, indexHash: Hash, hashFormat: String, manifest: ManifestFile, invalidatedUris: List) { if (manifest.indexFileHash == indexHash && invalidatedUris.isEmpty()) { println("Modpack files are already up to date!") - if (!stateHandler.optionsButton) { + if (!ui.optionsButtonPressed) { return } } @@ -258,7 +257,7 @@ class UpdateManager internal constructor(private val opts: Options, val ui: IUse ui.handleExceptionAndExit(RuntimeException("Your index hash is invalid! Please run packwiz refresh on the pack again")) return } - if (stateHandler.cancelButton) { + if (ui.cancelButtonPressed) { showCancellationDialog() return } @@ -295,7 +294,7 @@ class UpdateManager internal constructor(private val opts: Options, val ui: IUse } } - if (stateHandler.cancelButton) { + if (ui.cancelButtonPressed) { showCancellationDialog() return } @@ -326,7 +325,7 @@ class UpdateManager internal constructor(private val opts: Options, val ui: IUse f.updateFromCache(file) } - if (stateHandler.cancelButton) { + if (ui.cancelButtonPressed) { showCancellationDialog() return } @@ -360,7 +359,7 @@ class UpdateManager internal constructor(private val opts: Options, val ui: IUse } } - if (stateHandler.cancelButton) { + if (ui.cancelButtonPressed) { showCancellationDialog() return } @@ -369,7 +368,7 @@ class UpdateManager internal constructor(private val opts: Options, val ui: IUse val nonFailedFirstTasks = tasks.filter { t -> !t.failed() }.toList() val optionTasks = nonFailedFirstTasks.filter(DownloadTask::correctSide).filter(DownloadTask::isOptional).toList() // If options changed, present all options again - if (stateHandler.optionsButton || optionTasks.any(DownloadTask::isNewOptional)) { + if (ui.optionsButtonPressed || optionTasks.any(DownloadTask::isNewOptional)) { // new ArrayList is required so it's an IOptionDetails rather than a DownloadTask list val cancelledResult = ui.showOptions(ArrayList(optionTasks)) try { @@ -433,7 +432,7 @@ class UpdateManager internal constructor(private val opts: Options, val ui: IUse } ui.submitProgress(InstallProgress(progress, i + 1, tasks.size)) - if (stateHandler.cancelButton) { // Stop all tasks, don't launch the game (it's in an invalid state!) + if (ui.cancelButtonPressed) { // Stop all tasks, don't launch the game (it's in an invalid state!) threadPool.shutdown() cancelled = true return @@ -483,6 +482,7 @@ class UpdateManager internal constructor(private val opts: Options, val ui: IUse } } + // TODO: move to UI? private fun handleCancellation() { if (cancelled) { println("Update cancelled by user!") diff --git a/src/main/kotlin/link/infra/packwiz/installer/metadata/hash/HashingSourceHasher.kt b/src/main/kotlin/link/infra/packwiz/installer/metadata/hash/HashingSourceHasher.kt index 15f79e4..8ee9860 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/metadata/hash/HashingSourceHasher.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/metadata/hash/HashingSourceHasher.kt @@ -5,7 +5,7 @@ import okio.Source class HashingSourceHasher internal constructor(private val type: String) : IHasher { // i love naming things - private inner class HashingSourceGeneralHashingSource internal constructor(val delegateHashing: HashingSource) : GeneralHashingSource(delegateHashing) { + private inner class HashingSourceGeneralHashingSource(val delegateHashing: HashingSource) : GeneralHashingSource(delegateHashing) { override val hash: Hash by lazy(LazyThreadSafetyMode.NONE) { HashingSourceHash(delegateHashing.hash.hex()) } diff --git a/src/main/kotlin/link/infra/packwiz/installer/request/handlers/RequestHandlerZip.kt b/src/main/kotlin/link/infra/packwiz/installer/request/handlers/RequestHandlerZip.kt index 624437e..ec30c5a 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/request/handlers/RequestHandlerZip.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/request/handlers/RequestHandlerZip.kt @@ -25,7 +25,7 @@ abstract class RequestHandlerZip(private val modeHasFolder: Boolean) : RequestHa } } - private inner class ZipReader internal constructor(zip: Source) { + private inner class ZipReader(zip: Source) { private val zis = ZipInputStream(zip.buffer().inputStream()) private val readFiles: MutableMap = HashMap() // Write lock implies access to ZipInputStream - only 1 thread must read at a time! diff --git a/src/main/kotlin/link/infra/packwiz/installer/ui/IUserInterface.kt b/src/main/kotlin/link/infra/packwiz/installer/ui/IUserInterface.kt index 3744436..8909795 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/ui/IUserInterface.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/ui/IUserInterface.kt @@ -1,30 +1,29 @@ package link.infra.packwiz.installer.ui +import link.infra.packwiz.installer.ui.data.ExceptionDetails +import link.infra.packwiz.installer.ui.data.IOptionDetails +import link.infra.packwiz.installer.ui.data.InstallProgress import java.util.concurrent.CompletableFuture import java.util.concurrent.Future import kotlin.system.exitProcess interface IUserInterface { - fun show(handler: InputStateHandler) + fun show() + fun dispose() fun handleException(e: Exception) - @JvmDefault fun handleExceptionAndExit(e: Exception) { handleException(e) exitProcess(1) } - @JvmDefault fun setTitle(title: String) {} fun submitProgress(progress: InstallProgress) - fun executeManager(task: () -> Unit) // Return true if the installation was cancelled! fun showOptions(options: List): Future fun showExceptions(exceptions: List, numTotal: Int, allowsIgnore: Boolean): Future - @JvmDefault fun disableOptionsButton() {} - @JvmDefault fun showCancellationDialog(): Future { return CompletableFuture().apply { complete(CancellationResult.QUIT) @@ -38,4 +37,7 @@ interface IUserInterface { enum class CancellationResult { QUIT, CONTINUE } + + var optionsButtonPressed: Boolean + var cancelButtonPressed: Boolean } \ No newline at end of file diff --git a/src/main/kotlin/link/infra/packwiz/installer/ui/InputStateHandler.kt b/src/main/kotlin/link/infra/packwiz/installer/ui/InputStateHandler.kt deleted file mode 100644 index 75a517e..0000000 --- a/src/main/kotlin/link/infra/packwiz/installer/ui/InputStateHandler.kt +++ /dev/null @@ -1,21 +0,0 @@ -package link.infra.packwiz.installer.ui - -class InputStateHandler { - // TODO: convert to coroutines/locks? - @get:Synchronized - var optionsButton = false - private set - @get:Synchronized - var cancelButton = false - private set - - @Synchronized - fun pressCancelButton() { - cancelButton = true - } - - @Synchronized - fun pressOptionsButton() { - optionsButton = true - } -} \ No newline at end of file diff --git a/src/main/kotlin/link/infra/packwiz/installer/ui/SwingWorkerButWithPublicPublish.kt b/src/main/kotlin/link/infra/packwiz/installer/ui/SwingWorkerButWithPublicPublish.kt deleted file mode 100644 index fc67839..0000000 --- a/src/main/kotlin/link/infra/packwiz/installer/ui/SwingWorkerButWithPublicPublish.kt +++ /dev/null @@ -1,13 +0,0 @@ -package link.infra.packwiz.installer.ui - -import javax.swing.SwingWorker - -// Q: AAA WHAT HAVE YOU DONE THIS IS DISGUSTING -// A: it just makes things easier, so i can easily have one interface for CLI/GUI -// if someone has a better way to do this please PR it -abstract class SwingWorkerButWithPublicPublish : SwingWorker() { - @SafeVarargs - fun publishPublic(vararg chunks: V) { - publish(*chunks) - } -} \ No newline at end of file diff --git a/src/main/kotlin/link/infra/packwiz/installer/ui/CLIHandler.kt b/src/main/kotlin/link/infra/packwiz/installer/ui/cli/CLIHandler.kt similarity index 75% rename from src/main/kotlin/link/infra/packwiz/installer/ui/CLIHandler.kt rename to src/main/kotlin/link/infra/packwiz/installer/ui/cli/CLIHandler.kt index 6fbf3d9..0885d89 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/ui/CLIHandler.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/ui/cli/CLIHandler.kt @@ -1,16 +1,26 @@ -package link.infra.packwiz.installer.ui +package link.infra.packwiz.installer.ui.cli +import link.infra.packwiz.installer.ui.IUserInterface import link.infra.packwiz.installer.ui.IUserInterface.ExceptionListResult +import link.infra.packwiz.installer.ui.data.ExceptionDetails +import link.infra.packwiz.installer.ui.data.IOptionDetails +import link.infra.packwiz.installer.ui.data.InstallProgress import java.util.concurrent.CompletableFuture import java.util.concurrent.Future import kotlin.system.exitProcess class CLIHandler : IUserInterface { + @Volatile + override var optionsButtonPressed = false + @Volatile + override var cancelButtonPressed = false + override fun handleException(e: Exception) { e.printStackTrace() } - override fun show(handler: InputStateHandler) {} + override fun show() {} + override fun dispose() {} override fun submitProgress(progress: InstallProgress) { val sb = StringBuilder() if (progress.hasProgress) { @@ -24,11 +34,6 @@ class CLIHandler : IUserInterface { println(sb.toString()) } - override fun executeManager(task: () -> Unit) { - task() - println("Finished successfully!") - } - override fun showOptions(options: List): Future { for (opt in options) { opt.optionValue = true diff --git a/src/main/kotlin/link/infra/packwiz/installer/ui/ExceptionDetails.kt b/src/main/kotlin/link/infra/packwiz/installer/ui/data/ExceptionDetails.kt similarity index 63% rename from src/main/kotlin/link/infra/packwiz/installer/ui/ExceptionDetails.kt rename to src/main/kotlin/link/infra/packwiz/installer/ui/data/ExceptionDetails.kt index f4fc81c..6908048 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/ui/ExceptionDetails.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/ui/data/ExceptionDetails.kt @@ -1,4 +1,4 @@ -package link.infra.packwiz.installer.ui +package link.infra.packwiz.installer.ui.data data class ExceptionDetails( val name: String, diff --git a/src/main/kotlin/link/infra/packwiz/installer/ui/IOptionDetails.kt b/src/main/kotlin/link/infra/packwiz/installer/ui/data/IOptionDetails.kt similarity index 69% rename from src/main/kotlin/link/infra/packwiz/installer/ui/IOptionDetails.kt rename to src/main/kotlin/link/infra/packwiz/installer/ui/data/IOptionDetails.kt index 3665b6b..5963a9e 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/ui/IOptionDetails.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/ui/data/IOptionDetails.kt @@ -1,4 +1,4 @@ -package link.infra.packwiz.installer.ui +package link.infra.packwiz.installer.ui.data interface IOptionDetails { val name: String diff --git a/src/main/kotlin/link/infra/packwiz/installer/ui/InstallProgress.kt b/src/main/kotlin/link/infra/packwiz/installer/ui/data/InstallProgress.kt similarity index 87% rename from src/main/kotlin/link/infra/packwiz/installer/ui/InstallProgress.kt rename to src/main/kotlin/link/infra/packwiz/installer/ui/data/InstallProgress.kt index 1817c07..8d7d99e 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/ui/InstallProgress.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/ui/data/InstallProgress.kt @@ -1,4 +1,4 @@ -package link.infra.packwiz.installer.ui +package link.infra.packwiz.installer.ui.data data class InstallProgress( val message: String, diff --git a/src/main/kotlin/link/infra/packwiz/installer/ui/ExceptionListWindow.kt b/src/main/kotlin/link/infra/packwiz/installer/ui/gui/ExceptionListWindow.kt similarity index 94% rename from src/main/kotlin/link/infra/packwiz/installer/ui/ExceptionListWindow.kt rename to src/main/kotlin/link/infra/packwiz/installer/ui/gui/ExceptionListWindow.kt index d4a0cd3..dc20852 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/ui/ExceptionListWindow.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/ui/gui/ExceptionListWindow.kt @@ -1,5 +1,7 @@ -package link.infra.packwiz.installer.ui +package link.infra.packwiz.installer.ui.gui +import link.infra.packwiz.installer.ui.IUserInterface +import link.infra.packwiz.installer.ui.data.ExceptionDetails import java.awt.BorderLayout import java.awt.Desktop import java.awt.event.WindowAdapter @@ -16,7 +18,7 @@ import javax.swing.border.EmptyBorder class ExceptionListWindow(eList: List, future: CompletableFuture, numTotal: Int, allowsIgnore: Boolean, parentWindow: JFrame?) : JDialog(parentWindow, "Failed file downloads", true) { private val lblExceptionStacktrace: JTextArea - private class ExceptionListModel internal constructor(private val details: List) : AbstractListModel() { + private class ExceptionListModel(private val details: List) : AbstractListModel() { override fun getSize() = details.size override fun getElementAt(index: Int) = details[index].name fun getExceptionAt(index: Int) = details[index].exception diff --git a/src/main/kotlin/link/infra/packwiz/installer/ui/InstallWindow.kt b/src/main/kotlin/link/infra/packwiz/installer/ui/gui/InstallWindow.kt similarity index 70% rename from src/main/kotlin/link/infra/packwiz/installer/ui/InstallWindow.kt rename to src/main/kotlin/link/infra/packwiz/installer/ui/gui/InstallWindow.kt index fb3abd7..69eddac 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/ui/InstallWindow.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/ui/gui/InstallWindow.kt @@ -1,10 +1,13 @@ -package link.infra.packwiz.installer.ui +package link.infra.packwiz.installer.ui.gui +import link.infra.packwiz.installer.ui.IUserInterface import link.infra.packwiz.installer.ui.IUserInterface.ExceptionListResult +import link.infra.packwiz.installer.ui.data.ExceptionDetails +import link.infra.packwiz.installer.ui.data.IOptionDetails +import link.infra.packwiz.installer.ui.data.InstallProgress import java.awt.* import java.util.concurrent.CompletableFuture import java.util.concurrent.Future -import java.util.concurrent.atomic.AtomicBoolean import javax.swing.* import javax.swing.border.EmptyBorder import kotlin.system.exitProcess @@ -15,10 +18,12 @@ class InstallWindow : IUserInterface { private lateinit var progressBar: JProgressBar private lateinit var btnOptions: JButton - private var inputStateHandler: InputStateHandler? = null + @Volatile + override var optionsButtonPressed = false + @Volatile + override var cancelButtonPressed = false + private var title = "Updating modpack..." - private var worker: SwingWorkerButWithPublicPublish? = null - private val aboutToCrash = AtomicBoolean() // TODO: separate JFrame junk from IUserInterface junk? @@ -55,7 +60,7 @@ class InstallWindow : IUserInterface { addActionListener { text = "Loading..." isEnabled = false - inputStateHandler?.pressOptionsButton() + optionsButtonPressed = true } } add(btnOptions, GridBagConstraints().apply { @@ -66,7 +71,7 @@ class InstallWindow : IUserInterface { add(JButton("Cancel").apply { addActionListener { isEnabled = false - inputStateHandler?.pressCancelButton() + cancelButtonPressed = true } }, GridBagConstraints().apply { gridx = 0 @@ -77,10 +82,10 @@ class InstallWindow : IUserInterface { } } - override fun show(handler: InputStateHandler) { - inputStateHandler = handler + override fun show() { EventQueue.invokeLater { try { + // TODO: shouldn't we do this before everything else? UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()) frmPackwizlauncher.isVisible = true } catch (e: Exception) { @@ -89,9 +94,15 @@ class InstallWindow : IUserInterface { } } + override fun dispose() { + EventQueue.invokeAndWait { + frmPackwizlauncher.dispose() + } + } + override fun handleException(e: Exception) { e.printStackTrace() - EventQueue.invokeLater { + EventQueue.invokeAndWait { JOptionPane.showMessageDialog(null, "An error occurred: \n" + e.javaClass.canonicalName + ": " + e.message, title, JOptionPane.ERROR_MESSAGE) @@ -100,27 +111,17 @@ class InstallWindow : IUserInterface { override fun handleExceptionAndExit(e: Exception) { e.printStackTrace() - // TODO: Fix this mess - // Used to prevent the done() handler of SwingWorker executing if the invokeLater hasn't happened yet - aboutToCrash.set(true) - EventQueue.invokeLater { + EventQueue.invokeAndWait { JOptionPane.showMessageDialog(null, "A fatal error occurred: \n" + e.javaClass.canonicalName + ": " + e.message, title, JOptionPane.ERROR_MESSAGE) exitProcess(1) } - // Pause forever, so it blocks while we wait for System.exit to take effect - try { - Thread.currentThread().join() - } catch (ex: InterruptedException) { // no u - } } override fun setTitle(title: String) { this.title = title - frmPackwizlauncher.let { frame -> - EventQueue.invokeLater { frame.title = title } - } + EventQueue.invokeLater { frmPackwizlauncher.title = title } } override fun submitProgress(progress: InstallProgress) { @@ -135,45 +136,16 @@ class InstallWindow : IUserInterface { sb.append(progress.message) // TODO: better logging library? println(sb.toString()) - worker?.publishPublic(progress) - } - - override fun executeManager(task: Function0) { EventQueue.invokeLater { - // TODO: rewrite this stupidity to use channels??!!! - worker = object : SwingWorkerButWithPublicPublish() { - override fun doInBackground() { - task.invoke() - } - - override fun process(chunks: List) { - // Only process last chunk - if (chunks.isNotEmpty()) { - val (message, hasProgress, progress, progressTotal) = chunks[chunks.size - 1] - if (hasProgress) { - progressBar.isIndeterminate = false - progressBar.value = progress - progressBar.maximum = progressTotal - } else { - progressBar.isIndeterminate = true - progressBar.value = 0 - } - lblProgresslabel.text = message - } - } - - override fun done() { - if (aboutToCrash.get()) { - return - } - // TODO: a better way to do this? - frmPackwizlauncher.dispose() - println("Finished successfully!") - exitProcess(0) - } - }.also { - it.execute() + if (progress.hasProgress) { + progressBar.isIndeterminate = false + progressBar.value = progress.progress + progressBar.maximum = progress.progressTotal + } else { + progressBar.isIndeterminate = true + progressBar.value = 0 } + lblProgresslabel.text = progress.message } } @@ -207,9 +179,11 @@ class InstallWindow : IUserInterface { } override fun disableOptionsButton() { - btnOptions.apply { - text = "No optional mods" - isEnabled = false + EventQueue.invokeLater { + btnOptions.apply { + text = "No optional mods" + isEnabled = false + } } } diff --git a/src/main/kotlin/link/infra/packwiz/installer/ui/OptionTempHandler.kt b/src/main/kotlin/link/infra/packwiz/installer/ui/gui/OptionTempHandler.kt similarity index 78% rename from src/main/kotlin/link/infra/packwiz/installer/ui/OptionTempHandler.kt rename to src/main/kotlin/link/infra/packwiz/installer/ui/gui/OptionTempHandler.kt index 9c29304..840d745 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/ui/OptionTempHandler.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/ui/gui/OptionTempHandler.kt @@ -1,4 +1,6 @@ -package link.infra.packwiz.installer.ui +package link.infra.packwiz.installer.ui.gui + +import link.infra.packwiz.installer.ui.data.IOptionDetails // Serves as a proxy for IOptionDetails, so that setOptionValue isn't called until OK is clicked internal class OptionTempHandler(private val opt: IOptionDetails) : IOptionDetails { diff --git a/src/main/kotlin/link/infra/packwiz/installer/ui/OptionsSelectWindow.kt b/src/main/kotlin/link/infra/packwiz/installer/ui/gui/OptionsSelectWindow.kt similarity index 96% rename from src/main/kotlin/link/infra/packwiz/installer/ui/OptionsSelectWindow.kt rename to src/main/kotlin/link/infra/packwiz/installer/ui/gui/OptionsSelectWindow.kt index 5d24ee1..b01fdb3 100644 --- a/src/main/kotlin/link/infra/packwiz/installer/ui/OptionsSelectWindow.kt +++ b/src/main/kotlin/link/infra/packwiz/installer/ui/gui/OptionsSelectWindow.kt @@ -1,5 +1,6 @@ -package link.infra.packwiz.installer.ui +package link.infra.packwiz.installer.ui.gui +import link.infra.packwiz.installer.ui.data.IOptionDetails import java.awt.BorderLayout import java.awt.FlowLayout import java.awt.event.ActionEvent @@ -18,7 +19,7 @@ class OptionsSelectWindow internal constructor(optList: List, fu private val tableModel: OptionTableModel private val future: CompletableFuture - private class OptionTableModel internal constructor(givenOpts: List) : TableModel { + private class OptionTableModel(givenOpts: List) : TableModel { private val opts: List init {