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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Copyright (c) 2006-2014 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.ErrorListener;
  24. import com.dmdirc.logger.ErrorManager;
  25. import com.dmdirc.logger.ErrorReportStatus;
  26. import com.dmdirc.logger.ProgramError;
  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 javax.swing.BorderFactory;
  33. import javax.swing.Box;
  34. import javax.swing.BoxLayout;
  35. import javax.swing.ImageIcon;
  36. import javax.swing.JButton;
  37. import javax.swing.JDialog;
  38. import javax.swing.JLabel;
  39. import javax.swing.JPanel;
  40. import javax.swing.JScrollPane;
  41. import javax.swing.JTextArea;
  42. import javax.swing.JTextPane;
  43. import javax.swing.SwingUtilities;
  44. import javax.swing.SwingWorker;
  45. import javax.swing.WindowConstants;
  46. import javax.swing.text.DefaultStyledDocument;
  47. import javax.swing.text.SimpleAttributeSet;
  48. import javax.swing.text.StyleConstants;
  49. import javax.swing.text.StyledDocument;
  50. /**
  51. * The fatal error dialog is used to inform the user that a fatal error has occurred and to give them
  52. * a chance to quit or restart the client.
  53. */
  54. public final class FatalErrorDialog extends JDialog implements ActionListener,
  55. ErrorListener {
  56. /** Serialisation version ID. */
  57. private static final long serialVersionUID = 3;
  58. /** Do we need to restart? Else we quit. */
  59. private static boolean restart = true;
  60. /** Fatal error to be shown in this dialog. */
  61. private final ProgramError error;
  62. /** Restart client Button. */
  63. private JButton restartButton;
  64. /** Quit client button. */
  65. private JButton quitButton;
  66. /** Send error button. */
  67. private JButton sendButton;
  68. /** Info panel, informs the user what is happening. */
  69. private JTextPane infoLabel;
  70. /** Message label, contains details error information. */
  71. private JTextPane messageLabel;
  72. /** Fatal error icon. */
  73. private ImageIcon icon;
  74. /** Stack trace scroll pane. */
  75. private JScrollPane scrollPane;
  76. /** Are we waiting on user input? */
  77. private boolean waiting = true;
  78. /**
  79. * Creates a new fatal error dialog.
  80. *
  81. * @param error Error
  82. */
  83. public FatalErrorDialog(final ProgramError error) {
  84. super(null, Dialog.ModalityType.TOOLKIT_MODAL);
  85. setModal(true);
  86. this.error = error;
  87. initComponents();
  88. layoutComponents();
  89. try {
  90. ErrorManager.getErrorManager().addErrorListener(this);
  91. } catch (Exception e) {
  92. System.err.println("Error initialising error manager: ");
  93. e.printStackTrace();
  94. }
  95. setResizable(false);
  96. CoreUIUtils.centreWindow(this);
  97. }
  98. /**
  99. * Initialises the components for this dialog.
  100. */
  101. private void initComponents() {
  102. final JTextArea stacktraceField = new JTextArea();
  103. infoLabel = new JTextPane(new DefaultStyledDocument());
  104. infoLabel.setOpaque(false);
  105. infoLabel.setEditable(false);
  106. infoLabel.setHighlighter(null);
  107. messageLabel = new JTextPane(new DefaultStyledDocument());
  108. messageLabel.setOpaque(false);
  109. messageLabel.setEditable(false);
  110. messageLabel.setHighlighter(null);
  111. final SimpleAttributeSet sas = new SimpleAttributeSet();
  112. StyleConstants.setAlignment(sas, StyleConstants.ALIGN_JUSTIFIED);
  113. scrollPane = new JScrollPane();
  114. restartButton = new JButton();
  115. sendButton = new JButton();
  116. quitButton = new JButton();
  117. setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  118. setTitle("DMDirc: Fatal Error");
  119. infoLabel.setText("DMDirc has encountered a fatal error, and is "
  120. + "not able to recover. \nThe application will now terminate.");
  121. messageLabel.setText("Description: " + error.getMessage());
  122. ((StyledDocument) infoLabel.getDocument()).setParagraphAttributes(0,
  123. infoLabel.getText().length(), sas, false);
  124. ((StyledDocument) messageLabel.getDocument()).setParagraphAttributes(0,
  125. messageLabel.getText().length(), sas, false);
  126. icon = new ImageIcon(FatalErrorDialog.class.getResource("/com/dmdirc/res/error.png"));
  127. stacktraceField.setEditable(false);
  128. final String[] trace = error.getTrace();
  129. if (trace.length > 0) {
  130. for (String line : trace) {
  131. stacktraceField.append(line + "\n");
  132. }
  133. stacktraceField.setCaretPosition(0);
  134. }
  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. /**
  177. * Exits the program. {@inheritDoc}
  178. *
  179. * @param actionEvent Action event.
  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. new SwingWorker<Void, Void>() {
  188. /** {@inheritDoc} */
  189. @Override
  190. protected Void doInBackground() {
  191. ErrorManager.getErrorManager().sendError(error);
  192. return null;
  193. }
  194. }.execute();
  195. } else if (actionEvent.getSource() == quitButton) {
  196. restart = false;
  197. waiting = false;
  198. dispose();
  199. } else {
  200. waiting = false;
  201. dispose();
  202. }
  203. }
  204. /**
  205. * Returns the restart response of this dialog. This will default to true if the user is yet to
  206. * make a choice.
  207. *
  208. * @return Whether to restart after this error
  209. */
  210. public boolean getRestart() {
  211. return restart;
  212. }
  213. /**
  214. * Updates the send button with the specified status.
  215. *
  216. * @param status New error status
  217. */
  218. private void updateSendButtonText(final ErrorReportStatus status) {
  219. switch (status) {
  220. case WAITING:
  221. sendButton.setText("Send");
  222. sendButton.setEnabled(true);
  223. break;
  224. case QUEUED:
  225. sendButton.setText("Queued");
  226. sendButton.setEnabled(false);
  227. break;
  228. case SENDING:
  229. sendButton.setText("Sending");
  230. sendButton.setEnabled(false);
  231. break;
  232. case ERROR:
  233. sendButton.setText("Error, resend");
  234. sendButton.setEnabled(true);
  235. break;
  236. case FINISHED:
  237. sendButton.setText("Sent");
  238. sendButton.setEnabled(false);
  239. break;
  240. case NOT_APPLICABLE:
  241. sendButton.setText("N/A");
  242. sendButton.setEnabled(false);
  243. break;
  244. default:
  245. sendButton.setText("Send");
  246. sendButton.setEnabled(true);
  247. break;
  248. }
  249. }
  250. /** {@inheritDoc} */
  251. @Override
  252. public void errorAdded(final ProgramError error) {
  253. //Ignore
  254. }
  255. /** {@inheritDoc} */
  256. @Override
  257. public void errorDeleted(final ProgramError error) {
  258. //Ignore
  259. }
  260. /** {@inheritDoc} */
  261. @Override
  262. public void errorStatusChanged(final ProgramError error) {
  263. if (this.error.equals(error)) {
  264. final ErrorReportStatus status = error.getReportStatus();
  265. SwingUtilities.invokeLater(new Runnable() {
  266. /** {@inheritDoc} */
  267. @Override
  268. public void run() {
  269. restartButton.setEnabled(status.isTerminal());
  270. updateSendButtonText(status);
  271. }
  272. });
  273. }
  274. }
  275. /** {@inheritDoc} */
  276. @Override
  277. public boolean isReady() {
  278. //We're never ready
  279. return false;
  280. }
  281. /**
  282. * Are we waiting for user input. This will be true until the dialog has been disposed of.
  283. *
  284. * @return true if user has made no input
  285. */
  286. public boolean isWaiting() {
  287. return waiting;
  288. }
  289. }