]> git.argeo.org Git - lgpl/argeo-commons.git/blob - util/SystemNotifications.java
Prepare next development cycle
[lgpl/argeo-commons.git] / util / SystemNotifications.java
1 package org.argeo.cms.ui.util;
2
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;
8 import java.util.Date;
9
10 import org.apache.commons.io.IOUtils;
11 import org.argeo.cms.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;
26
27 /** Shell displaying system notifications such as exceptions */
28 public class SystemNotifications extends Shell implements CmsStyles,
29 MouseListener {
30 private static final long serialVersionUID = -8129377525216022683L;
31
32 private Control source;
33
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);
37
38 this.source = source;
39
40 // TODO UI
41 // setLocation(source.toDisplay(source.getSize().x - getSize().x,
42 // source.getSize().y));
43 setLayout(new GridLayout());
44 addMouseListener(this);
45
46 addShellListener(new ShellAdapter() {
47 private static final long serialVersionUID = 5178980294808435833L;
48
49 @Override
50 public void shellDeactivated(ShellEvent e) {
51 close();
52 dispose();
53 }
54 });
55
56 }
57
58 public void notifyException(Throwable exception) {
59 Composite pane = this;
60
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());
69
70 StringBuilder mailToUrl = new StringBuilder("mailto:?");
71 try {
72 mailToUrl.append("subject=").append(
73 URLEncoder.encode(
74 "Exception "
75 + new SimpleDateFormat("yyyy-MM-dd hh:mm")
76 .format(new Date()), "UTF-8")
77 .replace("+", "%20"));
78
79 StringWriter sw = new StringWriter();
80 exception.printStackTrace(new PrintWriter(sw));
81 IOUtils.closeQuietly(sw);
82
83 // see
84 // http://stackoverflow.com/questions/4737841/urlencoder-not-able-to-translate-space-character
85 String encoded = URLEncoder.encode(sw.toString(), "UTF-8").replace(
86 "+", "%20");
87 mailToUrl.append("&body=").append(encoded);
88 } catch (UnsupportedEncodingException e) {
89 mailToUrl.append("&body=").append("Could not encode: ")
90 .append(e.getMessage());
91 }
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));
96
97 pack();
98 layout();
99
100 setLocation(source.toDisplay(source.getSize().x - getSize().x,
101 source.getSize().y - getSize().y));
102 open();
103 }
104
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());
113 }
114
115 @Override
116 public void mouseDoubleClick(MouseEvent e) {
117 }
118
119 @Override
120 public void mouseDown(MouseEvent e) {
121 close();
122 dispose();
123 }
124
125 @Override
126 public void mouseUp(MouseEvent e) {
127 }
128
129 }