1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.awt.Toolkit;
|
---|
8 | import java.awt.datatransfer.Clipboard;
|
---|
9 | import java.awt.datatransfer.ClipboardOwner;
|
---|
10 | import java.awt.datatransfer.StringSelection;
|
---|
11 | import java.awt.datatransfer.Transferable;
|
---|
12 | import java.io.PrintWriter;
|
---|
13 | import java.io.StringWriter;
|
---|
14 |
|
---|
15 | import javax.swing.JLabel;
|
---|
16 | import javax.swing.JOptionPane;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import javax.swing.JScrollPane;
|
---|
19 | import javax.swing.JTextArea;
|
---|
20 |
|
---|
21 | import org.openstreetmap.josm.Main;
|
---|
22 | import org.openstreetmap.josm.actions.ShowStatusReportAction;
|
---|
23 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
24 | import org.openstreetmap.josm.tools.Base64;
|
---|
25 |
|
---|
26 | import java.net.URL;
|
---|
27 | import java.net.URLEncoder;
|
---|
28 | import org.openstreetmap.josm.tools.UrlLabel;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * An exception handler that asks the user to send a bug report.
|
---|
32 | *
|
---|
33 | * @author imi
|
---|
34 | */
|
---|
35 | public final class BugReportExceptionHandler implements Thread.UncaughtExceptionHandler {
|
---|
36 |
|
---|
37 | public void uncaughtException(Thread t, Throwable e) {
|
---|
38 | handleException(e);
|
---|
39 | }
|
---|
40 | public static void handleException(Throwable e) {
|
---|
41 | e.printStackTrace();
|
---|
42 | if (Main.parent != null) {
|
---|
43 | if (e instanceof OutOfMemoryError) {
|
---|
44 | // do not translate the string, as translation may raise an exception
|
---|
45 | JOptionPane.showMessageDialog(Main.parent, "JOSM is out of memory. " +
|
---|
46 | "Strange things may happen.\nPlease restart JOSM with the -Xmx###M option,\n" +
|
---|
47 | "where ### is the the number of MB assigned to JOSM (e.g. 256).\n" +
|
---|
48 | "Currently, " + Runtime.getRuntime().maxMemory()/1024/1024 + " MB are available to JOSM.",
|
---|
49 | tr("Error"),
|
---|
50 | JOptionPane.ERROR_MESSAGE
|
---|
51 | );
|
---|
52 | return;
|
---|
53 | }
|
---|
54 |
|
---|
55 | if(PluginHandler.checkException(e))
|
---|
56 | return;
|
---|
57 |
|
---|
58 | Object[] options = new String[]{tr("Do nothing"), tr("Report Bug")};
|
---|
59 | int answer = JOptionPane.showOptionDialog(Main.parent, tr("An unexpected exception occurred.\n\n" +
|
---|
60 | "This is always a coding error. If you are running the latest\n" +
|
---|
61 | "version of JOSM, please consider being kind and file a bug report."),
|
---|
62 | tr("Unexpected Exception"), JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE,null,
|
---|
63 | options, options[0]);
|
---|
64 | if (answer == 1) {
|
---|
65 | try {
|
---|
66 | StringWriter stack = new StringWriter();
|
---|
67 | e.printStackTrace(new PrintWriter(stack));
|
---|
68 |
|
---|
69 | String text = ShowStatusReportAction.getReportHeader()
|
---|
70 | + stack.getBuffer().toString();
|
---|
71 |
|
---|
72 | URL url = new URL("http://josm.openstreetmap.de/josmticket?" +
|
---|
73 | "data="+
|
---|
74 | Base64.encode(
|
---|
75 | // To note that it came from this code
|
---|
76 | "keywords=template_report&" +
|
---|
77 | "description=" + java.net.URLEncoder.encode(
|
---|
78 | // Note: This doesn't use tr() intentionally, we want bug reports in English
|
---|
79 | "What steps will reproduce the problem?\n"
|
---|
80 | + " 1. \n"
|
---|
81 | + " 2. \n"
|
---|
82 | + " 3. \n"
|
---|
83 | + "\n"
|
---|
84 | + "What is the expected result?\n\n"
|
---|
85 | + "What happens instead?\n\n"
|
---|
86 | + "Please provide any additional information below. Attach a screenshot if\n"
|
---|
87 | + "possible.\n\n"
|
---|
88 | + "{{{\n" + text + "\n}}}\n",
|
---|
89 | "UTF-8")));
|
---|
90 |
|
---|
91 | JPanel p = new JPanel(new GridBagLayout());
|
---|
92 | p.add(new JLabel(tr("<html>" +
|
---|
93 | "<p>You've encountered an error in JOSM. Before you file a bug<br>" +
|
---|
94 | "make sure you've updated to the latest version of JOSM here:</p></html>")), GBC.eol());
|
---|
95 | p.add(new UrlLabel("http://josm.openstreetmap.de/#Download"), GBC.eop().insets(8,0,0,0));
|
---|
96 | p.add(new JLabel(tr("<html>You should also update your plugins. If neither of those help please<br>" +
|
---|
97 | "file a bug in our bugtracker using this link:</p></html>")), GBC.eol());
|
---|
98 | p.add(new UrlLabel(url.toString(), "http://josm.openstreetmap.de/josmticket?..."), GBC.eop().insets(8,0,0,0));
|
---|
99 | p.add(new JLabel(tr("<html><p>" +
|
---|
100 | "There the the error information provided below should already be<br>" +
|
---|
101 | "filled out for you. Please include information on how to reproduce<br>" +
|
---|
102 | "the error and try to supply as much detail as possible.</p></html>")), GBC.eop());
|
---|
103 | p.add(new JLabel(tr("<html><p>" +
|
---|
104 | "Alternatively if that doesn't work you can manually fill in the information<br>" +
|
---|
105 | "below at this URL:</p></html>")), GBC.eol());
|
---|
106 | p.add(new UrlLabel("http://josm.openstreetmap.de/newticket"), GBC.eop().insets(8,0,0,0));
|
---|
107 | try {
|
---|
108 | Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), new ClipboardOwner(){
|
---|
109 | public void lostOwnership(Clipboard clipboard, Transferable contents) {}
|
---|
110 | });
|
---|
111 | p.add(new JLabel(tr("(The text has already been copied to your clipboard.)")), GBC.eop());
|
---|
112 | }
|
---|
113 | catch (RuntimeException x) {}
|
---|
114 |
|
---|
115 | JTextArea info = new JTextArea(text, 20, 60);
|
---|
116 | info.setCaretPosition(0);
|
---|
117 | info.setEditable(false);
|
---|
118 | p.add(new JScrollPane(info), GBC.eop());
|
---|
119 |
|
---|
120 | JOptionPane.showMessageDialog(Main.parent, p, tr("You've encountered a bug in JOSM"), JOptionPane.ERROR_MESSAGE);
|
---|
121 | } catch (Exception e1) {
|
---|
122 | e1.printStackTrace();
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|
127 | }
|
---|