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.

ErrorDialog.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (c) 2006-2007 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.dialogs;
  23. import java.awt.Dimension;
  24. import java.awt.GridBagConstraints;
  25. import java.awt.GridBagLayout;
  26. import java.awt.Insets;
  27. import java.awt.event.ActionEvent;
  28. import java.awt.event.ActionListener;
  29. import java.awt.event.WindowEvent;
  30. import java.awt.event.WindowListener;
  31. import java.io.DataOutputStream;
  32. import java.io.IOException;
  33. import java.io.UnsupportedEncodingException;
  34. import java.net.MalformedURLException;
  35. import java.net.URL;
  36. import java.net.URLConnection;
  37. import java.net.URLEncoder;
  38. import java.util.Arrays;
  39. import java.util.Timer;
  40. import java.util.TimerTask;
  41. import javax.swing.Box;
  42. import javax.swing.Icon;
  43. import javax.swing.JButton;
  44. import javax.swing.JCheckBox;
  45. import javax.swing.JDialog;
  46. import javax.swing.JLabel;
  47. import javax.swing.JScrollPane;
  48. import javax.swing.JTextArea;
  49. import javax.swing.WindowConstants;
  50. import com.dmdirc.logger.ErrorLevel;
  51. import com.dmdirc.ui.MainFrame;
  52. import com.dmdirc.ui.interfaces.StatusErrorNotifier;
  53. import static com.dmdirc.ui.UIUtilities.LARGE_BORDER;
  54. import static com.dmdirc.ui.UIUtilities.SMALL_BORDER;
  55. /**
  56. * The fatal error dialog is used to inform the user that a fatal error has
  57. * occured.
  58. * @author chris
  59. */
  60. public final class ErrorDialog extends JDialog implements ActionListener,
  61. StatusErrorNotifier, WindowListener {
  62. /**
  63. * A version number for this class. It should be changed whenever the class
  64. * structure is changed (or anything else that would prevent serialized
  65. * objects being unserialized with the new class).
  66. */
  67. private static final long serialVersionUID = 1;
  68. /** error level. */
  69. private final ErrorLevel level;
  70. /** icon. */
  71. private final Icon icon;
  72. /** message. */
  73. private final String message;
  74. /** trace. */
  75. private final String[] trace;
  76. /** button. */
  77. private JButton okButton;
  78. /** label. */
  79. private JLabel infoLabel;
  80. /** label. */
  81. private JLabel messageLabel;
  82. /** label. */
  83. private JButton showMore;
  84. /** checkbox. */
  85. private JCheckBox sendDataCheckbox;
  86. /** stack trace scroll pane. */
  87. private JScrollPane scrollPane;
  88. /**
  89. * Creates a new fatal error dialog.
  90. * @param newLevel Error level
  91. * @param newIcon Error icon
  92. * @param newMessage Error message
  93. * @param newTrace Error trace
  94. * @param autoSubmit whether to automatically submit the error
  95. */
  96. public ErrorDialog(final ErrorLevel newLevel, final Icon newIcon,
  97. final String newMessage, final String[] newTrace, final boolean autoSubmit) {
  98. super(MainFrame.getMainFrame(), newLevel == ErrorLevel.FATAL ? true : false);
  99. icon = newIcon;
  100. level = newLevel;
  101. message = newMessage;
  102. trace = new String[newTrace.length];
  103. System.arraycopy(newTrace, 0, trace, 0, newTrace.length);
  104. initComponents();
  105. layoutComponents();
  106. setLocationRelativeTo(MainFrame.getMainFrame());
  107. if (autoSubmit && level != ErrorLevel.FATAL) {
  108. new Timer().schedule(new TimerTask() {
  109. public void run() {
  110. sendData();
  111. }
  112. }, 1);
  113. }
  114. }
  115. /**
  116. * Initialises the components for this dialog.
  117. */
  118. private void initComponents() {
  119. final JTextArea stacktraceField = new JTextArea();
  120. messageLabel = new JLabel();
  121. infoLabel = new JLabel();
  122. showMore = new JButton();
  123. sendDataCheckbox = new JCheckBox();
  124. scrollPane = new JScrollPane();
  125. okButton = new JButton();
  126. setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  127. this.addWindowListener(this);
  128. setTitle("DMDirc: Error");
  129. if (level == ErrorLevel.FATAL) {
  130. infoLabel.setText("DMDirc has encountered " + level.toSentenceString()
  131. + ", it is unable to recover from this error and will terminate.");
  132. } else {
  133. infoLabel.setText("DMDirc has encountered " + level.toSentenceString() + ".");
  134. }
  135. infoLabel.setIcon(icon);
  136. messageLabel.setText("<html>Message: <br>" + message + "</html>");
  137. showMore.setText("Show details");
  138. showMore.addActionListener(this);
  139. stacktraceField.setColumns(20);
  140. stacktraceField.setEditable(false);
  141. stacktraceField.setRows(5);
  142. if (trace.length > 0) {
  143. for (String line : trace) {
  144. stacktraceField.append(line + "\n");
  145. }
  146. stacktraceField.setCaretPosition(0);
  147. }
  148. scrollPane.setViewportView(stacktraceField);
  149. scrollPane.setMinimumSize(new Dimension(600, 200));
  150. scrollPane.setPreferredSize(new Dimension(600, 200));
  151. scrollPane.setVisible(false);
  152. sendDataCheckbox.setText("Send error report to developers");
  153. sendDataCheckbox.setSelected(true);
  154. okButton.setText("OK");
  155. okButton.setPreferredSize(new Dimension(100, 25));
  156. okButton.addActionListener(this);
  157. }
  158. /**
  159. * lays the components out in the dialog.
  160. */
  161. private void layoutComponents() {
  162. final GridBagConstraints constraints = new GridBagConstraints();
  163. getContentPane().setLayout(new GridBagLayout());
  164. constraints.gridx = 0;
  165. constraints.gridy = 0;
  166. constraints.gridwidth = 3;
  167. constraints.weightx = 1.0;
  168. constraints.weighty = 0.0;
  169. constraints.fill = GridBagConstraints.HORIZONTAL;
  170. constraints.insets = new Insets(LARGE_BORDER, LARGE_BORDER,
  171. LARGE_BORDER, LARGE_BORDER);
  172. getContentPane().add(infoLabel, constraints);
  173. constraints.insets = new Insets(0, LARGE_BORDER, SMALL_BORDER,
  174. LARGE_BORDER);
  175. constraints.gridy = 1;
  176. getContentPane().add(messageLabel, constraints);
  177. if (trace.length > 0) {
  178. constraints.insets = new Insets(0, LARGE_BORDER, SMALL_BORDER,
  179. LARGE_BORDER);
  180. constraints.gridx = 0;
  181. constraints.gridy = 2;
  182. constraints.gridwidth = 3;
  183. getContentPane().add(showMore, constraints);
  184. constraints.insets = new Insets(0, LARGE_BORDER, 0, LARGE_BORDER);
  185. constraints.gridx = 0;
  186. constraints.gridwidth = 3;
  187. constraints.weightx = 1.0;
  188. constraints.weighty = 1.0;
  189. constraints.gridy = 3;
  190. constraints.fill = GridBagConstraints.BOTH;
  191. getContentPane().add(scrollPane, constraints);
  192. }
  193. constraints.insets = new Insets(0, LARGE_BORDER, SMALL_BORDER,
  194. LARGE_BORDER);
  195. constraints.gridx = 0;
  196. constraints.gridy = 4;
  197. constraints.weightx = 0.0;
  198. constraints.weighty = 0.0;
  199. constraints.gridwidth = 3;
  200. getContentPane().add(sendDataCheckbox, constraints);
  201. constraints.gridx = 0;
  202. constraints.gridy = 5;
  203. constraints.gridwidth = 1;
  204. constraints.fill = GridBagConstraints.HORIZONTAL;
  205. getContentPane().add(Box.createHorizontalGlue(), constraints);
  206. constraints.insets.set(LARGE_BORDER, LARGE_BORDER, LARGE_BORDER,
  207. LARGE_BORDER);
  208. constraints.gridx = 2;
  209. constraints.anchor = GridBagConstraints.EAST;
  210. constraints.fill = GridBagConstraints.NONE;
  211. getContentPane().add(okButton, constraints);
  212. resize();
  213. }
  214. /**
  215. * Exits the program. {@inheritDoc}
  216. */
  217. public void actionPerformed(final ActionEvent actionEvent) {
  218. if (actionEvent.getSource() == showMore) {
  219. if (showMore.getText().equals("Show details")) {
  220. scrollPane.setVisible(true);
  221. showMore.setText("Hide details");
  222. resize();
  223. setLocationRelativeTo(MainFrame.getMainFrame());
  224. } else {
  225. scrollPane.setVisible(false);
  226. showMore.setText("Show details");
  227. resize();
  228. setLocationRelativeTo(MainFrame.getMainFrame());
  229. }
  230. } else {
  231. if (sendDataCheckbox.isSelected() && sendDataCheckbox.isEnabled()) {
  232. new Timer().schedule(new TimerTask() {
  233. public void run() {
  234. sendData();
  235. }
  236. }, 1);
  237. }
  238. if (level == ErrorLevel.FATAL) {
  239. if (sendDataCheckbox.isSelected()) {
  240. MainFrame.getMainFrame().setVisible(false);
  241. } else {
  242. System.exit(-1);
  243. }
  244. }
  245. this.dispose();
  246. }
  247. }
  248. /** Called when the user clicks on the status notifier. */
  249. public void clickReceived() {
  250. this.setVisible(true);
  251. }
  252. /**
  253. * Sends an error report.
  254. */
  255. private void sendData() {
  256. URL url;
  257. URLConnection urlConn;
  258. DataOutputStream printout;
  259. try {
  260. url = new URL("http://www.dmdirc.com/error.php");
  261. urlConn = url.openConnection();
  262. urlConn.setDoInput(true);
  263. urlConn.setDoOutput(true);
  264. urlConn.setUseCaches(false);
  265. urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  266. printout = new DataOutputStream(urlConn.getOutputStream());
  267. final String content =
  268. "message=" + URLEncoder.encode(message, "UTF-8")
  269. + "&trace=" + URLEncoder.encode(Arrays.toString(trace), "UTF-8");
  270. printout.writeBytes(content);
  271. printout.flush();
  272. printout.close();
  273. } catch (MalformedURLException ex) {
  274. System.err.println("Malformed URL, unable to send error report.");
  275. } catch (UnsupportedEncodingException ex) {
  276. System.err.println("Unsupported exception, unable to send error report.");
  277. } catch (IOException ex) {
  278. System.err.println("IO Error, unable to send error report.");
  279. }
  280. if (level == ErrorLevel.FATAL) {
  281. System.exit(-1);
  282. }
  283. sendDataCheckbox.setSelected(true);
  284. sendDataCheckbox.setEnabled(false);
  285. sendDataCheckbox.setText("Error has been reported to the developers.");
  286. resize();
  287. }
  288. /** {@inheritDoc} */
  289. public void windowOpened(final WindowEvent e) {
  290. //ignore
  291. }
  292. /** {@inheritDoc} */
  293. public void windowClosing(final WindowEvent e) {
  294. if (level == ErrorLevel.FATAL) {
  295. if (sendDataCheckbox.isSelected()) {
  296. MainFrame.getMainFrame().setVisible(false);
  297. } else {
  298. System.exit(-1);
  299. }
  300. } else {
  301. this.dispose();
  302. }
  303. }
  304. /** {@inheritDoc} */
  305. public void windowClosed(final WindowEvent e) {
  306. //ignore
  307. }
  308. /** {@inheritDoc} */
  309. public void windowIconified(final WindowEvent e) {
  310. //ignore
  311. }
  312. /** {@inheritDoc} */
  313. public void windowDeiconified(final WindowEvent e) {
  314. //ignore
  315. }
  316. /** {@inheritDoc} */
  317. public void windowActivated(final WindowEvent e) {
  318. //ignore
  319. }
  320. /** {@inheritDoc} */
  321. public void windowDeactivated(final WindowEvent e) {
  322. //ignore
  323. }
  324. /** Synchronized method to pack the dialog. */
  325. private void resize() {
  326. pack();
  327. }
  328. }