summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/gui/CrashDialog.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/cuchaz/enigma/gui/CrashDialog.java')
-rw-r--r--src/main/java/cuchaz/enigma/gui/CrashDialog.java94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/gui/CrashDialog.java b/src/main/java/cuchaz/enigma/gui/CrashDialog.java
new file mode 100644
index 0000000..c0c0869
--- /dev/null
+++ b/src/main/java/cuchaz/enigma/gui/CrashDialog.java
@@ -0,0 +1,94 @@
1/*******************************************************************************
2 * Copyright (c) 2015 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser General Public
5 * License v3.0 which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/lgpl.html
7 * <p>
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11package cuchaz.enigma.gui;
12
13import java.awt.BorderLayout;
14import java.awt.Container;
15import java.awt.FlowLayout;
16import java.awt.event.ActionEvent;
17import java.awt.event.ActionListener;
18import java.io.PrintWriter;
19import java.io.StringWriter;
20
21import javax.swing.*;
22
23import cuchaz.enigma.Constants;
24
25public class CrashDialog {
26
27 private static CrashDialog m_instance = null;
28
29 private JFrame m_frame;
30 private JTextArea m_text;
31
32 private CrashDialog(JFrame parent) {
33 // init frame
34 m_frame = new JFrame(Constants.Name + " - Crash Report");
35 final Container pane = m_frame.getContentPane();
36 pane.setLayout(new BorderLayout());
37
38 JLabel label = new JLabel(Constants.Name + " has crashed! =(");
39 label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
40 pane.add(label, BorderLayout.NORTH);
41
42 // report panel
43 m_text = new JTextArea();
44 m_text.setTabSize(2);
45 pane.add(new JScrollPane(m_text), BorderLayout.CENTER);
46
47 // buttons panel
48 JPanel buttonsPanel = new JPanel();
49 FlowLayout buttonsLayout = new FlowLayout();
50 buttonsLayout.setAlignment(FlowLayout.RIGHT);
51 buttonsPanel.setLayout(buttonsLayout);
52 buttonsPanel.add(GuiTricks.unboldLabel(new JLabel("If you choose exit, you will lose any unsaved work.")));
53 JButton ignoreButton = new JButton("Ignore");
54 ignoreButton.addActionListener(new ActionListener() {
55 @Override
56 public void actionPerformed(ActionEvent event) {
57 // close (hide) the dialog
58 m_frame.setVisible(false);
59 }
60 });
61 buttonsPanel.add(ignoreButton);
62 JButton exitButton = new JButton("Exit");
63 exitButton.addActionListener(new ActionListener() {
64 @Override
65 public void actionPerformed(ActionEvent event) {
66 // exit enigma
67 System.exit(1);
68 }
69 });
70 buttonsPanel.add(exitButton);
71 pane.add(buttonsPanel, BorderLayout.SOUTH);
72
73 // show the frame
74 m_frame.setSize(600, 400);
75 m_frame.setLocationRelativeTo(parent);
76 m_frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
77 }
78
79 public static void init(JFrame parent) {
80 m_instance = new CrashDialog(parent);
81 }
82
83 public static void show(Throwable ex) {
84 // get the error report
85 StringWriter buf = new StringWriter();
86 ex.printStackTrace(new PrintWriter(buf));
87 String report = buf.toString();
88
89 // show it!
90 m_instance.m_text.setText(report);
91 m_instance.m_frame.doLayout();
92 m_instance.m_frame.setVisible(true);
93 }
94}