]> git.argeo.org Git - lgpl/argeo-commons.git/blob - swt/org.argeo.cms.swt/src/org/argeo/cms/swt/dialogs/CmsFeedback.java
Start improving single-user login
[lgpl/argeo-commons.git] / swt / org.argeo.cms.swt / src / org / argeo / cms / swt / dialogs / CmsFeedback.java
1 package org.argeo.cms.swt.dialogs;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.io.StringWriter;
6
7 import org.argeo.api.cms.CmsLog;
8 import org.argeo.cms.CmsMsg;
9 import org.argeo.cms.swt.Selected;
10 import org.argeo.cms.ux.widgets.CmsDialog;
11 import org.eclipse.swt.SWT;
12 import org.eclipse.swt.graphics.Point;
13 import org.eclipse.swt.layout.GridData;
14 import org.eclipse.swt.layout.GridLayout;
15 import org.eclipse.swt.widgets.Button;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Control;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.swt.widgets.Label;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.swt.widgets.Text;
22
23 /** A dialog feedback based on a {@link LightweightDialog}. */
24 public class CmsFeedback extends LightweightDialog {
25 private final static CmsLog log = CmsLog.getLog(CmsFeedback.class);
26
27 private String message;
28 private Throwable exception;
29
30 private Text stack;
31
32 private CmsFeedback(Shell parentShell, String message, Throwable e) {
33 super(parentShell);
34 this.message = message;
35 this.exception = e;
36 }
37
38 public static CmsFeedback error(String message, Throwable e) {
39 // rethrow ThreaDeath in order to make sure that RAP will properly clean
40 // up the UI thread
41 if (e instanceof ThreadDeath)
42 throw (ThreadDeath) e;
43
44 log.error(message, e);
45 try {
46 Display display = LightweightDialog.findDisplay();
47
48 CmsFeedback current = (CmsFeedback) display.getData(CmsFeedback.class.getName());
49 if (current != null) {// already one open
50 current.append("");
51 if (message != null)
52 current.append(message);
53 if (e != null)
54 current.append(e);
55 // FIXME set a limit to the size of the text
56 return current;
57 }
58
59 CmsFeedback cmsFeedback = new CmsFeedback(null, message, e);
60 cmsFeedback.setBlockOnOpen(false);
61 cmsFeedback.open();
62 cmsFeedback.getDisplay().setData(CmsFeedback.class.getName(), cmsFeedback);
63 return cmsFeedback;
64 } catch (Throwable e1) {
65 log.error("Cannot open error feedback (" + e.getMessage() + "), original error below", e);
66 return null;
67 }
68 }
69
70 public static CmsFeedback show(String message) {
71 CmsFeedback cmsFeedback = new CmsFeedback(null, message, null);
72 cmsFeedback.open();
73 return cmsFeedback;
74 }
75
76 protected Control createDialogArea(Composite parent) {
77 parent.setLayout(new GridLayout(2, false));
78
79 Label messageLbl = new Label(parent, SWT.WRAP);
80 messageLbl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
81 if (message != null)
82 messageLbl.setText(message);
83 else if (exception != null)
84 messageLbl.setText(exception.getLocalizedMessage());
85
86 Button close = new Button(parent, SWT.FLAT);
87 close.setText(CmsMsg.close.lead());
88 close.setLayoutData(new GridData(SWT.END, SWT.TOP, false, false));
89 close.addSelectionListener((Selected) (e) -> closeShell(CmsDialog.OK));
90
91 if (exception != null) {
92 stack = new Text(parent, SWT.MULTI | SWT.LEAD | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
93 stack.setEditable(false);
94 stack.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
95 append(exception);
96 }
97 return messageLbl;
98 }
99
100 protected Point getInitialSize() {
101 if (exception != null)
102 return new Point(800, 600);
103 else
104 return new Point(600, 400);
105 }
106
107 protected void append(String message) {
108 stack.append(message);
109 stack.append("\n");
110 }
111
112 protected void append(Throwable exception) {
113 try (StringWriter sw = new StringWriter()) {
114 exception.printStackTrace(new PrintWriter(sw));
115 stack.append(sw.toString());
116 } catch (IOException e) {
117 // ignore
118 }
119
120 }
121
122 @Override
123 protected void onClose() {
124 getDisplay().setData(CmsFeedback.class.getName(), null);
125 }
126
127 }