1 package org.argeo.cms.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.argeo.cms.CmsMsg;
9 import org.argeo.eclipse.ui.Selected;
10 import org.argeo.eclipse.ui.dialogs.LightweightDialog;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.layout.GridData;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Control;
17 import org.eclipse.swt.widgets.Label;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.swt.widgets.Text;
20
21 public class CmsFeedback extends LightweightDialog {
22 private final static Log log = LogFactory.getLog(CmsFeedback.class);
23
24 private String message;
25 private Throwable exception;
26
27 public CmsFeedback(Shell parentShell, String message, Throwable e) {
28 super(parentShell);
29 this.message = message;
30 this.exception = e;
31 log.error(message, e);
32 }
33
34 public static void show(String message, Throwable e) {
35
36
37 if (e instanceof ThreadDeath)
38 throw (ThreadDeath) e;
39
40 try {
41 CmsFeedback cmsFeedback = new CmsFeedback(null, message, e);
42 cmsFeedback.setBlockOnOpen(false);
43 cmsFeedback.open();
44 } catch (Throwable e1) {
45 log.error("Cannot open error feedback (" + e.getMessage() + "), original error below", e);
46 }
47 }
48
49 public static void show(String message) {
50 new CmsFeedback(null, message, null).open();
51 }
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 protected Control createDialogArea(Composite parent) {
67 parent.setLayout(new GridLayout(2, false));
68
69 Label messageLbl = new Label(parent, SWT.WRAP);
70 if (message != null)
71 messageLbl.setText(message);
72 else if (exception != null)
73 messageLbl.setText(exception.getLocalizedMessage());
74
75 Button close = new Button(parent, SWT.FLAT);
76 close.setText(CmsMsg.close.lead());
77 close.setLayoutData(new GridData(SWT.END, SWT.TOP, false, false));
78 close.addSelectionListener((Selected) (e) -> closeShell(OK));
79
80
81
82
83
84 if (exception != null) {
85 Text stack = new Text(parent, SWT.MULTI | SWT.LEAD | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
86 stack.setEditable(false);
87 stack.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
88 StringWriter sw = new StringWriter();
89 exception.printStackTrace(new PrintWriter(sw));
90 stack.setText(sw.toString());
91 }
92
93
94 return messageLbl;
95 }
96
97 }