mirror of
https://github.com/packwiz/packwiz-installer.git
synced 2025-10-25 18:24:32 +02:00
Refactor handlers into ui package
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package link.infra.packwiz.installer.ui;
|
||||
|
||||
public class CLIHandler implements IUserInterface {
|
||||
|
||||
@Override
|
||||
public void handleException(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show() {}
|
||||
|
||||
@Override
|
||||
public void submitProgress(InstallProgress progress) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (progress.hasProgress) {
|
||||
sb.append('(');
|
||||
sb.append(progress.progress);
|
||||
sb.append('/');
|
||||
sb.append(progress.progressTotal);
|
||||
sb.append(") ");
|
||||
}
|
||||
sb.append(progress.message);
|
||||
System.out.println(sb.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void executeManager(Runnable task) {
|
||||
task.run();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package link.infra.packwiz.installer.ui;
|
||||
|
||||
public interface IUserInterface {
|
||||
|
||||
public void show();
|
||||
|
||||
public void handleException(Exception e);
|
||||
|
||||
/**
|
||||
* This might not exit straight away, return after calling this!
|
||||
*/
|
||||
public default void handleExceptionAndExit(Exception e) {
|
||||
handleException(e);
|
||||
System.exit(1);
|
||||
};
|
||||
|
||||
public default void setTitle(String title) {};
|
||||
|
||||
public void submitProgress(InstallProgress progress);
|
||||
|
||||
public void executeManager(Runnable task);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package link.infra.packwiz.installer.ui;
|
||||
|
||||
public class InstallProgress {
|
||||
public final String message;
|
||||
public final boolean hasProgress;
|
||||
public final int progress;
|
||||
public final int progressTotal;
|
||||
|
||||
public InstallProgress(String message) {
|
||||
this.message = message;
|
||||
hasProgress = false;
|
||||
progress = 0;
|
||||
progressTotal = 0;
|
||||
}
|
||||
|
||||
public InstallProgress(String message, int progress, int progressTotal) {
|
||||
this.message = message;
|
||||
hasProgress = true;
|
||||
this.progress = progress;
|
||||
this.progressTotal = progressTotal;
|
||||
}
|
||||
}
|
||||
187
src/main/java/link/infra/packwiz/installer/ui/InstallWindow.java
Normal file
187
src/main/java/link/infra/packwiz/installer/ui/InstallWindow.java
Normal file
@@ -0,0 +1,187 @@
|
||||
package link.infra.packwiz.installer.ui;
|
||||
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Component;
|
||||
import java.awt.EventQueue;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JProgressBar;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.border.EmptyBorder;
|
||||
|
||||
public class InstallWindow implements IUserInterface {
|
||||
|
||||
private JFrame frmPackwizlauncher;
|
||||
private JLabel lblProgresslabel;
|
||||
private JProgressBar progressBar;
|
||||
|
||||
private String title = "Updating modpack...";
|
||||
private SwingWorkerButWithPublicPublish<Void, InstallProgress> worker;
|
||||
private AtomicBoolean aboutToCrash = new AtomicBoolean();
|
||||
|
||||
@Override
|
||||
public void show() {
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
InstallWindow.this.initialize();
|
||||
InstallWindow.this.frmPackwizlauncher.setVisible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the contents of the frame.
|
||||
*/
|
||||
private void initialize() {
|
||||
frmPackwizlauncher = new JFrame();
|
||||
frmPackwizlauncher.setTitle(title);
|
||||
frmPackwizlauncher.setBounds(100, 100, 493, 95);
|
||||
frmPackwizlauncher.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
frmPackwizlauncher.setLocationRelativeTo(null);
|
||||
|
||||
JPanel panel = new JPanel();
|
||||
panel.setBorder(new EmptyBorder(10, 10, 10, 10));
|
||||
frmPackwizlauncher.getContentPane().add(panel, BorderLayout.CENTER);
|
||||
panel.setLayout(new BorderLayout(0, 0));
|
||||
|
||||
progressBar = new JProgressBar();
|
||||
progressBar.setIndeterminate(true);
|
||||
panel.add(progressBar, BorderLayout.CENTER);
|
||||
|
||||
lblProgresslabel = new JLabel("Loading...");
|
||||
panel.add(lblProgresslabel, BorderLayout.SOUTH);
|
||||
|
||||
JPanel panel_1 = new JPanel();
|
||||
panel_1.setBorder(new EmptyBorder(0, 5, 0, 5));
|
||||
frmPackwizlauncher.getContentPane().add(panel_1, BorderLayout.EAST);
|
||||
GridBagLayout gbl_panel_1 = new GridBagLayout();
|
||||
panel_1.setLayout(gbl_panel_1);
|
||||
|
||||
JButton btnOptions = new JButton("Options...");
|
||||
btnOptions.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
GridBagConstraints gbc_btnOptions = new GridBagConstraints();
|
||||
gbc_btnOptions.gridx = 0;
|
||||
gbc_btnOptions.gridy = 0;
|
||||
panel_1.add(btnOptions, gbc_btnOptions);
|
||||
|
||||
JButton btnCancel = new JButton("Cancel");
|
||||
btnCancel.addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent arg0) {
|
||||
if (worker != null) {
|
||||
worker.cancel(true);
|
||||
}
|
||||
frmPackwizlauncher.dispose();
|
||||
// TODO: show window to ask user what to do
|
||||
System.out.println("Update process cancelled by user!");
|
||||
System.exit(1);
|
||||
}
|
||||
});
|
||||
btnCancel.setAlignmentX(Component.CENTER_ALIGNMENT);
|
||||
GridBagConstraints gbc_btnCancel = new GridBagConstraints();
|
||||
gbc_btnCancel.gridx = 0;
|
||||
gbc_btnCancel.gridy = 1;
|
||||
panel_1.add(btnCancel, gbc_btnCancel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleException(Exception e) {
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
JOptionPane.showMessageDialog(null, e.getMessage(), title, JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleExceptionAndExit(Exception e) {
|
||||
// Used to prevent the done() handler of SwingWorker executing if the invokeLater hasn't happened yet
|
||||
aboutToCrash.set(true);
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
JOptionPane.showMessageDialog(null, e.getMessage(), title, JOptionPane.ERROR_MESSAGE);
|
||||
System.exit(1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
if (frmPackwizlauncher != null) {
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
InstallWindow.this.frmPackwizlauncher.setTitle(title);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void submitProgress(InstallProgress progress) {
|
||||
if (worker != null) {
|
||||
worker.publishPublic(progress);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void executeManager(Runnable task) {
|
||||
EventQueue.invokeLater(new Runnable() {
|
||||
public void run() {
|
||||
worker = new SwingWorkerButWithPublicPublish<Void, InstallProgress>() {
|
||||
|
||||
@Override
|
||||
protected Void doInBackground() throws Exception {
|
||||
task.run();
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void process(List<InstallProgress> chunks) {
|
||||
// Only process last chunk
|
||||
if (chunks.size() > 0) {
|
||||
InstallProgress prog = chunks.get(chunks.size() - 1);
|
||||
if (prog.hasProgress) {
|
||||
progressBar.setIndeterminate(false);
|
||||
progressBar.setValue(prog.progress);
|
||||
progressBar.setMaximum(prog.progressTotal);
|
||||
} else {
|
||||
progressBar.setIndeterminate(true);
|
||||
progressBar.setValue(0);
|
||||
}
|
||||
lblProgresslabel.setText(prog.message);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void done() {
|
||||
if (aboutToCrash.get()) {
|
||||
return;
|
||||
}
|
||||
// TODO: a better way to do this?
|
||||
frmPackwizlauncher.dispose();
|
||||
System.out.println("Finished successfully!");
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
};
|
||||
worker.execute();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
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
|
||||
public abstract class SwingWorkerButWithPublicPublish<T,V> extends SwingWorker<T,V> {
|
||||
@SafeVarargs
|
||||
public final void publishPublic(V... chunks) {
|
||||
publish(chunks);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user