You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FatalErrorDialog.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (c) 2006-2015 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.ui;
  23. import com.dmdirc.logger.ErrorReportStatus;
  24. import com.dmdirc.logger.ErrorReportingRunnable;
  25. import com.dmdirc.logger.ProgramError;
  26. import com.dmdirc.logger.SentryErrorReporter;
  27. import java.awt.BorderLayout;
  28. import java.awt.Dialog;
  29. import java.awt.Dimension;
  30. import java.awt.event.ActionEvent;
  31. import java.awt.event.ActionListener;
  32. import java.util.concurrent.CountDownLatch;
  33. import javax.swing.BorderFactory;
  34. import javax.swing.Box;
  35. import javax.swing.BoxLayout;
  36. import javax.swing.ImageIcon;
  37. import javax.swing.JButton;
  38. import javax.swing.JDialog;
  39. import javax.swing.JLabel;
  40. import javax.swing.JPanel;
  41. import javax.swing.JScrollPane;
  42. import javax.swing.JTextArea;
  43. import javax.swing.JTextPane;
  44. import javax.swing.SwingWorker;
  45. import javax.swing.WindowConstants;
  46. import javax.swing.text.DefaultStyledDocument;
  47. import javax.swing.text.MutableAttributeSet;
  48. import javax.swing.text.SimpleAttributeSet;
  49. import javax.swing.text.StyleConstants;
  50. import javax.swing.text.StyledDocument;
  51. /**
  52. * The fatal error dialog is used to inform the user that a fatal error has occurred and to give them
  53. * a chance to quit or restart the client.
  54. */
  55. public final class FatalErrorDialog extends JDialog implements ActionListener {
  56. /** Serialisation version ID. */
  57. private static final long serialVersionUID = 3;
  58. /** Fatal error to be shown in this dialog. */
  59. private final ProgramError error;
  60. /** Countdown latch. */
  61. private final CountDownLatch countDownLatch;
  62. /** Are we auto sending errors? */
  63. private final boolean sendReports;
  64. /** Restart client Button. */
  65. private JButton restartButton;
  66. /** Quit client button. */
  67. private JButton quitButton;
  68. /** Send error button. */
  69. private JButton sendButton;
  70. /** Info panel, informs the user what is happening. */
  71. private JTextPane infoLabel;
  72. /** Message label, contains details error information. */
  73. private JTextPane messageLabel;
  74. /** Fatal error icon. */
  75. private ImageIcon icon;
  76. /** Stack trace scroll pane. */
  77. private JScrollPane scrollPane;
  78. /** Do we need to restart? Else we quit. */
  79. private boolean restart = true;
  80. /**
  81. * Creates a new fatal error dialog.
  82. *
  83. * @param error Error
  84. */
  85. public FatalErrorDialog(final ProgramError error, final SentryErrorReporter sentryErrorReporter,
  86. final CountDownLatch countDownLatch, final boolean sendReports) {
  87. super(null, Dialog.ModalityType.TOOLKIT_MODAL);
  88. setModal(true);
  89. this.error = error;
  90. this.countDownLatch = countDownLatch;
  91. this.sendReports = sendReports;
  92. initComponents();
  93. layoutComponents();
  94. new SwingWorker<Void, Void>() {
  95. @Override
  96. protected Void doInBackground() {
  97. new ErrorReportingRunnable(sentryErrorReporter, error).run();
  98. return null;
  99. }
  100. @Override
  101. protected void done() {
  102. allowClose();
  103. }
  104. }.execute();
  105. setResizable(false);
  106. CoreUIUtils.centreWindow(this);
  107. }
  108. /**
  109. * Initialises the components for this dialog.
  110. */
  111. private void initComponents() {
  112. final JTextArea stacktraceField = new JTextArea();
  113. infoLabel = new JTextPane(new DefaultStyledDocument());
  114. infoLabel.setOpaque(false);
  115. infoLabel.setEditable(false);
  116. infoLabel.setHighlighter(null);
  117. messageLabel = new JTextPane(new DefaultStyledDocument());
  118. messageLabel.setOpaque(false);
  119. messageLabel.setEditable(false);
  120. messageLabel.setHighlighter(null);
  121. final MutableAttributeSet sas = new SimpleAttributeSet();
  122. StyleConstants.setAlignment(sas, StyleConstants.ALIGN_JUSTIFIED);
  123. scrollPane = new JScrollPane();
  124. restartButton = new JButton();
  125. sendButton = new JButton();
  126. quitButton = new JButton();
  127. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  128. setTitle("DMDirc: Fatal Error");
  129. infoLabel.setText("DMDirc has encountered a fatal error, and is "
  130. + "not able to recover. \nThe application will now terminate.");
  131. messageLabel.setText("Description: " + error.getMessage());
  132. ((StyledDocument) infoLabel.getDocument()).setParagraphAttributes(0,
  133. infoLabel.getText().length(), sas, false);
  134. ((StyledDocument) messageLabel.getDocument()).setParagraphAttributes(0,
  135. messageLabel.getText().length(), sas, false);
  136. icon = new ImageIcon(FatalErrorDialog.class.getResource("/com/dmdirc/res/error.png"));
  137. stacktraceField.setEditable(false);
  138. error.getThrowableAsString().ifPresent(stacktraceField::append);
  139. stacktraceField.setCaretPosition(0);
  140. scrollPane.setViewportView(stacktraceField);
  141. restartButton.setText("Restart");
  142. quitButton.setText("Quit");
  143. sendButton.setText("Send");
  144. final ErrorReportStatus status = error.getReportStatus();
  145. restartButton.setEnabled(status.isTerminal());
  146. quitButton.setEnabled(status.isTerminal());
  147. updateSendButtonText(status);
  148. restartButton.addActionListener(this);
  149. sendButton.addActionListener(this);
  150. quitButton.addActionListener(this);
  151. }
  152. /**
  153. * lays the components out in the dialog.
  154. */
  155. private void layoutComponents() {
  156. final JPanel panel = new JPanel();
  157. final JPanel blurb = new JPanel();
  158. final JPanel info = new JPanel();
  159. final JPanel buttons = new JPanel();
  160. blurb.setLayout(new BorderLayout(5, 5));
  161. info.setLayout(new BorderLayout(5, 5));
  162. buttons.setLayout(new BoxLayout(buttons, BoxLayout.LINE_AXIS));
  163. blurb.add(new JLabel(icon), BorderLayout.LINE_START);
  164. blurb.add(infoLabel, BorderLayout.CENTER);
  165. info.add(messageLabel, BorderLayout.NORTH);
  166. info.add(scrollPane, BorderLayout.CENTER);
  167. buttons.add(Box.createHorizontalGlue());
  168. buttons.add(sendButton);
  169. buttons.add(Box.createHorizontalStrut(5));
  170. buttons.add(quitButton);
  171. buttons.add(Box.createHorizontalStrut(5));
  172. buttons.add(restartButton);
  173. panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
  174. panel.setLayout(new BorderLayout(5, 5));
  175. panel.add(blurb, BorderLayout.NORTH);
  176. panel.add(info, BorderLayout.CENTER);
  177. panel.add(buttons, BorderLayout.SOUTH);
  178. getContentPane().add(panel);
  179. setSize(new Dimension(550, 260));
  180. }
  181. @Override
  182. public void actionPerformed(final ActionEvent actionEvent) {
  183. if (actionEvent.getSource() == sendButton) {
  184. sendButton.setText("Sending...");
  185. restartButton.setEnabled(false);
  186. sendButton.setEnabled(false);
  187. } else if (actionEvent.getSource() == quitButton) {
  188. restart = false;
  189. dispose();
  190. } else {
  191. dispose();
  192. }
  193. if (!sendReports) {
  194. countDownLatch.countDown();
  195. }
  196. countDownLatch.countDown();
  197. }
  198. /**
  199. * Returns the restart response of this dialog. This will default to true if the user is yet to
  200. * make a choice.
  201. *
  202. * @return Whether to restart after this error
  203. */
  204. public boolean getRestart() {
  205. return restart;
  206. }
  207. /**
  208. * Updates the send button with the specified status.
  209. *
  210. * @param status New error status
  211. */
  212. private void updateSendButtonText(final ErrorReportStatus status) {
  213. switch (status) {
  214. case WAITING:
  215. sendButton.setText("Send");
  216. sendButton.setEnabled(true);
  217. break;
  218. case QUEUED:
  219. sendButton.setText("Queued");
  220. sendButton.setEnabled(false);
  221. break;
  222. case SENDING:
  223. sendButton.setText("Sending");
  224. sendButton.setEnabled(false);
  225. break;
  226. case ERROR:
  227. sendButton.setText("Error, resend");
  228. sendButton.setEnabled(true);
  229. break;
  230. case FINISHED:
  231. sendButton.setText("Sent");
  232. sendButton.setEnabled(false);
  233. break;
  234. case NOT_APPLICABLE:
  235. sendButton.setText("N/A");
  236. sendButton.setEnabled(false);
  237. break;
  238. default:
  239. sendButton.setText("Send");
  240. sendButton.setEnabled(true);
  241. break;
  242. }
  243. }
  244. private void allowClose() {
  245. final ErrorReportStatus status = error.getReportStatus();
  246. restartButton.setEnabled(status.isTerminal());
  247. updateSendButtonText(status);
  248. countDownLatch.countDown();
  249. }
  250. }