Skip to content

Commit

Permalink
Add "Open Log" Button to when an error popup occurs (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
RealMangorage authored Mar 4, 2024
1 parent 3edfaec commit d260e1c
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
24 changes: 2 additions & 22 deletions src/main/java/net/minecraftforge/installer/InstallerPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,18 @@
*/
package net.minecraftforge.installer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;

import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
Expand All @@ -36,29 +24,20 @@
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

import net.minecraftforge.installer.actions.Action;
import net.minecraftforge.installer.actions.ActionCanceledException;
import net.minecraftforge.installer.actions.Actions;
import net.minecraftforge.installer.actions.ProgressCallback;
import net.minecraftforge.installer.json.Install;
import net.minecraftforge.installer.json.InstallV1;
import net.minecraftforge.installer.json.Mirror;
import net.minecraftforge.installer.json.OptionalLibrary;
import net.minecraftforge.installer.json.Util;

@SuppressWarnings("unused")
public class InstallerPanel extends JPanel {
Expand Down Expand Up @@ -280,6 +259,7 @@ public void run(ProgressCallback monitor) {
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
int result = (Integer) (optionPane.getValue() != null ? optionPane.getValue() : -1);

if (result == JOptionPane.OK_OPTION) {
ProgressFrame prog = new ProgressFrame(monitor, "Installing " + profile.getVersion(), Thread.currentThread()::interrupt);
SimpleInstaller.hookStdOut(prog);
Expand All @@ -304,7 +284,7 @@ public void run(ProgressCallback monitor) {
"The following addresse did not have valid certificates: " + badCerts + "<br>" +
"This typically happens with an outdated java install. Try updating your java install from https://adoptium.net/";
}
JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
JOptionPane.showOptionDialog(null, message, "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, new Object[]{"Ok", SwingUtil.createLogButton()}, "");
e.printStackTrace();
} finally {
prog.dispose();
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/net/minecraftforge/installer/SwingUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package net.minecraftforge.installer;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;

public class SwingUtil {
public static JButton createLogButton() {
JButton button = new JButton("Open log");
button.addActionListener(ev -> {
File file = new File("installer.log");
try {
if (file.exists())
Desktop.getDesktop().open(file);
} catch (IOException e) {
// Handle any errors that may occur during file opening
e.printStackTrace();

// Show the error. Does not happen on headless.
// Can only occur on NON-Headless, so its safe to just show the error
JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
});
return button;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import net.minecraftforge.installer.SimpleInstaller;
import net.minecraftforge.installer.json.Artifact;
import net.minecraftforge.installer.json.InstallV1;
import net.minecraftforge.installer.SwingUtil;
import net.minecraftforge.installer.json.Util;
import net.minecraftforge.installer.json.Version;
import net.minecraftforge.installer.json.Version.Download;
Expand All @@ -40,7 +41,7 @@ protected Action(InstallV1 profile, ProgressCallback monitor, boolean isClient)

protected void error(String message) {
if (!SimpleInstaller.headless)
JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
JOptionPane.showOptionDialog(null, message, "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, new Object[]{"Ok", SwingUtil.createLogButton()}, "");
monitor.stage(message);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.minecraftforge.installer.json.Artifact;
import net.minecraftforge.installer.json.Install.Processor;
import net.minecraftforge.installer.json.InstallV1;
import net.minecraftforge.installer.SwingUtil;
import net.minecraftforge.installer.json.Version.Library;
import net.minecraftforge.installer.json.Util;

Expand Down Expand Up @@ -281,7 +282,7 @@ private void handleError(Throwable e) {

private void error(String message) {
if (!SimpleInstaller.headless)
JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
JOptionPane.showOptionDialog(null, message, "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, new Object[]{"Ok", SwingUtil.createLogButton()}, "");
for (String line : message.split("\n"))
monitor.message(line);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/minecraftforge/installer/json/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,5 @@ public static String replaceTokens(Map<String, ? extends Supplier<String>> token

return buf.toString();
}

}

0 comments on commit d260e1c

Please sign in to comment.