2015-01-16 03:55:26 +01:00
|
|
|
/*
|
|
|
|
* opsu! - an open-source osu! client
|
2015-01-16 18:05:44 +01:00
|
|
|
* Copyright (C) 2014, 2015 Jeffrey Han
|
2015-01-16 03:55:26 +01:00
|
|
|
*
|
|
|
|
* opsu! is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* opsu! is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with opsu!. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package itdelatrisu.opsu;
|
|
|
|
|
|
|
|
import java.awt.Cursor;
|
|
|
|
import java.awt.Desktop;
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.io.StringWriter;
|
|
|
|
|
|
|
|
import javax.swing.JOptionPane;
|
|
|
|
import javax.swing.JScrollPane;
|
|
|
|
import javax.swing.JTextArea;
|
|
|
|
import javax.swing.UIManager;
|
|
|
|
|
|
|
|
import org.newdawn.slick.util.Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error handler to log and display errors.
|
|
|
|
*/
|
|
|
|
public class ErrorHandler {
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Error popup title. */
|
2015-01-16 03:55:26 +01:00
|
|
|
private static final String title = "Error";
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Error popup description text. */
|
2015-01-16 03:55:26 +01:00
|
|
|
private static final String
|
2015-01-22 06:44:45 +01:00
|
|
|
desc = "opsu! has encountered an error.",
|
2015-01-16 03:55:26 +01:00
|
|
|
descR = "opsu! has encountered an error. Please report this!";
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Error popup button options. */
|
2015-01-16 03:55:26 +01:00
|
|
|
private static final String[]
|
2015-01-22 06:44:45 +01:00
|
|
|
options = {"View Error Log", "Close"},
|
2015-01-16 03:55:26 +01:00
|
|
|
optionsR = {"Send Report", "View Error Log", "Close"};
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Text area for Exception. */
|
2015-01-16 03:55:26 +01:00
|
|
|
private static final JTextArea textArea = new JTextArea(7, 30);
|
|
|
|
static {
|
|
|
|
textArea.setEditable(false);
|
|
|
|
textArea.setBackground(UIManager.getColor("Panel.background"));
|
|
|
|
textArea.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
|
|
|
|
textArea.setTabSize(2);
|
|
|
|
textArea.setLineWrap(true);
|
|
|
|
}
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Scroll pane holding JTextArea. */
|
2015-01-16 03:55:26 +01:00
|
|
|
private static final JScrollPane scroll = new JScrollPane(textArea);
|
|
|
|
|
2015-01-22 06:44:45 +01:00
|
|
|
/** Error popup objects. */
|
2015-01-16 03:55:26 +01:00
|
|
|
private static final Object[]
|
2015-01-22 06:44:45 +01:00
|
|
|
message = { desc, scroll },
|
2015-01-16 03:55:26 +01:00
|
|
|
messageR = { descR, scroll };
|
|
|
|
|
|
|
|
// This class should not be instantiated.
|
|
|
|
private ErrorHandler() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays an error popup and logs the given error.
|
|
|
|
* @param error a descR of the error
|
|
|
|
* @param e the exception causing the error
|
|
|
|
* @param report whether to ask to report the error
|
|
|
|
*/
|
|
|
|
public static void error(String error, Throwable e, boolean report) {
|
|
|
|
if (error == null && e == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// log the error
|
|
|
|
if (error == null)
|
|
|
|
Log.error(e);
|
|
|
|
else if (e == null)
|
|
|
|
Log.error(error);
|
|
|
|
else
|
|
|
|
Log.error(error, e);
|
|
|
|
|
|
|
|
// set the textArea to the error message
|
|
|
|
textArea.setText(null);
|
|
|
|
if (error != null) {
|
|
|
|
textArea.append(error);
|
|
|
|
textArea.append("\n");
|
|
|
|
}
|
|
|
|
if (e != null) {
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
e.printStackTrace(new PrintWriter(sw));
|
|
|
|
textArea.append(sw.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
// display popup
|
|
|
|
try {
|
|
|
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
|
|
|
if (Desktop.isDesktopSupported()) {
|
|
|
|
// try to open the log file and/or issues webpage
|
|
|
|
if (report) {
|
|
|
|
// ask to report the error
|
|
|
|
int n = JOptionPane.showOptionDialog(null, messageR, title,
|
|
|
|
JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
|
|
|
|
null, optionsR, optionsR[2]);
|
|
|
|
if (n == 0) {
|
2015-01-16 19:42:54 +01:00
|
|
|
Desktop.getDesktop().browse(Options.ISSUES_URI);
|
2015-01-16 03:55:26 +01:00
|
|
|
Desktop.getDesktop().open(Options.LOG_FILE);
|
|
|
|
} else if (n == 1)
|
|
|
|
Desktop.getDesktop().open(Options.LOG_FILE);
|
|
|
|
} else {
|
|
|
|
// don't report the error
|
|
|
|
int n = JOptionPane.showOptionDialog(null, message, title,
|
|
|
|
JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE,
|
2015-01-17 06:44:23 +01:00
|
|
|
null, options, options[1]);
|
2015-01-16 03:55:26 +01:00
|
|
|
if (n == 0)
|
|
|
|
Desktop.getDesktop().open(Options.LOG_FILE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// display error only
|
|
|
|
JOptionPane.showMessageDialog(null, report ? messageR : message,
|
|
|
|
title, JOptionPane.ERROR_MESSAGE);
|
|
|
|
}
|
|
|
|
} catch (Exception e1) {
|
|
|
|
Log.error("Error opening crash popup.", e1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|