summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/gui/CrashDialog.java
blob: 904273c1137d62fd978fe779a0c2072e5aeef8a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*******************************************************************************
 * Copyright (c) 2015 Jeff Martin.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public
 * License v3.0 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.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);
		buttonsPanel.add(GuiTricks.unboldLabel(new JLabel("If you choose exit, you will lose any unsaved work.")));
		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);
	}
}