]> git.argeo.org Git - lgpl/argeo-commons.git/blob - dialogs/FeedbackDialog.java
Prepare next development cycle
[lgpl/argeo-commons.git] / dialogs / FeedbackDialog.java
1 package org.argeo.eclipse.ui.dialogs;
2
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5
6 import org.argeo.api.cms.CmsLog;
7 import org.argeo.eclipse.ui.EclipseUiException;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.events.ShellAdapter;
10 import org.eclipse.swt.events.ShellEvent;
11 import org.eclipse.swt.graphics.Point;
12 import org.eclipse.swt.graphics.Rectangle;
13 import org.eclipse.swt.layout.GridData;
14 import org.eclipse.swt.layout.GridLayout;
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;
21
22 /**
23 * Generic lightweight dialog, not based on JFace.
24 *
25 * @deprecated Use CMS dialogs instead.
26 */
27 @Deprecated
28 public class FeedbackDialog extends LightweightDialog {
29 private final static CmsLog log = CmsLog.getLog(FeedbackDialog.class);
30
31 private String message;
32 private Throwable exception;
33
34 // private Shell parentShell;
35 private Shell shell;
36
37 public static void show(String message, Throwable e) {
38 // rethrow ThreaDeath in order to make sure that RAP will properly clean
39 // up the UI thread
40 if (e instanceof ThreadDeath)
41 throw (ThreadDeath) e;
42
43 new FeedbackDialog(getDisplay().getActiveShell(), message, e).open();
44 }
45
46 public static void show(String message) {
47 new FeedbackDialog(getDisplay().getActiveShell(), message, null).open();
48 }
49
50 /** Tries to find a display */
51 private static Display getDisplay() {
52 try {
53 Display display = Display.getCurrent();
54 if (display != null)
55 return display;
56 else
57 return Display.getDefault();
58 } catch (Exception e) {
59 return Display.getCurrent();
60 }
61 }
62
63 public FeedbackDialog(Shell parentShell, String message, Throwable e) {
64 super(parentShell);
65 this.message = message;
66 this.exception = e;
67 log.error(message, e);
68 }
69
70 public int open() {
71 if (shell != null)
72 throw new EclipseUiException("There is already a shell");
73 shell = new Shell(getDisplay(), SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
74 shell.setLayout(new GridLayout());
75 // shell.setText("Error");
76 shell.setSize(getInitialSize());
77 createDialogArea(shell);
78 // shell.pack();
79 // shell.layout();
80
81 Rectangle shellBounds = Display.getCurrent().getBounds();// RAP
82 Point dialogSize = shell.getSize();
83 int x = shellBounds.x + (shellBounds.width - dialogSize.x) / 2;
84 int y = shellBounds.y + (shellBounds.height - dialogSize.y) / 2;
85 shell.setLocation(x, y);
86
87 shell.addShellListener(new ShellAdapter() {
88 private static final long serialVersionUID = -2701270481953688763L;
89
90 @Override
91 public void shellDeactivated(ShellEvent e) {
92 closeShell();
93 }
94 });
95
96 shell.open();
97 return OK;
98 }
99
100 protected void closeShell() {
101 shell.close();
102 shell.dispose();
103 shell = null;
104 }
105
106 protected Point getInitialSize() {
107 // if (exception != null)
108 // return new Point(800, 600);
109 // else
110 return new Point(400, 300);
111 }
112
113 protected Control createDialogArea(Composite parent) {
114 Composite dialogarea = new Composite(parent, SWT.NONE);
115 dialogarea.setLayout(new GridLayout());
116 // Composite dialogarea = (Composite) super.createDialogArea(parent);
117 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
118
119 Label messageLbl = new Label(dialogarea, SWT.NONE);
120 if (message != null)
121 messageLbl.setText(message);
122 else if (exception != null)
123 messageLbl.setText(exception.getLocalizedMessage());
124
125 Composite composite = new Composite(dialogarea, SWT.NONE);
126 composite.setLayout(new GridLayout(2, false));
127 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
128
129 if (exception != null) {
130 Text stack = new Text(composite, SWT.MULTI | SWT.LEAD | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
131 stack.setEditable(false);
132 stack.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
133 StringWriter sw = new StringWriter();
134 exception.printStackTrace(new PrintWriter(sw));
135 stack.setText(sw.toString());
136 }
137
138 // parent.pack();
139 return composite;
140 }
141 }