Revamp UI system, improve UpdateManager

This commit is contained in:
comp500 2019-06-05 20:06:09 +01:00
parent fd87edd6ca
commit 905630cb2a
No known key found for this signature in database
GPG Key ID: 214C822FFEC586B5
7 changed files with 111 additions and 35 deletions

View File

@ -9,6 +9,7 @@ dependencies {
implementation 'commons-cli:commons-cli:1.4' implementation 'commons-cli:commons-cli:1.4'
implementation 'com.moandjiezana.toml:toml4j:0.7.2' implementation 'com.moandjiezana.toml:toml4j:0.7.2'
//testImplementation 'junit:junit:4.12' //testImplementation 'junit:junit:4.12'
// TODO: add GSON, as toml4j depends on it anyway
} }
repositories { repositories {
@ -33,4 +34,13 @@ shadowJar {
exclude(dependency('commons-cli:commons-cli:1.4')) exclude(dependency('commons-cli:commons-cli:1.4'))
exclude(dependency('com.eclipsesource.minimal-json:minimal-json:0.9.5')) exclude(dependency('com.eclipsesource.minimal-json:minimal-json:0.9.5'))
} }
} }
// Used for vscode launch.json
task copyJar(type: Copy) {
from shadowJar
rename "packwiz-installer-(.*)\\.jar", "packwiz-installer.jar"
into "build/libs/"
}
build.dependsOn copyJar

View File

@ -0,0 +1,13 @@
package link.infra.packwiz.installer;
public class CLIHandler implements IUserInterface {
@Override
public void handleException(Exception e) {
e.printStackTrace();
}
@Override
public void show() {}
}

View File

@ -0,0 +1,16 @@
package link.infra.packwiz.installer;
public interface IUserInterface {
public void show();
public void handleException(Exception e);
public default void handleExceptionAndExit(Exception e) {
handleException(e);
System.exit(1);
};
public default void setTitle(String title) {};
}

View File

@ -9,6 +9,7 @@ import java.awt.GridBagLayout;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JProgressBar; import javax.swing.JProgressBar;
import javax.swing.UIManager; import javax.swing.UIManager;
@ -16,23 +17,19 @@ import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
public class InstallWindow { public class InstallWindow implements IUserInterface {
// TODO: move to seperate file, make usable without GUI
private JFrame frmPackwizlauncher; private JFrame frmPackwizlauncher;
private UpdateManager updateManager = new UpdateManager(); private String title = "Updating modpack...";
/** @Override
* Launch the application. public void show() {
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() { EventQueue.invokeLater(new Runnable() {
public void run() { public void run() {
try { try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
InstallWindow window = new InstallWindow(); InstallWindow.this.initialize();
window.frmPackwizlauncher.setVisible(true); InstallWindow.this.frmPackwizlauncher.setVisible(true);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -40,19 +37,12 @@ public class InstallWindow {
}); });
} }
/**
* Create the application.
*/
public InstallWindow() {
initialize();
}
/** /**
* Initialize the contents of the frame. * Initialize the contents of the frame.
*/ */
private void initialize() { private void initialize() {
frmPackwizlauncher = new JFrame(); frmPackwizlauncher = new JFrame();
frmPackwizlauncher.setTitle("Updating modpack..."); frmPackwizlauncher.setTitle(title);
frmPackwizlauncher.setBounds(100, 100, 493, 95); frmPackwizlauncher.setBounds(100, 100, 493, 95);
frmPackwizlauncher.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frmPackwizlauncher.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmPackwizlauncher.setLocationRelativeTo(null); frmPackwizlauncher.setLocationRelativeTo(null);
@ -85,7 +75,7 @@ public class InstallWindow {
JButton btnCancel = new JButton("Cancel"); JButton btnCancel = new JButton("Cancel");
btnCancel.addActionListener(new ActionListener() { btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) { public void actionPerformed(ActionEvent arg0) {
updateManager.cleanup(); //updateManager.cleanup();
frmPackwizlauncher.dispose(); frmPackwizlauncher.dispose();
} }
}); });
@ -96,4 +86,21 @@ public class InstallWindow {
panel_1.add(btnCancel, gbc_btnCancel); panel_1.add(btnCancel, gbc_btnCancel);
} }
@Override
public void handleException(Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage(), title, JOptionPane.ERROR_MESSAGE);
}
@Override
public void setTitle(String title) {
this.title = title;
if (frmPackwizlauncher != null) {
EventQueue.invokeLater(new Runnable() {
public void run() {
InstallWindow.this.frmPackwizlauncher.setTitle(title);
}
});
}
}
} }

View File

@ -33,12 +33,38 @@ public class Main {
System.exit(1); System.exit(1);
} }
System.out.println("Hello World!"); IUserInterface ui;
if (cmd.hasOption("no-gui")) {
ui = new CLIHandler();
} else {
ui = new InstallWindow();
}
String[] unparsedArgs = cmd.getArgs();
if (unparsedArgs.length > 1) {
ui.handleExceptionAndExit(new RuntimeException("Too many arguments specified!"));
} else if (unparsedArgs.length < 1) {
ui.handleExceptionAndExit(new RuntimeException("URI to install from must be specified!"));
}
String title = cmd.getOptionValue("title");
if (title != null) {
ui.setTitle(title);
}
String side = cmd.getOptionValue("side");
if (side == null) {
side = "client";
}
ui.show();
} }
// Called by packwiz-installer-bootstrap to set up the help command // Called by packwiz-installer-bootstrap to set up the help command
public static void addNonBootstrapOptions(Options options) { public static void addNonBootstrapOptions(Options options) {
//options.addOption("w", "welp", false, "Testing options"); options.addOption("s", "side", true, "Side to install mods from (client/server, defaults to client)"); // TODO: implement
options.addOption(null, "title", true, "Title of the installer window");
} }
// TODO: link these somehow so they're only defined once? // TODO: link these somehow so they're only defined once?
@ -47,8 +73,8 @@ public class Main {
options.addOption(null, "bootstrap-update-token", true, "Github API Access Token, for private repositories"); options.addOption(null, "bootstrap-update-token", true, "Github API Access Token, for private repositories");
options.addOption(null, "bootstrap-no-update", false, "Don't update packwiz-installer"); options.addOption(null, "bootstrap-no-update", false, "Don't update packwiz-installer");
options.addOption(null, "bootstrap-main-jar", true, "Location of the packwiz-installer JAR file"); options.addOption(null, "bootstrap-main-jar", true, "Location of the packwiz-installer JAR file");
options.addOption("g", "no-gui", false, "Don't display a GUI to show update progress"); options.addOption("g", "no-gui", false, "Don't display a GUI to show update progress"); // TODO: implement
options.addOption("h", "help", false, "Display this message"); options.addOption("h", "help", false, "Display this message"); // Implemented in packwiz-installer-bootstrap!
} }
} }

View File

@ -1,7 +1,21 @@
package link.infra.packwiz.installer; package link.infra.packwiz.installer;
import java.net.URI;
public class UpdateManager { public class UpdateManager {
Thread updateThread = new Thread(new UpdateThread());
public final Options opts;
public final IUserInterface ui;
public static class Options {
public URI downloadURI;
public String manifestFile = "packwiz.json";
}
public UpdateManager(Options opts, IUserInterface ui) {
this.opts = opts;
this.ui = ui;
}
public void cleanup() { public void cleanup() {

View File

@ -1,10 +0,0 @@
package link.infra.packwiz.installer;
public class UpdateThread implements Runnable {
@Override
public void run() {
}
}