-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathExceptionDialog.java
More file actions
100 lines (92 loc) · 3.85 KB
/
ExceptionDialog.java
File metadata and controls
100 lines (92 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package nodebox.client;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.io.PrintWriter;
import java.io.StringWriter;
public class ExceptionDialog extends JDialog implements ClipboardOwner {
private Throwable exception;
private String log;
public ExceptionDialog(Frame owner, Throwable exception) {
this(owner, exception, "");
}
public ExceptionDialog(Frame owner, Throwable exception, String extraMessage) {
super(owner, "Error", true);
Container container = getContentPane();
container.setLayout(new BorderLayout(0, 0));
JPanel innerPanel = new JPanel(new BorderLayout(10, 10));
innerPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
this.exception = exception;
JLabel messageLabel = new JLabel("<html><b>Error: </b>" + exception.getClass().getName() + ": " + exception.getMessage() + " " + extraMessage + "</html>");
JTextArea textArea = new JTextArea();
StringBuffer sb = new StringBuffer();
StringWriter sw = new StringWriter();
exception.printStackTrace(new PrintWriter(sw));
sb.append(sw.toString());
sb.append(exception.getMessage());
sb.append("\n");
for (StackTraceElement ste : exception.getStackTrace()) {
sb.append(ste.toString());
sb.append("\n");
}
Throwable cause = exception.getCause();
while (cause != null) {
sb.append("Caused by: \n");
sb.append(cause.toString());
sb.append("\n");
sb.append(cause.getMessage());
sb.append("\n");
for (StackTraceElement ste : cause.getStackTrace()) {
sb.append(ste.toString());
sb.append("\n");
}
cause = cause.getCause();
}
log = sb.toString();
textArea.setText(sb.toString());
textArea.setEditable(false);
textArea.setCaretPosition(0);
JScrollPane scrollPane = new JScrollPane(textArea);
innerPanel.add(messageLabel, BorderLayout.NORTH);
innerPanel.add(scrollPane, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 10, 10));
JButton quitButton = new JButton("Quit");
quitButton.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.exit(-1);
}
});
JButton copyButton = new JButton("Copy");
copyButton.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent actionEvent) {
Clipboard clipboard = getToolkit().getSystemClipboard();
StringSelection ss = new StringSelection(log);
clipboard.setContents(ss, ExceptionDialog.this);
}
});
JButton closeButton = new JButton("Close");
closeButton.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
buttonPanel.add(quitButton);
// Simple spacer hack
buttonPanel.add(new JLabel(" "));
buttonPanel.add(copyButton);
buttonPanel.add(new JLabel(" "));
buttonPanel.add(closeButton);
innerPanel.add(buttonPanel, BorderLayout.SOUTH);
container.add(innerPanel, BorderLayout.CENTER);
setSize(600, 400);
Window win = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow();
SwingUtils.centerOnScreen(this, win);
}
public void lostOwnership(Clipboard clipboard, Transferable transferable) {
// Do nothing
}
}