b5b44b103c88766874519400a6db0b309d9dc5dd
[debian/openrocket] / src / net / sf / openrocket / gui / dialogs / BugReportDialog.java
1 package net.sf.openrocket.gui.dialogs;
2
3 import java.awt.Desktop;
4 import java.awt.Dialog;
5 import java.awt.Dimension;
6 import java.awt.Window;
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.io.IOException;
10 import java.io.PrintWriter;
11 import java.io.StringWriter;
12 import java.io.UnsupportedEncodingException;
13 import java.net.URI;
14 import java.net.URISyntaxException;
15 import java.net.URLEncoder;
16 import java.util.SortedSet;
17 import java.util.TreeSet;
18
19 import javax.swing.JButton;
20 import javax.swing.JDialog;
21 import javax.swing.JLabel;
22 import javax.swing.JOptionPane;
23 import javax.swing.JPanel;
24 import javax.swing.JScrollPane;
25 import javax.swing.JTextArea;
26
27 import net.miginfocom.swing.MigLayout;
28 import net.sf.openrocket.communication.BugReporter;
29 import net.sf.openrocket.gui.components.SelectableLabel;
30 import net.sf.openrocket.gui.components.StyledLabel;
31 import net.sf.openrocket.util.GUIUtil;
32 import net.sf.openrocket.util.JarUtil;
33 import net.sf.openrocket.util.Prefs;
34
35 public class BugReportDialog extends JDialog {
36         
37         private static final String REPORT_EMAIL = "openrocket-bugs@lists.sourceforge.net";
38         
39
40         public BugReportDialog(Window parent, String labelText, String message) {
41                 super(parent, "Bug report", Dialog.ModalityType.APPLICATION_MODAL);
42                 
43                 JPanel panel = new JPanel(new MigLayout("fill"));
44                 
45                 // Some fscking Swing bug that makes html labels initially way too high
46                 JLabel label = new JLabel(labelText);
47                 Dimension d = label.getPreferredSize();
48                 d.width = 100000;
49                 label.setMaximumSize(d);
50                 panel.add(label, "gapleft para, wrap para");
51
52                 label = new JLabel("<html>If connected to the Internet, you can simply click " +
53                 "<em>Send bug report</em>.");
54                 d = label.getPreferredSize();
55                 d.width = 100000;
56                 label.setMaximumSize(d);
57                 panel.add(label, "gapleft para, wrap");
58                 
59                 panel.add(new JLabel("Otherwise, send the text below to the address: "), 
60                                 "gapleft para, split 2, gapright rel");
61                 panel.add(new SelectableLabel(REPORT_EMAIL), "growx, wrap para");
62                 
63
64                 final JTextArea textArea = new JTextArea(message, 20, 70);
65                 textArea.setEditable(true);
66                 panel.add(new JScrollPane(textArea), "grow, wrap");
67                 
68                 
69                 panel.add(new StyledLabel("The information above may be included in a public " +
70                                 "bug report.  Make sure it does not contain any sensitive information you " +
71                                 "do not want to be made public.", -1), "wrap para");
72                 
73                 
74                 
75                 ////  Close button
76                 JButton close = new JButton("Close");
77                 close.addActionListener(new ActionListener() {
78                         @Override
79                         public void actionPerformed(ActionEvent e) {
80                                 BugReportDialog.this.dispose();
81                         }
82                 });
83                 panel.add(close, "right, sizegroup buttons, split");
84                 
85                 
86                 ////  Mail button
87 //              if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Action.MAIL)) {
88 //                      JButton mail = new JButton("Open email");
89 //                      mail.setToolTipText("Open email client with the suitable email ready.");
90 //                      mail.addActionListener(new ActionListener() {
91 //                              @Override
92 //                              public void actionPerformed(ActionEvent e) {
93 //                                      String text = textArea.getText();
94 //                                      openEmail(text);
95 //                              }
96 //                      });
97 //                      panel.add(mail, "right, sizegroup buttons");
98 //              }
99                 
100                 
101                 ////  Send button
102                 JButton send = new JButton("Send bug report");
103                 send.setToolTipText("Automatically send the bug report to the OpenRocket developers.");
104                 send.addActionListener(new ActionListener() {
105                         @Override
106                         public void actionPerformed(ActionEvent e) {
107                                 String text = textArea.getText();
108                                 try {
109                                         
110                                         BugReporter.sendBugReport(text);
111
112                                         // Success if we came here
113                                         JOptionPane.showMessageDialog(BugReportDialog.this,
114                                                         new Object[] { "Bug report successfully sent.",
115                                                         "Thank you for helping make OpenRocket better!" },
116                                                         "Bug report sent", JOptionPane.INFORMATION_MESSAGE);
117                                         
118                                 } catch (Exception ex) {
119                                         // Sending the message failed.
120                                         JOptionPane.showMessageDialog(BugReportDialog.this,
121                                                         new Object[] { "OpenRocket was unable to send the bug report:",
122                                                         ex.getClass().getSimpleName() + ": " + ex.getMessage(), " ",
123                                                         "Please send the report manually to " + REPORT_EMAIL },
124                                                         "Error sending report", JOptionPane.ERROR_MESSAGE);
125                                 }
126                         }
127                 });
128                 panel.add(send, "right, sizegroup buttons");
129                 
130                 this.add(panel);
131                 
132                 this.validate();
133                 this.pack();
134                 this.pack();
135                 this.setLocationRelativeTo(parent);
136                 
137                 GUIUtil.setDisposableDialogOptions(this, send);
138         }
139
140         
141         
142         /**
143          * Show a general bug report dialog allowing the user to input information about
144          * the bug they encountered.
145          * 
146          * @param parent        the parent window (may be null).
147          */
148         public static void showBugReportDialog(Window parent) {
149
150                 StringBuilder sb = new StringBuilder();
151                 
152                 sb.append("---------- Bug report ----------\n");
153                 sb.append('\n');
154                 sb.append("Include detailed steps on how to trigger the bug:\n");
155                 sb.append('\n');
156                 sb.append("1. \n");
157                 sb.append("2. \n");
158                 sb.append("3. \n");
159                 sb.append('\n');
160                 
161                 sb.append("What does the software do and what in your opinion should it do in the " +
162                                 "case described above:\n");
163                 sb.append('\n');
164                 sb.append('\n');
165                 sb.append('\n');
166                 
167                 sb.append("Include your email address (optional; it helps if we can " +
168                 "contact you in case we need additional information):\n");
169                 sb.append('\n');
170                 sb.append('\n');
171                 sb.append('\n');
172                 
173
174                 sb.append("(Do not modify anything below this line.)\n");
175                 sb.append("---------- System information ----------\n");
176                 addSystemInformation(sb);
177                 sb.append("---------- End of information ----------\n");
178                 sb.append('\n');
179                 
180                 BugReportDialog reportDialog = 
181                         new BugReportDialog(parent,
182                                         "<html><b>You can report a bug in OpenRocket by filling in and submitting " +
183                                         "the form below.</b><br>" +
184                                         "You can also report bugs and include attachments on the project " +
185                                         "web site.", sb.toString());
186                 reportDialog.setVisible(true);
187         }
188         
189         
190         /**
191          * Show a dialog presented when an uncaught exception occurs.
192          * 
193          * @param parent        the parent window (may be null).
194          * @param t                     the thread that encountered the exception (may be null).
195          * @param e                     the exception.
196          */
197         public static void showExceptionDialog(Window parent, Thread t, Throwable e) {
198
199                 StringBuilder sb = new StringBuilder();
200                 
201                 sb.append("---------- Bug report ----------\n");
202                 sb.append('\n');
203                 sb.append("Please include a description about what actions you were " +
204                                 "performing when the exception occurred:\n");
205                 sb.append('\n');
206                 sb.append('\n');
207                 sb.append('\n');
208                 sb.append('\n');
209                 
210
211                 sb.append("Include your email address (optional; it helps if we can " +
212                                 "contact you in case we need additional information):\n");
213                 sb.append('\n');
214                 sb.append('\n');
215                 sb.append('\n');
216                 sb.append('\n');
217
218                 sb.append("(Do not modify anything below this line.)\n");
219                 sb.append("---------- Exception stack trace ----------\n");
220                 StringWriter sw = new StringWriter();
221                 PrintWriter pw = new PrintWriter(sw);
222                 e.printStackTrace(pw);
223                 sb.append(sw.getBuffer());
224                 sb.append('\n');
225                 
226                 
227                 sb.append("---------- Thread information ----------\n");
228                 if (t == null) {
229                         sb.append("Thread is not specified.");
230                 } else {
231                         sb.append(t + "\n");
232                 }
233                 sb.append('\n');
234                 
235                 
236                 sb.append("---------- System information ----------\n");
237                 addSystemInformation(sb);
238                 sb.append("---------- End of information ----------\n");
239                 sb.append('\n');
240                 
241                 BugReportDialog reportDialog = 
242                         new BugReportDialog(parent, "<html><b>Please include a short description about " +
243                                         "what you were doing when the exception occurred.</b>", sb.toString());
244                 reportDialog.setVisible(true);
245         }
246         
247         
248         private static void addSystemInformation(StringBuilder sb) {
249                 sb.append("OpenRocket version: " + Prefs.getVersion() + "\n");
250                 sb.append("OpenRocket source: " + Prefs.getBuildSource() + "\n");
251                 sb.append("OpenRocket location: " + JarUtil.getCurrentJarFile() + "\n");
252                 sb.append("System properties:\n");
253
254                 // Sort the keys
255                 SortedSet<String> keys = new TreeSet<String>();
256                 for (Object key: System.getProperties().keySet()) {
257                         keys.add((String)key);
258                 }
259                 
260                 for (String key: keys) {
261                         String value = System.getProperty(key);
262                         sb.append("  " + key + "=");
263                         if (key.equals("line.separator")) {
264                                 for (char c: value.toCharArray()) {
265                                         sb.append(String.format("\\u%04x", (int)c));
266                                 }
267                         } else {
268                                 sb.append(value);
269                         }
270                         sb.append('\n');
271                 }
272         }
273         
274         
275         
276         /**
277          * Open the default email client with the suitable bug report.
278          * Note that this does not work on some systems even if Desktop.isSupported()
279          * claims so.
280          * 
281          * @param text  the bug report text.
282          * @return              whether opening the client succeeded.
283          */
284         private boolean openEmail(String text) {
285                 String version;
286                 
287                 try {
288                         text = URLEncoder.encode(text, "UTF-8");
289                         version = URLEncoder.encode(Prefs.getVersion(), "UTF-8");
290                 } catch (UnsupportedEncodingException e) {
291                         throw new RuntimeException(e);
292                 }
293                 
294                 
295                 
296                 String mailto = "mailto:" + REPORT_EMAIL
297                         + "?subject=Bug%20report%20for%20OpenRocket%20" + version 
298                         + "?body=" + text;
299                 URI uri;
300                 try {
301                         uri = new URI(mailto);
302                 } catch (URISyntaxException e) {
303                         e.printStackTrace();
304                         return false;
305                 }
306                 
307                 Desktop desktop = Desktop.getDesktop();
308                 try {
309                         desktop.mail(uri);
310                 } catch (IOException e) {
311                         e.printStackTrace();
312                         return false;
313                 }
314                 
315                 return true;
316         }
317         
318 }