Fix Swing multithreading issue, clean up slightly

This commit is contained in:
comp500 2020-09-29 02:14:56 +01:00
parent ecc6f0440a
commit b3370739a5
2 changed files with 74 additions and 64 deletions

View File

@ -18,6 +18,9 @@ import kotlin.system.exitProcess
@Suppress("unused") @Suppress("unused")
class Main(args: Array<String>) { class Main(args: Array<String>) {
// Don't attempt to start a GUI if we are headless
var guiEnabled = !GraphicsEnvironment.isHeadless()
private fun startup(args: Array<String>) { private fun startup(args: Array<String>) {
val options = Options() val options = Options()
addNonBootstrapOptions(options) addNonBootstrapOptions(options)
@ -28,19 +31,24 @@ class Main(args: Array<String>) {
parser.parse(options, args) parser.parse(options, args)
} catch (e: ParseException) { } catch (e: ParseException) {
e.printStackTrace() e.printStackTrace()
if (guiEnabled) {
EventQueue.invokeAndWait {
try { try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()) UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
} catch (e1: Exception) { } catch (ignored: Exception) {
// Ignore the exceptions, just continue using the ugly L&F // Ignore the exceptions, just continue using the ugly L&F
} }
JOptionPane.showMessageDialog(null, e.message, "packwiz-installer", JOptionPane.ERROR_MESSAGE) JOptionPane.showMessageDialog(null, e.message, "packwiz-installer", JOptionPane.ERROR_MESSAGE)
}
}
exitProcess(1) exitProcess(1)
} }
// if "headless", GUI creation will fail anyway! if (guiEnabled && cmd.hasOption("no-gui")) {
val ui = if (cmd.hasOption("no-gui") || GraphicsEnvironment.isHeadless()) { guiEnabled = false
CLIHandler() }
} else InstallWindow()
val ui = if (guiEnabled) InstallWindow() else CLIHandler()
val unparsedArgs = cmd.args val unparsedArgs = cmd.args
if (unparsedArgs.size > 1) { if (unparsedArgs.size > 1) {
@ -49,15 +57,13 @@ class Main(args: Array<String>) {
ui.handleExceptionAndExit(RuntimeException("URI to install from must be specified!")) ui.handleExceptionAndExit(RuntimeException("URI to install from must be specified!"))
} }
cmd.getOptionValue("title")?.also { cmd.getOptionValue("title")?.also(ui::setTitle)
ui.setTitle(it)
}
val inputStateHandler = InputStateHandler() val inputStateHandler = InputStateHandler()
ui.show(inputStateHandler) ui.show(inputStateHandler)
val uOptions = UpdateManager.Options().apply { val uOptions = UpdateManager.Options().apply {
side = cmd.getOptionValue("side")?.let { UpdateManager.Options.Side.from(it) } ?: side side = cmd.getOptionValue("side")?.let((UpdateManager.Options.Side)::from) ?: side
packFolder = cmd.getOptionValue("pack-folder") ?: packFolder packFolder = cmd.getOptionValue("pack-folder") ?: packFolder
manifestFile = cmd.getOptionValue("meta-file") ?: manifestFile manifestFile = cmd.getOptionValue("meta-file") ?: manifestFile
} }
@ -113,12 +119,14 @@ class Main(args: Array<String>) {
startup(args) startup(args)
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()
if (guiEnabled) {
EventQueue.invokeLater { EventQueue.invokeLater {
JOptionPane.showMessageDialog(null, JOptionPane.showMessageDialog(null,
"A fatal error occurred: \n" + e.javaClass.canonicalName + ": " + e.message, "A fatal error occurred: \n" + e.javaClass.canonicalName + ": " + e.message,
"packwiz-installer", JOptionPane.ERROR_MESSAGE) "packwiz-installer", JOptionPane.ERROR_MESSAGE)
exitProcess(1) exitProcess(1)
} }
}
// In case the EventQueue is broken, exit after 1 minute // In case the EventQueue is broken, exit after 1 minute
Thread.sleep(60 * 1000.toLong()) Thread.sleep(60 * 1000.toLong())
exitProcess(1) exitProcess(1)

View File

@ -10,10 +10,10 @@ import javax.swing.border.EmptyBorder
import kotlin.system.exitProcess import kotlin.system.exitProcess
class InstallWindow : IUserInterface { class InstallWindow : IUserInterface {
private val frmPackwizlauncher: JFrame private lateinit var frmPackwizlauncher: JFrame
private val lblProgresslabel: JLabel private lateinit var lblProgresslabel: JLabel
private val progressBar: JProgressBar private lateinit var progressBar: JProgressBar
private val btnOptions: JButton private lateinit var btnOptions: JButton
private var inputStateHandler: InputStateHandler? = null private var inputStateHandler: InputStateHandler? = null
private var title = "Updating modpack..." private var title = "Updating modpack..."
@ -23,6 +23,7 @@ class InstallWindow : IUserInterface {
// TODO: separate JFrame junk from IUserInterface junk? // TODO: separate JFrame junk from IUserInterface junk?
init { init {
EventQueue.invokeAndWait {
frmPackwizlauncher = JFrame().apply { frmPackwizlauncher = JFrame().apply {
title = this@InstallWindow.title title = this@InstallWindow.title
setBounds(100, 100, 493, 95) setBounds(100, 100, 493, 95)
@ -74,6 +75,7 @@ class InstallWindow : IUserInterface {
}, BorderLayout.EAST) }, BorderLayout.EAST)
} }
} }
}
override fun show(handler: InputStateHandler) { override fun show(handler: InputStateHandler) {
inputStateHandler = handler inputStateHandler = handler