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