]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/ui/dialogs/CmsFeedback.java
Keyring working for E4
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / dialogs / CmsFeedback.java
1 package org.argeo.cms.ui.dialogs;
2
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.argeo.eclipse.ui.Selected;
9 import org.argeo.eclipse.ui.dialogs.LightweightDialog;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.layout.GridData;
12 import org.eclipse.swt.layout.GridLayout;
13 import org.eclipse.swt.widgets.Button;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Control;
16 import org.eclipse.swt.widgets.Display;
17 import org.eclipse.swt.widgets.Label;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.swt.widgets.Text;
20
21 public class CmsFeedback extends LightweightDialog {
22 private final static Log log = LogFactory.getLog(CmsFeedback.class);
23
24 private String message;
25 private Throwable exception;
26
27 public CmsFeedback(Shell parentShell, String message, Throwable e) {
28 super(parentShell);
29 this.message = message;
30 this.exception = e;
31 log.error(message, e);
32 }
33
34 public static void show(String message, Throwable e) {
35 // rethrow ThreaDeath in order to make sure that RAP will properly clean
36 // up the UI thread
37 if (e instanceof ThreadDeath)
38 throw (ThreadDeath) e;
39
40 new CmsFeedback(getDisplay().getActiveShell(), message, e).open();
41 }
42
43 public static void show(String message) {
44 new CmsFeedback(getDisplay().getActiveShell(), message, null).open();
45 }
46
47 /** Tries to find a display */
48 private static Display getDisplay() {
49 try {
50 Display display = Display.getCurrent();
51 if (display != null)
52 return display;
53 else
54 return Display.getDefault();
55 } catch (Exception e) {
56 return Display.getCurrent();
57 }
58 }
59
60 protected Control createDialogArea(Composite parent) {
61 parent.setLayout(new GridLayout(2, false));
62
63 Label messageLbl = new Label(parent, SWT.WRAP);
64 if (message != null)
65 messageLbl.setText(message);
66 else if (exception != null)
67 messageLbl.setText(exception.getLocalizedMessage());
68
69 Button close = new Button(parent, SWT.FLAT);
70 close.setText("Close");
71 close.setLayoutData(new GridData(SWT.END, SWT.TOP, false, false));
72 close.addSelectionListener((Selected) (e) -> closeShell(OK));
73
74 // Composite composite = new Composite(dialogarea, SWT.NONE);
75 // composite.setLayout(new GridLayout(2, false));
76 // composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
77
78 if (exception != null) {
79 Text stack = new Text(parent, SWT.MULTI | SWT.LEAD | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
80 stack.setEditable(false);
81 stack.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
82 StringWriter sw = new StringWriter();
83 exception.printStackTrace(new PrintWriter(sw));
84 stack.setText(sw.toString());
85 }
86
87 // parent.pack();
88 return messageLbl;
89 }
90
91 }