From 452ab15cc70abc0cc830c729af3bbba96e2aa29d Mon Sep 17 00:00:00 2001 From: comp500 Date: Sun, 11 Aug 2019 00:53:29 +0100 Subject: [PATCH] Add new GUI code from windowbuilder, fix a thing --- .../installer/ui/ExceptionListWindow.java | 201 ++++++++++++++++++ .../installer/ui/IExceptionDetails.java | 10 + .../installer/ui/OptionsSelectWindow.java | 1 - 3 files changed, 211 insertions(+), 1 deletion(-) create mode 100644 src/main/java/link/infra/packwiz/installer/ui/ExceptionListWindow.java create mode 100644 src/main/java/link/infra/packwiz/installer/ui/IExceptionDetails.java diff --git a/src/main/java/link/infra/packwiz/installer/ui/ExceptionListWindow.java b/src/main/java/link/infra/packwiz/installer/ui/ExceptionListWindow.java new file mode 100644 index 0000000..f99d0a1 --- /dev/null +++ b/src/main/java/link/infra/packwiz/installer/ui/ExceptionListWindow.java @@ -0,0 +1,201 @@ +package link.infra.packwiz.installer.ui; + +import link.infra.packwiz.installer.ui.IExceptionDetails.ExceptionListResult; + +import javax.swing.*; +import javax.swing.border.EmptyBorder; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +public class ExceptionListWindow extends JDialog { + + private static final long serialVersionUID = 1L; + private final JPanel contentPanel = new JPanel(); + private final JTextArea lblExceptionStacktrace; + + /** + * Create the dialog. + */ + public ExceptionListWindow(List eList, CompletableFuture future, int numTotal, JFrame parentWindow) { + super(parentWindow, "Failed file downloads", true); + + setBounds(100, 100, 540, 340); + setLocationRelativeTo(parentWindow); + getContentPane().setLayout(new BorderLayout()); + { + JPanel errorPanel = new JPanel(); + getContentPane().add(errorPanel, BorderLayout.NORTH); + { + JLabel lblWarning = new JLabel("One or more errors were encountered while installing the modpack!"); + lblWarning.setIcon(UIManager.getIcon("OptionPane.warningIcon")); + errorPanel.add(lblWarning); + } + } + contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5)); + getContentPane().add(contentPanel, BorderLayout.CENTER); + contentPanel.setLayout(new BorderLayout(0, 0)); + { + JSplitPane splitPane = new JSplitPane(); + splitPane.setResizeWeight(0.3); + contentPanel.add(splitPane); + { + lblExceptionStacktrace = new JTextArea("Select a file"); + lblExceptionStacktrace.setBackground(UIManager.getColor("List.background")); + lblExceptionStacktrace.setOpaque(true); + lblExceptionStacktrace.setWrapStyleWord(true); + lblExceptionStacktrace.setLineWrap(true); + lblExceptionStacktrace.setEditable(false); + lblExceptionStacktrace.setFocusable(true); + lblExceptionStacktrace.setFont(UIManager.getFont("Label.font")); + lblExceptionStacktrace.setBorder(new EmptyBorder(5, 5, 5, 5)); + JScrollPane scrollPane = new JScrollPane(lblExceptionStacktrace); + scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0)); + splitPane.setRightComponent(scrollPane); + } + { + JList list = new JList(); + list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + list.setBorder(new EmptyBorder(5, 5, 5, 5)); + ExceptionListModel listModel = new ExceptionListModel(eList); + list.setModel(listModel); + list.addListSelectionListener(new ListSelectionListener() { + @Override + public void valueChanged(ListSelectionEvent e) { + int i = list.getSelectedIndex(); + if (i > -1) { + StringWriter sw = new StringWriter(); + listModel.getExceptionAt(i).printStackTrace(new PrintWriter(sw)); + lblExceptionStacktrace.setText(sw.toString()); + // Scroll to the top + lblExceptionStacktrace.setCaretPosition(0); + } else { + lblExceptionStacktrace.setText("Select a file"); + } + } + }); + JScrollPane scrollPane = new JScrollPane(list); + scrollPane.setBorder(new EmptyBorder(0, 0, 0, 0)); + splitPane.setLeftComponent(scrollPane); + } + } + { + JPanel buttonPane = new JPanel(); + getContentPane().add(buttonPane, BorderLayout.SOUTH); + buttonPane.setLayout(new BorderLayout(0, 0)); + { + JPanel rightButtons = new JPanel(); + buttonPane.add(rightButtons, BorderLayout.EAST); + { + JButton btnContinue = new JButton("Continue"); + btnContinue.setToolTipText("Attempt to continue installing, excluding the failed downloads"); + btnContinue.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + future.complete(ExceptionListResult.CONTINUE); + ExceptionListWindow.this.dispose(); + } + }); + rightButtons.add(btnContinue); + } + { + JButton btnCancelLaunch = new JButton("Cancel launch"); + btnCancelLaunch.setToolTipText("Stop launching the game"); + btnCancelLaunch.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + future.complete(ExceptionListResult.CANCEL); + ExceptionListWindow.this.dispose(); + } + }); + rightButtons.add(btnCancelLaunch); + } + { + JButton btnIgnoreUpdate = new JButton("Ignore update"); + btnIgnoreUpdate.setToolTipText("Start the game without attempting to update"); + btnIgnoreUpdate.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + future.complete(ExceptionListResult.IGNORE); + ExceptionListWindow.this.dispose(); + } + }); + rightButtons.add(btnIgnoreUpdate); + { + JLabel lblErrored = new JLabel(eList.size() + "/" + numTotal + " errored"); + lblErrored.setHorizontalAlignment(SwingConstants.CENTER); + buttonPane.add(lblErrored, BorderLayout.CENTER); + } + { + JPanel leftButtons = new JPanel(); + buttonPane.add(leftButtons, BorderLayout.WEST); + { + JButton btnReportIssue = new JButton("Report issue"); + boolean supported = Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE); + btnReportIssue.setEnabled(supported); + if (supported) { + btnReportIssue.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + try { + Desktop.getDesktop().browse(new URI("https://github.com/comp500/packwiz-installer/issues/new")); + } catch (IOException | URISyntaxException e1) { + // lol the button just won't work i guess + } + } + }); + } + leftButtons.add(btnReportIssue); + } + } + } + } + } + addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + future.complete(ExceptionListResult.CANCEL); + } + + @Override + public void windowClosed(WindowEvent e) { + // Just in case closing didn't get triggered - if something else called dispose() the + // future will have already completed + future.complete(ExceptionListResult.CANCEL); + } + }); + } + + private static class ExceptionListModel extends AbstractListModel { + private static final long serialVersionUID = 1L; + private final List details; + + public ExceptionListModel(List details) { + this.details = details; + } + + public int getSize() { + return details.size(); + } + + public String getElementAt(int index) { + return details.get(index).getName(); + } + + public Exception getExceptionAt(int index) { + return details.get(index).getException(); + } + } + +} diff --git a/src/main/java/link/infra/packwiz/installer/ui/IExceptionDetails.java b/src/main/java/link/infra/packwiz/installer/ui/IExceptionDetails.java new file mode 100644 index 0000000..7207f8b --- /dev/null +++ b/src/main/java/link/infra/packwiz/installer/ui/IExceptionDetails.java @@ -0,0 +1,10 @@ +package link.infra.packwiz.installer.ui; + +public interface IExceptionDetails { + Exception getException(); + String getName(); + + enum ExceptionListResult { + CONTINUE, CANCEL, IGNORE + } +} diff --git a/src/main/java/link/infra/packwiz/installer/ui/OptionsSelectWindow.java b/src/main/java/link/infra/packwiz/installer/ui/OptionsSelectWindow.java index c20c1c8..392f5ba 100644 --- a/src/main/java/link/infra/packwiz/installer/ui/OptionsSelectWindow.java +++ b/src/main/java/link/infra/packwiz/installer/ui/OptionsSelectWindow.java @@ -77,7 +77,6 @@ public class OptionsSelectWindow extends JDialog implements ActionListener { lblOptionDescription.setOpaque(true); lblOptionDescription.setWrapStyleWord(true); lblOptionDescription.setLineWrap(true); - lblOptionDescription.setOpaque(true); lblOptionDescription.setEditable(false); lblOptionDescription.setFocusable(false); lblOptionDescription.setFont(UIManager.getFont("Label.font"));