Merged l10n branch to trunk
[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(trans.get("bugreport.dlg.connectedInternet"));
64                 d = label.getPreferredSize();
65                 d.width = 100000;
66                 label.setMaximumSize(d);
67                 panel.add(label, "gapleft para, wrap");
68                 
69                 //// Otherwise, send the text below to the address:
70                 panel.add(new JLabel(trans.get("bugreport.dlg.otherwise") +" "),
71                                 "gapleft para, split 2, gapright rel");
72                 panel.add(new SelectableLabel(REPORT_EMAIL), "growx, wrap para");
73                 
74
75                 final JTextArea textArea = new JTextArea(message, 20, 70);
76                 textArea.setEditable(true);
77                 panel.add(new JScrollPane(textArea), "grow, wrap");
78                 
79
80                 panel.add(new StyledLabel(trans.get("bugreport.lbl.Theinformation"), -1), "wrap para");
81                 
82
83
84                 ////Close button
85                 JButton close = new JButton(trans.get("dlg.but.close"));
86                 close.addActionListener(new ActionListener() {
87                         @Override
88                         public void actionPerformed(ActionEvent e) {
89                                 BugReportDialog.this.dispose();
90                         }
91                 });
92                 panel.add(close, "right, sizegroup buttons, split");
93                 
94
95                 ////  Mail button
96                 //              if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Action.MAIL)) {
97                 //                      JButton mail = new JButton("Open email");
98                 //                      mail.setToolTipText("Open email client with the suitable email ready.");
99                 //                      mail.addActionListener(new ActionListener() {
100                 //                              @Override
101                 //                              public void actionPerformed(ActionEvent e) {
102                 //                                      String text = textArea.getText();
103                 //                                      openEmail(text);
104                 //                              }
105                 //                      });
106                 //                      panel.add(mail, "right, sizegroup buttons");
107                 //              }
108                 
109
110                 ////  Send bug report button
111                 JButton send = new JButton(trans.get("bugreport.dlg.but.Sendbugreport"));
112                 ////  Automatically send the bug report to the OpenRocket developers.
113                 send.setToolTipText(trans.get("bugreport.dlg.but.Sendbugreport.Ttip"));
114                 send.addActionListener(new ActionListener() {
115                         @Override
116                         public void actionPerformed(ActionEvent e) {
117                                 String text = textArea.getText();
118                                 try {
119                                         
120                                         BugReporter.sendBugReport(text);
121                                         
122                                         // Success if we came here
123                                         //bugreport.dlg.successmsg
124                                         /*JOptionPane.showMessageDialog(BugReportDialog.this,
125                                                         new Object[] { "Bug report successfully sent.",
126                                                                         "Thank you for helping make OpenRocket better!" },
127                                                         "Bug report sent", JOptionPane.INFORMATION_MESSAGE);*/
128                                         JOptionPane.showMessageDialog(BugReportDialog.this,
129                                                         new Object[] { trans.get("bugreport.dlg.successmsg1"),
130                                                         trans.get("bugreport.dlg.successmsg2") },
131                                                         trans.get("bugreport.dlg.successmsg3"), JOptionPane.INFORMATION_MESSAGE);
132                                         
133                                 } catch (Exception ex) {
134                                         // Sending the message failed.
135                                         JOptionPane.showMessageDialog(BugReportDialog.this,
136                                                         //// OpenRocket was unable to send the bug report:
137                                                         new Object[] { trans.get("bugreport.dlg.failedmsg1"),
138                                                                         ex.getClass().getSimpleName() + ": " + ex.getMessage(), " ",
139                                                                         //// Please send the report manually to 
140                                                                         trans.get("bugreport.dlg.failedmsg2") +" " + REPORT_EMAIL },
141                                                                         //// Error sending report
142                                                                         trans.get("bugreport.dlg.failedmsg3"), 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 the form below.</b><br>You can also report bugs and include attachments on the project web site.
203                                                 trans.get("bugreport.reportDialog.txt"), sb.toString());
204                 reportDialog.setVisible(true);
205         }
206         
207         
208         /**
209          * Show a dialog presented when an uncaught exception occurs.
210          * 
211          * @param parent        the parent window (may be null).
212          * @param t                     the thread that encountered the exception (may be null).
213          * @param e                     the exception.
214          */
215         public static void showExceptionDialog(Window parent, Thread t, Throwable e) {
216                 StringBuilder sb = new StringBuilder();
217                 
218                 sb.append("---------- Bug report ----------\n");
219                 sb.append('\n');
220                 sb.append("Please include a description about what actions you were " +
221                                 "performing when the exception occurred:\n");
222                 sb.append('\n');
223                 sb.append('\n');
224                 sb.append('\n');
225                 sb.append('\n');
226                 
227
228                 sb.append("Include your email address (optional; it helps if we can " +
229                                 "contact you in case we need additional information):\n");
230                 sb.append('\n');
231                 sb.append('\n');
232                 sb.append('\n');
233                 sb.append('\n');
234                 
235                 sb.append("(Do not modify anything below this line.)\n");
236                 sb.append("---------- Exception stack trace ----------\n");
237                 StringWriter sw = new StringWriter();
238                 PrintWriter pw = new PrintWriter(sw);
239                 e.printStackTrace(pw);
240                 sb.append(sw.getBuffer());
241                 sb.append('\n');
242                 
243
244                 sb.append("---------- Thread information ----------\n");
245                 if (t == null) {
246                         sb.append("Thread is not specified.");
247                 } else {
248                         sb.append(t + "\n");
249                 }
250                 sb.append('\n');
251                 
252
253                 sb.append("---------- System information ----------\n");
254                 addSystemInformation(sb);
255                 sb.append("---------- Error log ----------\n");
256                 addErrorLog(sb);
257                 sb.append("---------- End of bug report ----------\n");
258                 sb.append('\n');
259                 
260                 BugReportDialog reportDialog =
261                         //// <html><b>Please include a short description about what you were doing when the exception occurred.</b>
262                                 new BugReportDialog(parent, trans.get("bugreport.reportDialog.txt2"), sb.toString());
263                 reportDialog.setVisible(true);
264         }
265         
266         
267         private static void addSystemInformation(StringBuilder sb) {
268                 sb.append("OpenRocket version: " + Prefs.getVersion() + "\n");
269                 sb.append("OpenRocket source: " + Prefs.getBuildSource() + "\n");
270                 sb.append("OpenRocket location: " + JarUtil.getCurrentJarFile() + "\n");
271                 sb.append("Current default locale: " + Locale.getDefault() + "\n");
272                 sb.append("System properties:\n");
273                 
274                 // Sort the keys
275                 SortedSet<String> keys = new TreeSet<String>();
276                 for (Object key : System.getProperties().keySet()) {
277                         keys.add((String) key);
278                 }
279                 
280                 for (String key : keys) {
281                         String value = System.getProperty(key);
282                         sb.append("  " + key + "=");
283                         if (key.equals("line.separator")) {
284                                 for (char c : value.toCharArray()) {
285                                         sb.append(String.format("\\u%04x", (int) c));
286                                 }
287                         } else {
288                                 sb.append(value);
289                         }
290                         sb.append('\n');
291                 }
292         }
293         
294         
295         private static void addErrorLog(StringBuilder sb) {
296                 LogLevelBufferLogger buffer = Application.getLogBuffer();
297                 List<LogLine> logs = buffer.getLogs();
298                 for (LogLine l : logs) {
299                         sb.append(l.toString()).append('\n');
300                 }
301         }
302         
303         
304
305         /**
306          * Open the default email client with the suitable bug report.
307          * Note that this does not work on some systems even if Desktop.isSupported()
308          * claims so.
309          * 
310          * @param text  the bug report text.
311          * @return              whether opening the client succeeded.
312          */
313         private boolean openEmail(String text) {
314                 String version;
315                 
316                 try {
317                         text = URLEncoder.encode(text, "UTF-8");
318                         version = URLEncoder.encode(Prefs.getVersion(), "UTF-8");
319                 } catch (UnsupportedEncodingException e) {
320                         throw new BugException(e);
321                 }
322                 
323
324
325                 String mailto = "mailto:" + REPORT_EMAIL
326                                 + "?subject=Bug%20report%20for%20OpenRocket%20" + version
327                                 + "?body=" + text;
328                 URI uri;
329                 try {
330                         uri = new URI(mailto);
331                 } catch (URISyntaxException e) {
332                         e.printStackTrace();
333                         return false;
334                 }
335                 
336                 Desktop desktop = Desktop.getDesktop();
337                 try {
338                         desktop.mail(uri);
339                 } catch (IOException e) {
340                         e.printStackTrace();
341                         return false;
342                 }
343                 
344                 return true;
345         }
346         
347 }