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.AboutAction;
|
---|
23 | import org.openstreetmap.josm.actions.ShowStatusReportAction;
|
---|
24 | import org.openstreetmap.josm.plugins.PluginHandler;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * An exception handler that asks the user to send a bug report.
|
---|
28 | *
|
---|
29 | * @author imi
|
---|
30 | */
|
---|
31 | public final class BugReportExceptionHandler implements Thread.UncaughtExceptionHandler {
|
---|
32 |
|
---|
33 | public void uncaughtException(Thread t, Throwable e) {
|
---|
34 | handleException(e);
|
---|
35 | }
|
---|
36 | public static void handleException(Throwable e) {
|
---|
37 | e.printStackTrace();
|
---|
38 | if (Main.parent != null) {
|
---|
39 | if (e instanceof OutOfMemoryError) {
|
---|
40 | // do not translate the string, as translation may raise an exception
|
---|
41 | JOptionPane.showMessageDialog(Main.parent, "JOSM is out of memory. " +
|
---|
42 | "Strange things may happen.\nPlease restart JOSM with the -Xmx###M option,\n" +
|
---|
43 | "where ### is the the number of MB assigned to JOSM (e.g. 256).\n" +
|
---|
44 | "Currently, " + Runtime.getRuntime().maxMemory()/1024/1024 + " MB are available to JOSM.");
|
---|
45 | return;
|
---|
46 | }
|
---|
47 |
|
---|
48 | if(PluginHandler.checkException(e))
|
---|
49 | return;
|
---|
50 |
|
---|
51 | Object[] options = new String[]{tr("Do nothing"), tr("Report Bug")};
|
---|
52 | int answer = JOptionPane.showOptionDialog(Main.parent, tr("An unexpected exception occurred.\n\n" +
|
---|
53 | "This is always a coding error. If you are running the latest\n" +
|
---|
54 | "version of JOSM, please consider being kind and file a bug report."),
|
---|
55 | tr("Unexpected Exception"), JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE,
|
---|
56 | null, options, options[0]);
|
---|
57 | if (answer == 1) {
|
---|
58 | try {
|
---|
59 | StringWriter stack = new StringWriter();
|
---|
60 | e.printStackTrace(new PrintWriter(stack));
|
---|
61 |
|
---|
62 | String text = ShowStatusReportAction.getReportHeader()
|
---|
63 | + stack.getBuffer().toString();
|
---|
64 |
|
---|
65 | JPanel p = new JPanel(new GridBagLayout());
|
---|
66 | p.add(new JLabel("<html>" + tr("Please report a ticket at {0}","http://josm.openstreetmap.de/newticket") +
|
---|
67 | "<br>" + tr("Include your steps to get to the error (as detailed as possible)!") +
|
---|
68 | "<br>" + tr("Try updating to the newest version of JOSM and all plugins before reporting a bug.") +
|
---|
69 | "<br>" + tr("Be sure to include the following information:") + "</html>"), GBC.eol());
|
---|
70 | try {
|
---|
71 | Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), new ClipboardOwner(){
|
---|
72 | public void lostOwnership(Clipboard clipboard, Transferable contents) {}
|
---|
73 | });
|
---|
74 | p.add(new JLabel(tr("(The text has already been copied to your clipboard.)")), GBC.eop());
|
---|
75 | }
|
---|
76 | catch (RuntimeException x) {}
|
---|
77 |
|
---|
78 | JTextArea info = new JTextArea(text, 20, 60);
|
---|
79 | info.setCaretPosition(0);
|
---|
80 | info.setEditable(false);
|
---|
81 | p.add(new JScrollPane(info), GBC.eop());
|
---|
82 |
|
---|
83 | JOptionPane.showMessageDialog(Main.parent, p);
|
---|
84 | } catch (Exception e1) {
|
---|
85 | e1.printStackTrace();
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|