]> git.argeo.org Git - lgpl/argeo-commons.git/blob - Error.java
96724adff9d3285165cb967001267ea19be0ff69
[lgpl/argeo-commons.git] / Error.java
1 package org.argeo.eclipse.ui;
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 import org.eclipse.ui.PlatformUI;
20
21 /** Generic error dialog to be used in try/catch blocks */
22 public class Error extends TitleAreaDialog {
23 private final static Log log = LogFactory.getLog(Error.class);
24
25 private final String message;
26 private final Throwable exception;
27
28 public static void show(String message, Throwable e) {
29 new Error(getDisplay().getActiveShell(), message, e).open();
30 }
31
32 public static void show(String message) {
33 new Error(getDisplay().getActiveShell(), message, null).open();
34 }
35
36 /** Tries to find a display */
37 private static Display getDisplay() {
38 try {
39 return PlatformUI.getWorkbench().getDisplay();
40 } catch (Exception e) {
41 return Display.getCurrent();
42 }
43 }
44
45 public Error(Shell parentShell, String message, Throwable e) {
46 super(parentShell);
47 this.message = message;
48 this.exception = e;
49 log.error(message, e);
50 }
51
52 protected Point getInitialSize() {
53 if (exception != null)
54 return new Point(800, 600);
55 else
56 return new Point(400, 300);
57 }
58
59 @Override
60 protected Control createDialogArea(Composite parent) {
61 Composite dialogarea = (Composite) super.createDialogArea(parent);
62 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
63 Composite composite = new Composite(dialogarea, SWT.NONE);
64 composite.setLayout(new GridLayout(2, false));
65 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
66
67 setMessage(message != null ? message
68 + (exception != null ? ": " + exception.getMessage() : "")
69 : exception != null ? exception.getMessage() : "Unkown Error",
70 IMessageProvider.ERROR);
71
72 if (exception != null) {
73 Text stack = new Text(composite, SWT.MULTI | SWT.LEAD | SWT.BORDER
74 | SWT.V_SCROLL | SWT.H_SCROLL);
75 stack.setEditable(false);
76 stack.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
77 StringWriter sw = new StringWriter();
78 exception.printStackTrace(new PrintWriter(sw));
79 stack.setText(sw.toString());
80 }
81
82 parent.pack();
83 return composite;
84 }
85
86 protected void configureShell(Shell shell) {
87 super.configureShell(shell);
88 shell.setText("Error");
89 }
90
91 }