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