]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/org.argeo.cms.swt/src/org/argeo/cms/swt/dialogs/CmsFeedback.java
Prepare next development cycle
[lgpl/argeo-commons.git] / eclipse / org.argeo.cms.swt / src / org / argeo / cms / swt / dialogs / CmsFeedback.java
1 package org.argeo.cms.swt.dialogs;
2
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5
6 import org.argeo.api.cms.CmsLog;
7 import org.argeo.cms.CmsMsg;
8 import org.argeo.cms.swt.Selected;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.layout.GridData;
11 import org.eclipse.swt.layout.GridLayout;
12 import org.eclipse.swt.widgets.Button;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Control;
15 import org.eclipse.swt.widgets.Label;
16 import org.eclipse.swt.widgets.Shell;
17 import org.eclipse.swt.widgets.Text;
18
19 /** A dialog feedback based on a {@link LightweightDialog}. */
20 public class CmsFeedback extends LightweightDialog {
21 private final static CmsLog log = CmsLog.getLog(CmsFeedback.class);
22
23 private String message;
24 private Throwable exception;
25
26 public CmsFeedback(Shell parentShell, String message, Throwable e) {
27 super(parentShell);
28 this.message = message;
29 this.exception = e;
30 log.error(message, e);
31 }
32
33 public static CmsFeedback 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 try {
40 CmsFeedback cmsFeedback = new CmsFeedback(null, message, e);
41 cmsFeedback.setBlockOnOpen(false);
42 cmsFeedback.open();
43 return cmsFeedback;
44 } catch (Throwable e1) {
45 log.error("Cannot open error feedback (" + e.getMessage() + "), original error below", e);
46 return null;
47 }
48 }
49
50 public static CmsFeedback show(String message) {
51 CmsFeedback cmsFeedback = new CmsFeedback(null, message, null);
52 cmsFeedback.open();
53 return cmsFeedback;
54 }
55
56 /** Tries to find a display */
57 // private static Display getDisplay() {
58 // try {
59 // Display display = Display.getCurrent();
60 // if (display != null)
61 // return display;
62 // else
63 // return Display.getDefault();
64 // } catch (Exception e) {
65 // return Display.getCurrent();
66 // }
67 // }
68
69 protected Control createDialogArea(Composite parent) {
70 parent.setLayout(new GridLayout(2, false));
71
72 Label messageLbl = new Label(parent, SWT.WRAP);
73 if (message != null)
74 messageLbl.setText(message);
75 else if (exception != null)
76 messageLbl.setText(exception.getLocalizedMessage());
77
78 Button close = new Button(parent, SWT.FLAT);
79 close.setText(CmsMsg.close.lead());
80 close.setLayoutData(new GridData(SWT.END, SWT.TOP, false, false));
81 close.addSelectionListener((Selected) (e) -> closeShell(OK));
82
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 if (exception != null) {
88 Text stack = new Text(parent, SWT.MULTI | SWT.LEAD | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
89 stack.setEditable(false);
90 stack.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
91 StringWriter sw = new StringWriter();
92 exception.printStackTrace(new PrintWriter(sw));
93 stack.setText(sw.toString());
94 }
95
96 // parent.pack();
97 return messageLbl;
98 }
99
100 }