Debian Codegraph @

netx/net/sourceforge/jnlp/splashscreen/parts/JEditorPaneBasedExceptionDialog.java

raw
1 /* JeditorPaneBasedExceptionDialog.java
2 Copyright (C) 2012 Red Hat, Inc.
3
4 This file is part of IcedTea.
5
6 IcedTea is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 IcedTea is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with IcedTea; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
37 package net.sourceforge.jnlp.splashscreen.parts;
38
39 import java.awt.Toolkit;
40 import java.awt.datatransfer.Clipboard;
41 import java.awt.datatransfer.StringSelection;
42 import java.awt.event.WindowEvent;
43 import java.text.DateFormat;
44 import java.util.Date;
45 import java.util.List;
46 import javax.swing.BorderFactory;
47 import javax.swing.GroupLayout;
48 import javax.swing.Icon;
49 import javax.swing.ImageIcon;
50 import javax.swing.JButton;
51 import javax.swing.JDialog;
52 import javax.swing.JEditorPane;
53 import javax.swing.JFrame;
54 import javax.swing.JLabel;
55 import javax.swing.JOptionPane;
56 import javax.swing.JPanel;
57 import javax.swing.JScrollPane;
58 import javax.swing.LayoutStyle;
59 import javax.swing.SwingConstants;
60 import javax.swing.WindowConstants;
61 import javax.swing.event.HyperlinkEvent;
62 import javax.swing.event.HyperlinkListener;
63 import net.sourceforge.jnlp.LaunchException;
64 import net.sourceforge.jnlp.about.AboutDialog;
65 import net.sourceforge.jnlp.runtime.Translator;
66 import net.sourceforge.jnlp.util.BasicExceptionDialog;
67 import net.sourceforge.jnlp.util.docprovider.TextsProvider;
68 import net.sourceforge.jnlp.util.logging.OutputController;
69 import net.sourceforge.swing.SwingUtils;
70
71 public class JEditorPaneBasedExceptionDialog extends JDialog implements HyperlinkListener {
72
73 // components
74 private JButton closeButton;
75 private JButton closeAndCopyButton;
76 private JButton homeButton;
77 private JButton aboutButton;
78 private JButton consoleButton;
79 private JButton cacheButton;
80 private JEditorPane htmlErrorAndHelpPanel;
81 private JLabel exceptionLabel;
82 private JLabel iconLabel;
83 private JPanel mainPanel;
84 private JPanel topPanel;
85 private JPanel bottomPanel;
86 private JScrollPane htmlPaneScroller;
87 // End of components declaration
88 private final String message;
89 private final Throwable exception;
90 private final Date shown;
91 private final String anotherInfo;
92
93 /** Creates new form JEditorPaneBasedExceptionDialog */
94 public JEditorPaneBasedExceptionDialog(java.awt.Frame parent, boolean modal, Throwable ex, InformationElement information, String anotherInfo) {
95 super(parent, modal);
96 shown = new Date();
97 initComponents();
98 htmlErrorAndHelpPanel.setContentType("text/html");
99 htmlErrorAndHelpPanel.setEditable(false);
100 this.anotherInfo=anotherInfo;
101 List<String> l = infoElementToList(information);
102 this.message = getText(ex, l, anotherInfo, shown);
103 this.exception = ex;
104 if (exception == null) {
105 closeAndCopyButton.setVisible(false);
106 }
107 htmlErrorAndHelpPanel.setText(message);
108 //htmlPaneScroller.getVerticalScrollBar().setValue(1);
109 htmlErrorAndHelpPanel.setCaretPosition(0);
110 try {
111 Icon icon = new ImageIcon(this.getClass().getResource("/net/sourceforge/jnlp/resources/warning.png"));
112 iconLabel.setIcon(icon);
113 } catch (Exception lex) {
114 OutputController.getLogger().log(OutputController.Level.ERROR_ALL, lex);
115 }
116 htmlErrorAndHelpPanel.addHyperlinkListener(this);
117 homeButton.setVisible(false);
118 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
119
120
121 }
122
123 static List<String> infoElementToList(InformationElement information) {
124 List<String> l = null;
125 if (information != null) {
126 l = information.getHeader();
127 InfoItem ii = information.getLongestDescriptionForSplash();
128 if (ii != null) {
129 l.add(ii.toNiceString());
130 }
131 }
132 return l;
133 }
134
135 private void initComponents() {
136
137 topPanel = new JPanel();
138 closeButton = new JButton();
139 closeAndCopyButton = new JButton();
140 mainPanel = new JPanel();
141 exceptionLabel = new JLabel();
142 iconLabel = new JLabel();
143 bottomPanel = new JPanel();
144 htmlPaneScroller = new JScrollPane();
145 htmlErrorAndHelpPanel = new JEditorPane();
146 homeButton = new JButton();
147 aboutButton = new JButton();
148 consoleButton = BasicExceptionDialog.getShowButton(JEditorPaneBasedExceptionDialog.this);
149 cacheButton = BasicExceptionDialog.getClearCacheButton(JEditorPaneBasedExceptionDialog.this);
150
151 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
152
153 closeButton.setText(Translator.R(InfoItem.SPLASH + "Close"));
154 closeButton.addActionListener(new java.awt.event.ActionListener() {
155
156 @Override
157 public void actionPerformed(java.awt.event.ActionEvent evt) {
158 closeWindowButtonActionPerformed(evt);
159 }
160 });
161
162 closeAndCopyButton.setText(Translator.R(InfoItem.SPLASH + "closewAndCopyException"));
163 closeAndCopyButton.addActionListener(new java.awt.event.ActionListener() {
164
165 @Override
166 public void actionPerformed(java.awt.event.ActionEvent evt) {
167 copyAndCloseButtonActionPerformed(evt);
168 }
169 });
170
171 GroupLayout jPanel2Layout = new GroupLayout(topPanel);
172 topPanel.setLayout(jPanel2Layout);
173 jPanel2Layout.setHorizontalGroup(
174 jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
175 .addGroup(jPanel2Layout.createSequentialGroup()
176 .addContainerGap()
177 .addComponent(closeButton)
178 .addContainerGap()
179 .addComponent(aboutButton)
180 .addContainerGap()
181 .addComponent(cacheButton)
182 .addContainerGap()
183 .addComponent(consoleButton)
184 .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 314, Short.MAX_VALUE)
185 .addComponent(closeAndCopyButton)
186 .addContainerGap()));
187 jPanel2Layout.setVerticalGroup(
188 jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
189 .addGroup(GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
190 .addContainerGap(24, Short.MAX_VALUE)
191 .addGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
192 .addComponent(closeButton)
193 .addComponent(aboutButton)
194 .addComponent(cacheButton)
195 .addComponent(consoleButton)
196 .addComponent(closeAndCopyButton))
197 .addContainerGap()));
198
199 exceptionLabel.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
200 exceptionLabel.setHorizontalAlignment(SwingConstants.CENTER);
201 exceptionLabel.setText(Translator.R(InfoItem.SPLASH + "exOccured"));
202
203 bottomPanel.setBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
204 bottomPanel.setLayout(new java.awt.BorderLayout());
205
206 htmlPaneScroller.setViewportView(htmlErrorAndHelpPanel);
207
208 bottomPanel.add(htmlPaneScroller, java.awt.BorderLayout.CENTER);
209
210 homeButton.setText(Translator.R(InfoItem.SPLASH + "Home"));
211 homeButton.addActionListener(new java.awt.event.ActionListener() {
212
213 @Override
214 public void actionPerformed(java.awt.event.ActionEvent evt) {
215 homeButtonActionPerformed(evt);
216 }
217 });
218
219
220 aboutButton.setText(Translator.R("AboutDialogueTabAbout"));
221 aboutButton.addActionListener(new java.awt.event.ActionListener() {
222
223 @Override
224 public void actionPerformed(java.awt.event.ActionEvent evt) {
225 try{
226 AboutDialog.display(true, TextsProvider.ITW_PLUGIN);
227 }catch(Exception ex){
228 OutputController.getLogger().log(OutputController.Level.ERROR_ALL, ex);
229 JOptionPane.showConfirmDialog(JEditorPaneBasedExceptionDialog.this, ex);
230 }
231 }
232 });
233
234 GroupLayout jPanel1Layout = new GroupLayout(mainPanel);
235 mainPanel.setLayout(jPanel1Layout);
236 jPanel1Layout.setHorizontalGroup(
237 jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(iconLabel, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE).addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(exceptionLabel, GroupLayout.DEFAULT_SIZE, 503, Short.MAX_VALUE).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(homeButton, GroupLayout.PREFERRED_SIZE, 101, GroupLayout.PREFERRED_SIZE).addContainerGap()).addComponent(bottomPanel, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 723, Short.MAX_VALUE));
238 jPanel1Layout.setVerticalGroup(
239 jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(iconLabel, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE).addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(exceptionLabel, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE).addComponent(homeButton, GroupLayout.PREFERRED_SIZE, 64, GroupLayout.PREFERRED_SIZE))).addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(bottomPanel, GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)));
240
241 GroupLayout layout = new GroupLayout(getContentPane());
242 getContentPane().setLayout(layout);
243 layout.setHorizontalGroup(
244 layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addGroup(layout.createParallelGroup(GroupLayout.Alignment.TRAILING).addComponent(mainPanel, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(topPanel, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)).addContainerGap()));
245 layout.setVerticalGroup(
246 layout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup().addContainerGap().addComponent(mainPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED).addComponent(topPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE).addContainerGap()));
247
248 pack();
249 }
250
251 private void copyAndCloseButtonActionPerformed(java.awt.event.ActionEvent evt) {
252 if (exception != null) {
253 try {
254 StringSelection data = new StringSelection(anotherInfo+"\n"+shown.toString()+"\n"+getExceptionStackTraceAsString(exception)+addPlainChain());
255 Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
256 clipboard.setContents(data, data);
257 } catch (Exception ex) {
258 OutputController.getLogger().log(OutputController.Level.ERROR_ALL, ex);
259 JOptionPane.showMessageDialog(this, Translator.R(InfoItem.SPLASH + "cantCopyEx"));
260 }
261 } else {
262 JOptionPane.showMessageDialog(this, Translator.R(InfoItem.SPLASH + "noExRecorded"));
263 }
264 close();
265 }
266
267 private void homeButtonActionPerformed(java.awt.event.ActionEvent evt) {
268 htmlErrorAndHelpPanel.setText(message);
269 homeButton.setVisible(false);
270 }
271
272 private void closeWindowButtonActionPerformed(java.awt.event.ActionEvent evt) {
273 close();
274 }
275
276 /**
277 * @param args the command line arguments
278 */
279 public static void main(String args[]) {
280 SwingUtils.invokeLater(new Runnable() {
281 public void run() {
282 Exception ex = new RuntimeException("dsgsfdg");
283 JEditorPaneBasedExceptionDialog dialog = new JEditorPaneBasedExceptionDialog(new JFrame(), true, ex, null, "uaaa: aaa\nwqdeweq:sdsds");
284 dialog.addWindowListener(new java.awt.event.WindowAdapter() {
285
286 public void windowClosing(java.awt.event.WindowEvent e) {
287 System.exit(0);
288 }
289 });
290 dialog.setVisible(true);
291 }
292 });
293 }
294
295 static String getText(Throwable ex, List<String> l, String anotherInfo,Date shown) {
296 StringBuilder s = new StringBuilder("<html><body>");
297 String info = "<p>"
298 + Translator.R(InfoItem.SPLASH + "mainL1", createLink())
299 + " </p> \n" +
300 "<p>"
301 + Translator.R(InfoItem.SPLASH + "mainL2", createLink())
302 + " </p> \n";
303 String t = "<p>"
304 + Translator.R(InfoItem.SPLASH + "mainL3")
305 + "</p> \n"
306 + info + formatListInfoList(l) + formatInfo(anotherInfo);
307 Object[] options = new String[2];
308 options[0] = Translator.R(InfoItem.SPLASH + "Close");
309 options[1] = Translator.R(InfoItem.SPLASH + "closeAndCopyShorter");
310 if (ex != null) {
311 t = "<p>"
312 + Translator.R(InfoItem.SPLASH + "mainL4")
313 + " </p>\n"
314 + info + formatListInfoList(l) + formatInfo(anotherInfo)
315 +"<br/>"+DateFormat.getInstance().format(shown)+"<br/>"
316 + "<p>"
317 + Translator.R(InfoItem.SPLASH + "exWas")
318 + " <br/>\n" + "<pre>" + getExceptionStackTraceAsString(ex) + "</pre>"
319 + addChain();
320
321
322 } else {
323 t += formatListInfoList(l);
324 }
325 s.append(t);
326 s.append("</body></html>");
327 return s.toString();
328 }
329
330 public static String getExceptionStackTraceAsString(Throwable exception) {
331 if (exception == null) {
332 return "";
333 }
334 return OutputController.exceptionToString(exception);
335 }
336
337 public static String[] getExceptionStackTraceAsStrings(Throwable exception) {
338 if (exception == null) {
339 return new String[0];
340 }
341 return OutputController.exceptionToString(exception).split("\n");
342 }
343
344 @Override
345 public void hyperlinkUpdate(HyperlinkEvent event) {
346 if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
347 try {
348 htmlErrorAndHelpPanel.setPage(event.getURL());
349 homeButton.setVisible(true);
350
351 } catch (Exception ioe) {
352 JOptionPane.showMessageDialog(this, Translator.R(InfoItem.SPLASH + "cfl") + " "
353 + event.getURL().toExternalForm() + ": " + ioe);
354 }
355 }
356 }
357
358 private void close() {
359 processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
360 }
361
362 static String formatListInfoList(List<String> l) {
363 if (l == null) {
364 return "";
365 }
366 StringBuilder sb = new StringBuilder();
367 sb.append("<p>");
368 sb.append("<h3>").
369 append(Translator.R(InfoItem.SPLASH + "vendorsInfo")).append(":</h3>");
370 sb.append("<pre>");
371 for (int i = 0; i < l.size(); i++) {
372 String string = l.get(i);
373 sb.append(string).append("\n");
374 }
375 sb.append("</pre>");
376 sb.append("</p>");
377 return sb.toString();
378 }
379
380 static String formatInfo(String l) {
381 if (l == null) {
382 return "";
383 }
384 StringBuilder sb = new StringBuilder();
385 sb.append("<p>");
386 sb.append("<h3>").
387 append(Translator.R(InfoItem.SPLASH + "anotherInfo")).append(": </h3>");
388 sb.append("<pre>");
389 sb.append(l);
390 sb.append("</pre>");
391 sb.append("</p>");
392 return sb.toString();
393 }
394
395 Throwable getException() {
396 return exception;
397 }
398
399 String getMessage() {
400 return message;
401 }
402
403 private static String createLink() {
404 return "<a href=\"" + TextsProvider.ITW_BUGS + "\">" +
405 TextsProvider.ITW_HOME + "</a>";
406 }
407
408
409 private static String addChain() {
410 if (LaunchException.getLaunchExceptionChain().isEmpty()) {
411 return "";
412 }
413 return Translator.R(InfoItem.SPLASH + "chainWas")
414 + " <br/>\n" + "<pre>" + getChainAsString(true) + "</pre>";
415
416 }
417
418 private static String addPlainChain() {
419 if (LaunchException.getLaunchExceptionChain().isEmpty()) {
420 return "";
421 }
422 return "\n Chain: \n" + getChainAsString(false);
423
424 }
425
426 private static String getChainAsString(boolean formatTime) {
427 return getChainAsString(LaunchException.getLaunchExceptionChain(), formatTime);
428 }
429
430 private static String getChainAsString(List<LaunchException.LaunchExceptionWithStamp> launchExceptionChain, boolean formatTime) {
431 String s = "";
432 if (launchExceptionChain != null) {
433 for (int i = 0; i < launchExceptionChain.size(); i++) {
434 LaunchException.LaunchExceptionWithStamp launchException = launchExceptionChain.get(i);
435 s = s + (i+1) + ") at " + formatTime(launchException.getStamp(), formatTime) + "\n" + getExceptionStackTraceAsString(launchException.getEx());
436 }
437 }
438 return s;
439 }
440
441 private static String formatTime(Date dateTime, boolean formatTime) {
442 if (dateTime == null) {
443 return "unknown time";
444 }
445 if (formatTime) {
446 return DateFormat.getInstance().format(dateTime);
447 } else {
448 return dateTime.toString();
449 }
450 }
451 }