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