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