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