]> git.argeo.org Git - lgpl/argeo-commons.git/blob - Error.java
4e6716ecc41c56fae4d673d0c9117c3373e55ad6
[lgpl/argeo-commons.git] / Error.java
1 package org.argeo.eclipse.ui.dialogs;
2
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.eclipse.jface.dialogs.IMessageProvider;
9 import org.eclipse.jface.dialogs.TitleAreaDialog;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.graphics.Point;
12 import org.eclipse.swt.layout.GridData;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Control;
16 import org.eclipse.swt.widgets.Display;
17 import org.eclipse.swt.widgets.Shell;
18 import org.eclipse.swt.widgets.Text;
19
20 /** !Generic error dialog to be used in try/catch blocks*/
21 public class Error extends TitleAreaDialog {
22 private final static Log log = LogFactory.getLog(Error.class);
23
24 private final String message;
25 private final Throwable exception;
26
27 public static void show(String message, Throwable e) {
28 new Error(Display.getDefault().getActiveShell(), message, e).open();
29 }
30
31 public static void show(String message) {
32 new Error(Display.getDefault().getActiveShell(), message, null).open();
33 }
34
35 public Error(Shell parentShell, String message, Throwable e) {
36 super(parentShell);
37 this.message = message;
38 this.exception = e;
39 log.error(message, e);
40 }
41
42 protected Point getInitialSize() {
43 if (exception != null)
44 return new Point(800, 600);
45 else
46 return new Point(400, 300);
47 }
48
49 @Override
50 protected Control createDialogArea(Composite parent) {
51 Composite dialogarea = (Composite) super.createDialogArea(parent);
52 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
53 Composite composite = new Composite(dialogarea, SWT.NONE);
54 composite.setLayout(new GridLayout(2, false));
55 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
56
57 setMessage(message != null ? message
58 + (exception != null ? ": " + exception.getMessage() : "")
59 : exception != null ? exception.getMessage() : "Unkown Error",
60 IMessageProvider.ERROR);
61
62 if (exception != null) {
63 Text stack = new Text(composite, SWT.MULTI | SWT.LEAD
64 | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
65 stack.setEditable(false);
66 stack.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
67 StringWriter sw = new StringWriter();
68 exception.printStackTrace(new PrintWriter(sw));
69 stack.setText(sw.toString());
70 }
71
72 parent.pack();
73 return composite;
74 }
75
76 protected void configureShell(Shell shell) {
77 super.configureShell(shell);
78 shell.setText("Error");
79 }
80
81 }