Debian Codegraph @

source/org/jfree/report/modules/gui/swing/common/ExceptionDialog.java

raw
1 /**
2 * ========================================
3 * JFreeReport : a free Java report library
4 * ========================================
5 *
6 * Project Info: http://reporting.pentaho.org/
7 *
8 * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
9 *
10 * This library is free software; you can redistribute it and/or modify it under the terms
11 * of the GNU Lesser General Public License as published by the Free Software Foundation;
12 * either version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License along with this
19 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
21 *
22 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
23 * in the United States and other countries.]
24 *
25 * ------------
26 * $Id: ExceptionDialog.java 10756 2009-12-02 15:58:24Z tmorgner $
27 * ------------
28 * (C) Copyright 2000-2005, by Object Refinery Limited.
29 * (C) Copyright 2005-2007, by Pentaho Corporation.
30 */
31
32 package org.jfree.report.modules.gui.swing.common;
33
34 import java.awt.Color;
35 import java.awt.Dimension;
36 import java.awt.FlowLayout;
37 import java.awt.GridBagConstraints;
38 import java.awt.GridBagLayout;
39 import java.awt.event.ActionEvent;
40 import java.io.PrintWriter;
41 import java.io.StringWriter;
42 import javax.swing.AbstractAction;
43 import javax.swing.BorderFactory;
44 import javax.swing.JButton;
45 import javax.swing.JDialog;
46 import javax.swing.JLabel;
47 import javax.swing.JPanel;
48 import javax.swing.JScrollPane;
49 import javax.swing.JTextArea;
50 import javax.swing.UIManager;
51
52 import org.pentaho.reporting.libraries.base.util.DebugLog;
53 import org.jfree.report.modules.gui.swing.common.action.ActionButton;
54
55 /**
56 * The exception dialog is used to display an exception and the exceptions stacktrace to
57 * the user.
58 *
59 * @author Thomas Morgner
60 */
61 public class ExceptionDialog extends JDialog
62 {
63 /**
64 * OK action.
65 */
66 private final class OKAction extends AbstractAction
67 {
68 /**
69 * Default constructor.
70 */
71 private OKAction ()
72 {
73 putValue(NAME, UIManager.getDefaults().getString("OptionPane.okButtonText"));
74 }
75
76 /**
77 * Receives notification that an action event has occurred.
78 *
79 * @param event the action event.
80 */
81 public void actionPerformed (final ActionEvent event)
82 {
83 setVisible(false);
84 }
85 }
86
87 /**
88 * Details action.
89 */
90 private final class DetailsAction extends AbstractAction
91 {
92 /**
93 * Default constructor.
94 */
95 private DetailsAction ()
96 {
97 putValue(NAME, ">>");
98 }
99
100 /**
101 * Receives notification that an action event has occurred.
102 *
103 * @param event the action event.
104 */
105 public void actionPerformed (final ActionEvent event)
106 {
107 setScrollerVisible(!(isScrollerVisible()));
108 if (isScrollerVisible())
109 {
110 putValue(NAME, "<<");
111 }
112 else
113 {
114 putValue(NAME, ">>");
115 }
116 adjustSize();
117 }
118 }
119
120 /**
121 * A UI component for displaying the stack trace.
122 */
123 private final JTextArea backtraceArea;
124
125 /**
126 * A UI component for displaying the message.
127 */
128 private final JLabel messageLabel;
129
130 /**
131 * The exception.
132 */
133 private Exception currentEx;
134
135 /**
136 * An action associated with the 'Details' button.
137 */
138 private DetailsAction detailsAction;
139
140 /**
141 * A scroll pane.
142 */
143 private final JScrollPane scroller;
144
145 /**
146 * A filler panel.
147 */
148 private final JPanel filler;
149
150 /**
151 * The default dialog.
152 */
153 private static ExceptionDialog defaultDialog;
154
155 /**
156 * Creates a new ExceptionDialog.
157 */
158 public ExceptionDialog ()
159 {
160 setModal(true);
161 detailsAction = new DetailsAction();
162
163 messageLabel = new JLabel();
164 backtraceArea = new JTextArea();
165
166 scroller = new JScrollPane(backtraceArea);
167 scroller.setVisible(false);
168
169 final JPanel detailPane = new JPanel();
170 detailPane.setLayout(new GridBagLayout());
171 GridBagConstraints gbc = new GridBagConstraints();
172 gbc.anchor = GridBagConstraints.CENTER;
173 gbc.fill = GridBagConstraints.NONE;
174 gbc.weightx = 0;
175 gbc.weighty = 0;
176 gbc.gridx = 0;
177 gbc.gridy = 0;
178 final JLabel icon = new JLabel(UIManager.getDefaults().getIcon("OptionPane.errorIcon"));
179 icon.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
180 detailPane.add(icon, gbc);
181
182 gbc = new GridBagConstraints();
183 gbc.anchor = GridBagConstraints.WEST;
184 gbc.fill = GridBagConstraints.NONE;
185 gbc.weightx = 1;
186 gbc.weighty = 1;
187 gbc.gridx = 1;
188 gbc.gridy = 0;
189 detailPane.add(messageLabel);
190
191 gbc = new GridBagConstraints();
192 gbc.anchor = GridBagConstraints.SOUTH;
193 gbc.fill = GridBagConstraints.HORIZONTAL;
194 gbc.weightx = 0;
195 gbc.weighty = 0;
196 gbc.gridx = 0;
197 gbc.gridy = 2;
198 gbc.gridwidth = 2;
199 detailPane.add(createButtonPane(), gbc);
200
201 filler = new JPanel();
202 filler.setPreferredSize(new Dimension(0, 0));
203 filler.setBackground(Color.green);
204 gbc = new GridBagConstraints();
205 gbc.anchor = GridBagConstraints.NORTH;
206 gbc.fill = GridBagConstraints.HORIZONTAL;
207 gbc.weightx = 1;
208 gbc.weighty = 5;
209 gbc.gridx = 0;
210 gbc.gridy = 3;
211 gbc.gridwidth = 2;
212 detailPane.add(filler, gbc);
213
214 gbc = new GridBagConstraints();
215 gbc.anchor = GridBagConstraints.SOUTHWEST;
216 gbc.fill = GridBagConstraints.BOTH;
217 gbc.weightx = 1;
218 gbc.weighty = 5;
219 gbc.gridx = 0;
220 gbc.gridy = 4;
221 gbc.gridwidth = 2;
222 detailPane.add(scroller, gbc);
223
224 setContentPane(detailPane);
225 }
226
227 /**
228 * Adjusts the size of the dialog to fit the with of the contained message and
229 * stacktrace.
230 */
231 public void adjustSize ()
232 {
233 final Dimension scSize = scroller.getPreferredSize();
234 final Dimension cbase = filler.getPreferredSize();
235 cbase.width = Math.max(scSize.width, cbase.width);
236 cbase.height = 0;
237 filler.setMinimumSize(cbase);
238 pack();
239
240 }
241
242 /**
243 * Defines, whether the scroll pane of the exception stack trace area is visible.
244 *
245 * @param b true, if the scroller should be visible, false otherwise.
246 */
247 protected void setScrollerVisible (final boolean b)
248 {
249 scroller.setVisible(b);
250 }
251
252 /**
253 * Checks, whether the scroll pane of the exception stack trace area is visible.
254 *
255 * @return true, if the scroller is visible, false otherwise.
256 */
257 protected boolean isScrollerVisible ()
258 {
259 return scroller.isVisible();
260 }
261
262 /**
263 * Initializes the buttonpane.
264 *
265 * @return a panel containing the 'OK' and 'Details' buttons.
266 */
267 private JPanel createButtonPane ()
268 {
269 final JPanel buttonPane = new JPanel();
270 buttonPane.setLayout(new FlowLayout(2));
271 buttonPane.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
272 final OKAction okAction = new OKAction();
273
274 final JButton ok = new ActionButton(okAction);
275 final JButton details = new ActionButton(detailsAction);
276
277 buttonPane.add(ok);
278 buttonPane.add(details);
279 return buttonPane;
280 }
281
282 /**
283 * Sets the message for this exception dialog. The message is displayed on the main
284 * page.
285 *
286 * @param mesg the message.
287 */
288 public void setMessage (final String mesg)
289 {
290 messageLabel.setText(mesg);
291 }
292
293 /**
294 * Returns the message for this exception dialog. The message is displayed on the main
295 * page.
296 *
297 * @return the message.
298 */
299 public String getMessage ()
300 {
301 return messageLabel.getText();
302 }
303
304 /**
305 * Sets the exception for this dialog. If no exception is set, the "Detail" button is
306 * disabled and the stacktrace text cleared. Else the stacktraces text is read into the
307 * detail message area.
308 *
309 * @param e the exception.
310 */
311 public void setException (final Exception e)
312 {
313 currentEx = e;
314 if (e == null)
315 {
316 detailsAction.setEnabled(false);
317 backtraceArea.setText("");
318 }
319 else
320 {
321 backtraceArea.setText(readFromException(e));
322 }
323 }
324
325 /**
326 * Reads the stacktrace text from the exception.
327 *
328 * @param e the exception.
329 * @return the stack trace.
330 */
331 private String readFromException (final Exception e)
332 {
333 String text = "No backtrace available";
334 try
335 {
336 final StringWriter writer = new StringWriter();
337 final PrintWriter pwriter = new PrintWriter(writer);
338 e.printStackTrace(pwriter);
339 text = writer.toString();
340 writer.close();
341 }
342 catch (Exception ex)
343 {
344 DebugLog.log("ExceptionDialog: exception suppressed.");
345 }
346 return text;
347 }
348
349 /**
350 * Returns the exception that was the reason for this dialog to show up.
351 *
352 * @return the exception.
353 */
354 public Exception getException ()
355 {
356 return currentEx;
357 }
358
359 /**
360 * Shows an default dialog with the given message and title and the exceptions
361 * stacktrace in the detail area.
362 *
363 * @param title the title.
364 * @param message the message.
365 * @param e the exception.
366 */
367 public static void showExceptionDialog
368 (final String title, final String message, final Exception e)
369 {
370 if (defaultDialog == null)
371 {
372 defaultDialog = new ExceptionDialog();
373 }
374 if (e != null)
375 {
376 DebugLog.log("UserError", e);
377 }
378 defaultDialog.setTitle(title);
379 defaultDialog.setMessage(message);
380 defaultDialog.setException(e);
381 defaultDialog.adjustSize();
382 defaultDialog.setVisible(true);
383 }
384
385 }