1 package org
.argeo
.cms
.ui
.util
;
3 import java
.io
.PrintWriter
;
4 import java
.io
.StringWriter
;
5 import java
.io
.UnsupportedEncodingException
;
6 import java
.net
.URLEncoder
;
7 import java
.text
.SimpleDateFormat
;
10 import org
.apache
.commons
.io
.IOUtils
;
11 import org
.argeo
.cms
.swt
.CmsException
;
12 import org
.argeo
.cms
.swt
.CmsStyles
;
13 import org
.argeo
.cms
.swt
.CmsSwtUtils
;
14 import org
.eclipse
.rap
.rwt
.RWT
;
15 import org
.eclipse
.swt
.SWT
;
16 import org
.eclipse
.swt
.events
.MouseEvent
;
17 import org
.eclipse
.swt
.events
.MouseListener
;
18 import org
.eclipse
.swt
.events
.ShellAdapter
;
19 import org
.eclipse
.swt
.events
.ShellEvent
;
20 import org
.eclipse
.swt
.layout
.GridData
;
21 import org
.eclipse
.swt
.layout
.GridLayout
;
22 import org
.eclipse
.swt
.widgets
.Composite
;
23 import org
.eclipse
.swt
.widgets
.Control
;
24 import org
.eclipse
.swt
.widgets
.Label
;
25 import org
.eclipse
.swt
.widgets
.Shell
;
27 /** Shell displaying system notifications such as exceptions */
28 public class SystemNotifications
extends Shell
implements CmsStyles
,
30 private static final long serialVersionUID
= -8129377525216022683L;
32 private Control source
;
34 public SystemNotifications(Control source
) {
35 super(source
.getDisplay(), SWT
.NO_TRIM
| SWT
.BORDER
| SWT
.ON_TOP
);
36 setData(RWT
.CUSTOM_VARIANT
, CMS_USER_MENU
);
41 // setLocation(source.toDisplay(source.getSize().x - getSize().x,
42 // source.getSize().y));
43 setLayout(new GridLayout());
44 addMouseListener(this);
46 addShellListener(new ShellAdapter() {
47 private static final long serialVersionUID
= 5178980294808435833L;
50 public void shellDeactivated(ShellEvent e
) {
58 public void notifyException(Throwable exception
) {
59 Composite pane
= this;
61 Label lbl
= new Label(pane
, SWT
.NONE
);
62 lbl
.setText(exception
.getLocalizedMessage()
63 + (exception
instanceof CmsException ?
"" : "("
64 + exception
.getClass().getName() + ")") + "\n");
65 lbl
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
66 lbl
.addMouseListener(this);
67 if (exception
.getCause() != null)
68 appendCause(pane
, exception
.getCause());
70 StringBuilder mailToUrl
= new StringBuilder("mailto:?");
72 mailToUrl
.append("subject=").append(
75 + new SimpleDateFormat("yyyy-MM-dd hh:mm")
76 .format(new Date()), "UTF-8")
77 .replace("+", "%20"));
79 StringWriter sw
= new StringWriter();
80 exception
.printStackTrace(new PrintWriter(sw
));
81 IOUtils
.closeQuietly(sw
);
84 // http://stackoverflow.com/questions/4737841/urlencoder-not-able-to-translate-space-character
85 String encoded
= URLEncoder
.encode(sw
.toString(), "UTF-8").replace(
87 mailToUrl
.append("&body=").append(encoded
);
88 } catch (UnsupportedEncodingException e
) {
89 mailToUrl
.append("&body=").append("Could not encode: ")
90 .append(e
.getMessage());
92 Label mailTo
= new Label(pane
, SWT
.NONE
);
93 CmsSwtUtils
.markup(mailTo
);
94 mailTo
.setText("<a href=\"" + mailToUrl
+ "\">Send details</a>");
95 mailTo
.setLayoutData(new GridData(SWT
.END
, SWT
.FILL
, true, false));
100 setLocation(source
.toDisplay(source
.getSize().x
- getSize().x
,
101 source
.getSize().y
- getSize().y
));
105 private void appendCause(Composite parent
, Throwable e
) {
106 Label lbl
= new Label(parent
, SWT
.NONE
);
107 lbl
.setText(" caused by: " + e
.getLocalizedMessage() + " ("
108 + e
.getClass().getName() + ")" + "\n");
109 lbl
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
110 lbl
.addMouseListener(this);
111 if (e
.getCause() != null)
112 appendCause(parent
, e
.getCause());
116 public void mouseDoubleClick(MouseEvent e
) {
120 public void mouseDown(MouseEvent e
) {
126 public void mouseUp(MouseEvent e
) {