]>
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
.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
;
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);
26 private String message
;
27 private Throwable exception
;
31 private CmsFeedback(Shell parentShell
, String message
, Throwable e
) {
33 this.message
= message
;
37 public static CmsFeedback
error(String message
, Throwable e
) {
38 // rethrow ThreaDeath in order to make sure that RAP will properly clean
40 if (e
instanceof ThreadDeath
)
41 throw (ThreadDeath
) e
;
43 log
.error(message
, e
);
45 Display display
= LightweightDialog
.findDisplay();
47 CmsFeedback current
= (CmsFeedback
) display
.getData(CmsFeedback
.class.getName());
48 if (current
!= null) {// already one open
51 current
.append(message
);
54 // FIXME set a limit to the size of the text
58 CmsFeedback cmsFeedback
= new CmsFeedback(null, message
, e
);
59 cmsFeedback
.setBlockOnOpen(false);
61 cmsFeedback
.getDisplay().setData(CmsFeedback
.class.getName(), cmsFeedback
);
63 } catch (Throwable e1
) {
64 log
.error("Cannot open error feedback (" + e
.getMessage() + "), original error below", e
);
69 public static CmsFeedback
show(String message
) {
70 CmsFeedback cmsFeedback
= new CmsFeedback(null, message
, null);
75 protected Control
createDialogArea(Composite parent
) {
76 parent
.setLayout(new GridLayout(2, false));
78 Label messageLbl
= new Label(parent
, SWT
.WRAP
);
79 messageLbl
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
81 messageLbl
.setText(message
);
82 else if (exception
!= null)
83 messageLbl
.setText(exception
.getLocalizedMessage());
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
));
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));
99 protected Point
getInitialSize() {
100 if (exception
!= null)
101 return new Point(800, 600);
103 return new Point(600, 400);
106 protected void append(String message
) {
107 stack
.append(message
);
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
) {
122 protected void onClose() {
123 getDisplay().setData(CmsFeedback
.class.getName(), null);