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