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