Mercurial > hg > truffle
comparison graal/com.oracle.max.base/src/com/sun/max/gui/ThrowableDialog.java @ 3733:e233f5660da4
Added Java files from Maxine project.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Sat, 17 Dec 2011 19:59:18 +0100 |
parents | |
children | bc8527f3071c |
comparison
equal
deleted
inserted
replaced
3732:3e2e8b8abdaf | 3733:e233f5660da4 |
---|---|
1 /* | |
2 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.sun.max.gui; | |
24 | |
25 import java.awt.*; | |
26 import java.awt.event.*; | |
27 import java.io.*; | |
28 | |
29 import javax.swing.*; | |
30 import javax.swing.border.*; | |
31 | |
32 /** | |
33 * A dialog for displaying the details of an exception. Initially the dialog only shows the exception's | |
34 * {@linkplain Throwable#getMessage() message}. It includes a button for expanding the dialog to also show the stack | |
35 * trace. When the stack trace is shown, pressing the same button hides the stack trace. | |
36 */ | |
37 public final class ThrowableDialog extends JDialog { | |
38 | |
39 /** | |
40 * Creates a dialog to display the details of an exception and makes it visible. | |
41 * | |
42 * @param throwable the exception whose details are being displayed | |
43 * @param owner the {@code Frame} from which the dialog is displayed | |
44 * @param title the {@code String} to display in the dialog's title bar | |
45 */ | |
46 public static void show(Throwable throwable, Frame owner, String title) { | |
47 new ThrowableDialog(throwable, owner, title).setVisible(true); | |
48 } | |
49 | |
50 /** | |
51 * Creates a dialog to display the details of an exception and makes it visible on the | |
52 * {@linkplain SwingUtilities#invokeLater(Runnable) AWT dispatching thread}. | |
53 * | |
54 * @param throwable the exception whose details are being displayed | |
55 * @param owner the {@code Frame} from which the dialog is displayed | |
56 * @param title the {@code String} to display in the dialog's title bar | |
57 */ | |
58 public static void showLater(Throwable throwable, Frame owner, String title) { | |
59 final ThrowableDialog dialog = new ThrowableDialog(throwable, owner, title); | |
60 SwingUtilities.invokeLater(new Runnable() { | |
61 public void run() { | |
62 dialog.setVisible(true); | |
63 } | |
64 }); | |
65 } | |
66 | |
67 /** | |
68 * Creates a dialog to display the details of an exception. The dialog is not displayed by this method. | |
69 * | |
70 * @param throwable the exception whose details are being displayed | |
71 * @param owner the {@code Frame} from which the dialog is displayed | |
72 * @param title the {@code String} to display in the dialog's title bar | |
73 */ | |
74 private ThrowableDialog(Throwable throwable, Frame owner, String title) { | |
75 super(owner, title, false); | |
76 setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); | |
77 setAlwaysOnTop(true); | |
78 | |
79 final JToggleButton stackTraceButton = new JToggleButton("Show stack trace"); | |
80 final JButton closeButton = new JButton("Close"); | |
81 closeButton.addActionListener(new ActionListener() { | |
82 public void actionPerformed(ActionEvent e) { | |
83 setVisible(false); | |
84 dispose(); | |
85 } | |
86 }); | |
87 | |
88 final JPanel buttonsPanel = new JPanel(); | |
89 buttonsPanel.add(closeButton); | |
90 buttonsPanel.add(stackTraceButton); | |
91 | |
92 final Container mainPane = getContentPane(); | |
93 //mainPane.setLayout(new FlowLayout()); | |
94 mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.PAGE_AXIS)); | |
95 final JLabel message = new JLabel(throwable.getMessage()); | |
96 final JPanel messagePanel = new JPanel(); | |
97 messagePanel.add(message); | |
98 mainPane.add(messagePanel); | |
99 mainPane.add(buttonsPanel); | |
100 pack(); | |
101 | |
102 final Dimension dialogWithoutStackTracePreferredSize = getPreferredSize(); | |
103 | |
104 final JTextArea stackTraceText = new JTextArea(20, 40); | |
105 throwable.printStackTrace(new PrintWriter(new Writer() { | |
106 | |
107 @Override | |
108 public void close() throws IOException { | |
109 } | |
110 | |
111 @Override | |
112 public void flush() throws IOException { | |
113 } | |
114 | |
115 @Override | |
116 public void write(char[] cbuf, int off, int len) throws IOException { | |
117 stackTraceText.append(new String(cbuf, off, len)); | |
118 } | |
119 })); | |
120 | |
121 final JScrollPane stackTracePanel = new JScrollPane(stackTraceText, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); | |
122 final Dimension stackTraceTextPreferredSize = stackTraceText.getPreferredSize(); | |
123 stackTracePanel.setBorder(new TitledBorder("Stack trace")); | |
124 final Dimension stackTracePanelPreferredSize = new Dimension( | |
125 stackTraceTextPreferredSize.width + stackTracePanel.getVerticalScrollBar().getPreferredSize().width * 2, | |
126 stackTraceTextPreferredSize.height + stackTracePanel.getHorizontalScrollBar().getPreferredSize().height); | |
127 stackTracePanel.setPreferredSize(stackTracePanelPreferredSize); | |
128 stackTracePanel.setVisible(false); | |
129 | |
130 stackTraceButton.addActionListener(new ActionListener() { | |
131 public void actionPerformed(ActionEvent e) { | |
132 if (stackTraceButton.isSelected()) { | |
133 stackTraceButton.setText("Hide stack trace"); | |
134 stackTracePanel.setVisible(true); | |
135 setSize(new Dimension(Math.max(dialogWithoutStackTracePreferredSize.width, stackTracePanelPreferredSize.width), | |
136 dialogWithoutStackTracePreferredSize.height + Math.min(1000, stackTracePanelPreferredSize.height))); | |
137 } else { | |
138 stackTraceButton.setText("Show stack trace"); | |
139 stackTracePanel.setVisible(false); | |
140 setSize(dialogWithoutStackTracePreferredSize); | |
141 } | |
142 validate(); | |
143 } | |
144 }); | |
145 mainPane.add(stackTracePanel); | |
146 | |
147 setSize(dialogWithoutStackTracePreferredSize); | |
148 pack(); | |
149 setLocationRelativeTo(owner); | |
150 } | |
151 | |
152 // Test code | |
153 | |
154 public static void main(String[] args) { | |
155 try { | |
156 recurse(0); | |
157 } catch (RuntimeException runtimeException) { | |
158 new ThrowableDialog(runtimeException, null, "Runtime Exception"); | |
159 } | |
160 } | |
161 | |
162 static void recurse(int more) { | |
163 if (more > 345) { | |
164 throw new RuntimeException("This is a test. Repeat. This is a test."); | |
165 } | |
166 recurse(more + 1); | |
167 } | |
168 } |