summaryrefslogtreecommitdiff
path: root/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/CrashDialog.java
blob: c2a93fa56fd19d05f36cc52f8b7bd2666f5ca988 (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
102
103
104
105
/*******************************************************************************
 * 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
 * <p>
 * Contributors:
 * Jeff Martin - initial API and implementation
 ******************************************************************************/

package cuchaz.enigma.gui.dialog;

import cuchaz.enigma.Enigma;
import cuchaz.enigma.gui.util.GuiUtil;
import cuchaz.enigma.utils.I18n;
import cuchaz.enigma.gui.util.ScaleUtil;

import javax.swing.*;
import java.awt.*;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.FileWriter;
import java.io.File;
import java.io.IOException;

public class CrashDialog {

	private static CrashDialog instance = null;

	private JFrame frame;
	private JTextArea text;

	private CrashDialog(JFrame parent) {
		// init frame
		frame = new JFrame(String.format(I18n.translate("crash.title"), Enigma.NAME));
		final Container pane = frame.getContentPane();
		pane.setLayout(new BorderLayout());

		JLabel label = new JLabel(String.format(I18n.translate("crash.summary"), Enigma.NAME));
		label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		pane.add(label, BorderLayout.NORTH);

		// report panel
		text = new JTextArea();
		text.setTabSize(2);
		pane.add(new JScrollPane(text), BorderLayout.CENTER);

		// buttons panel
		JPanel buttonsPanel = new JPanel();
		buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.LINE_AXIS));
		JButton exportButton = new JButton(I18n.translate("crash.export"));
		exportButton.addActionListener(event -> {
			JFileChooser chooser = new JFileChooser();
			chooser.setSelectedFile(new File("enigma_crash.log"));
			if (chooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
				try {
					File file = chooser.getSelectedFile();
					FileWriter writer = new FileWriter(file);
					writer.write(instance.text.getText());
					writer.close();
				} catch (IOException ex) {
					ex.printStackTrace();
				}
			}
		});
		buttonsPanel.add(exportButton);
		buttonsPanel.add(Box.createHorizontalGlue());
		buttonsPanel.add(GuiUtil.unboldLabel(new JLabel(I18n.translate("crash.exit.warning"))));
		JButton ignoreButton = new JButton(I18n.translate("crash.ignore"));
		ignoreButton.addActionListener(event -> {
			// close (hide) the dialog
			frame.setVisible(false);
		});
		buttonsPanel.add(ignoreButton);
		JButton exitButton = new JButton(I18n.translate("crash.exit"));
		exitButton.addActionListener(event -> {
			// exit enigma
			System.exit(1);
		});
		buttonsPanel.add(exitButton);
		pane.add(buttonsPanel, BorderLayout.SOUTH);

		// show the frame
		frame.setSize(ScaleUtil.getDimension(600, 400));
		frame.setLocationRelativeTo(parent);
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
	}

	public static void init(JFrame parent) {
		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!
		instance.text.setText(report);
		instance.frame.doLayout();
		instance.frame.setVisible(true);
	}
}