]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - swt/org.argeo.cms.swt/src/org/argeo/cms/swt/dialogs/CmsFeedback.java
1 package org
.argeo
.cms
.swt
.dialogs
;
3 import java
.io
.IOException
;
4 import java
.io
.PrintWriter
;
5 import java
.io
.StringWriter
;
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
;
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);
27 private String message
;
28 private Throwable exception
;
32 private CmsFeedback(Shell parentShell
, String message
, Throwable e
) {
34 this.message
= message
;
38 public static CmsFeedback
error(String message
, Throwable e
) {
39 // rethrow ThreaDeath in order to make sure that RAP will properly clean
41 if (e
instanceof ThreadDeath
)
42 throw (ThreadDeath
) e
;
44 log
.error(message
, e
);
46 Display display
= LightweightDialog
.findDisplay();
48 CmsFeedback current
= (CmsFeedback
) display
.getData(CmsFeedback
.class.getName());
49 if (current
!= null) {// already one open
52 current
.append(message
);
55 // FIXME set a limit to the size of the text
59 CmsFeedback cmsFeedback
= new CmsFeedback(null, message
, e
);
60 cmsFeedback
.setBlockOnOpen(false);
62 cmsFeedback
.getDisplay().setData(CmsFeedback
.class.getName(), cmsFeedback
);
64 } catch (Throwable e1
) {
65 log
.error("Cannot open error feedback (" + e
.getMessage() + "), original error below", e
);
70 public static CmsFeedback
show(String message
) {
71 CmsFeedback cmsFeedback
= new CmsFeedback(null, message
, null);
76 protected Control
createDialogArea(Composite parent
) {
77 parent
.setLayout(new GridLayout(2, false));
79 Label messageLbl
= new Label(parent
, SWT
.WRAP
);
80 messageLbl
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
82 messageLbl
.setText(message
);
83 else if (exception
!= null)
84 messageLbl
.setText(exception
.getLocalizedMessage());
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
));
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));
100 protected Point
getInitialSize() {
101 if (exception
!= null)
102 return new Point(800, 600);
104 return new Point(600, 400);
107 protected void append(String message
) {
108 stack
.append(message
);
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
) {
123 protected void onClose() {
124 getDisplay().setData(CmsFeedback
.class.getName(), null);