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.7KB

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