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