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