]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/ErrorFeedback.java
Introduce ranking key.
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui / src / org / argeo / eclipse / ui / dialogs / ErrorFeedback.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 ErrorFeedback extends TitleAreaDialog {
22 private static final long serialVersionUID = -8918084784628179044L;
23
24 private final static Log log = LogFactory.getLog(ErrorFeedback.class);
25
26 private final String message;
27 private final Throwable exception;
28
29 public static void show(String message, Throwable e) {
30 // rethrow ThreaDeath in order to make sure that RAP will properly clean
31 // up the UI thread
32 if (e instanceof ThreadDeath)
33 throw (ThreadDeath) e;
34
35 new ErrorFeedback(newShell(), message, e).open();
36 }
37
38 public static void show(String message) {
39 new ErrorFeedback(newShell(), message, null).open();
40 }
41
42 private static Shell newShell() {
43 return new Shell(getDisplay(), SWT.NO_TRIM);
44 }
45
46 /** Tries to find a display */
47 private static Display getDisplay() {
48 try {
49 Display display = Display.getCurrent();
50 if (display != null)
51 return display;
52 else
53 return Display.getDefault();
54 } catch (Exception e) {
55 return Display.getCurrent();
56 }
57 }
58
59 public ErrorFeedback(Shell parentShell, String message, Throwable e) {
60 super(parentShell);
61 setShellStyle(SWT.NO_TRIM);
62 this.message = message;
63 this.exception = e;
64 log.error(message, e);
65 }
66
67 protected Point getInitialSize() {
68 if (exception != null)
69 return new Point(800, 600);
70 else
71 return new Point(400, 300);
72 }
73
74 @Override
75 protected Control createDialogArea(Composite parent) {
76 Composite dialogarea = (Composite) super.createDialogArea(parent);
77 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
78 Composite composite = new Composite(dialogarea, SWT.NONE);
79 composite.setLayout(new GridLayout(2, false));
80 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
81
82 setMessage(message != null ? message + (exception != null ? ": " + exception.getMessage() : "")
83 : exception != null ? exception.getMessage() : "Unkown Error", IMessageProvider.ERROR);
84
85 if (exception != null) {
86 Text stack = new Text(composite, SWT.MULTI | SWT.LEAD | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
87 stack.setEditable(false);
88 stack.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
89 StringWriter sw = new StringWriter();
90 exception.printStackTrace(new PrintWriter(sw));
91 stack.setText(sw.toString());
92 }
93
94 parent.pack();
95 return composite;
96 }
97
98 protected void configureShell(Shell shell) {
99 super.configureShell(shell);
100 shell.setText("Error");
101 }
102 }