From 3301d00ab1df7a0f88985d143787f9f3c2283e38 Mon Sep 17 00:00:00 2001 From: jeff Date: Thu, 28 Aug 2014 10:43:48 -0400 Subject: added crash reporter --- src/cuchaz/enigma/gui/CrashDialog.java | 107 +++++++++++++++++++++++++++++++++ src/cuchaz/enigma/gui/Gui.java | 22 +++++-- 2 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 src/cuchaz/enigma/gui/CrashDialog.java (limited to 'src/cuchaz') diff --git a/src/cuchaz/enigma/gui/CrashDialog.java b/src/cuchaz/enigma/gui/CrashDialog.java new file mode 100644 index 00000000..501080ec --- /dev/null +++ b/src/cuchaz/enigma/gui/CrashDialog.java @@ -0,0 +1,107 @@ +/******************************************************************************* + * Copyright (c) 2014 Jeff Martin. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + * + * Contributors: + * Jeff Martin - initial API and implementation + ******************************************************************************/ +package cuchaz.enigma.gui; + +import java.awt.BorderLayout; +import java.awt.Container; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.PrintWriter; +import java.io.StringWriter; + +import javax.swing.BorderFactory; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTextArea; +import javax.swing.WindowConstants; + +import cuchaz.enigma.Constants; + +public class CrashDialog +{ + private static CrashDialog m_instance = null; + + private JFrame m_frame; + private JTextArea m_text; + + private CrashDialog( JFrame parent ) + { + // init frame + m_frame = new JFrame( Constants.Name + " - Crash Report" ); + final Container pane = m_frame.getContentPane(); + pane.setLayout( new BorderLayout() ); + + JLabel label = new JLabel( Constants.Name + " has crashed! =(" ); + label.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) ); + pane.add( label, BorderLayout.NORTH ); + + // report panel + m_text = new JTextArea(); + m_text.setTabSize( 2 ); + pane.add( new JScrollPane( m_text ), BorderLayout.CENTER ); + + // buttons panel + JPanel buttonsPanel = new JPanel(); + FlowLayout buttonsLayout = new FlowLayout(); + buttonsLayout.setAlignment( FlowLayout.RIGHT ); + buttonsPanel.setLayout( buttonsLayout ); + JButton ignoreButton = new JButton( "Ignore" ); + ignoreButton.addActionListener( new ActionListener( ) + { + @Override + public void actionPerformed( ActionEvent event ) + { + // close (hide) the dialog + m_frame.setVisible( false ); + } + } ); + buttonsPanel.add( ignoreButton ); + JButton exitButton = new JButton( "Exit" ); + exitButton.addActionListener( new ActionListener( ) + { + @Override + public void actionPerformed( ActionEvent event ) + { + // exit enigma + System.exit( 1 ); + } + } ); + buttonsPanel.add( exitButton ); + pane.add( buttonsPanel, BorderLayout.SOUTH ); + + // show the frame + m_frame.setSize( 600, 400 ); + m_frame.setLocationRelativeTo( parent ); + m_frame.setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE ); + } + + public static void init( JFrame parent ) + { + m_instance = new CrashDialog( parent ); + } + + public static void show( Throwable ex ) + { + // get the error report + StringWriter buf = new StringWriter(); + ex.printStackTrace( new PrintWriter( buf ) ); + String report = buf.toString(); + + // show it! + m_instance.m_text.setText( report ); + m_instance.m_frame.doLayout(); + m_instance.m_frame.setVisible( true ); + } +} diff --git a/src/cuchaz/enigma/gui/Gui.java b/src/cuchaz/enigma/gui/Gui.java index 3dcb4e24..e357382a 100644 --- a/src/cuchaz/enigma/gui/Gui.java +++ b/src/cuchaz/enigma/gui/Gui.java @@ -28,6 +28,7 @@ import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.File; import java.io.IOException; +import java.lang.Thread.UncaughtExceptionHandler; import java.util.Collection; import java.util.Collections; import java.util.Comparator; @@ -176,6 +177,22 @@ public class Gui public Gui( ) { + // init frame + m_frame = new JFrame( Constants.Name ); + final Container pane = m_frame.getContentPane(); + pane.setLayout( new BorderLayout() ); + + // install a global exception handler to the event thread + CrashDialog.init( m_frame ); + Thread.setDefaultUncaughtExceptionHandler( new UncaughtExceptionHandler( ) + { + @Override + public void uncaughtException( Thread thread, Throwable ex ) + { + CrashDialog.show( ex ); + } + } ); + m_controller = new GuiController( this ); // init file choosers @@ -184,11 +201,6 @@ public class Gui m_exportFileChooser = new JFileChooser(); m_exportFileChooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY ); - // init frame - m_frame = new JFrame( Constants.Name ); - final Container pane = m_frame.getContentPane(); - pane.setLayout( new BorderLayout() ); - // init obfuscated classes list m_obfClasses = new JList(); m_obfClasses.setSelectionMode( ListSelectionModel.SINGLE_SELECTION ); -- cgit v1.2.3