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