]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/ErrorFeedback.java
Start working on pseudo Eclipse Forms in order to ease transition to
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui / src / org / argeo / eclipse / ui / dialogs / ErrorFeedback.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.eclipse.ui.dialogs;
17
18 import java.io.PrintWriter;
19 import java.io.StringWriter;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.eclipse.jface.dialogs.IMessageProvider;
24 import org.eclipse.jface.dialogs.TitleAreaDialog;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.graphics.Point;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Control;
31 import org.eclipse.swt.widgets.Display;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.swt.widgets.Text;
34
35 /** Generic error dialog to be used in try/catch blocks */
36 public class ErrorFeedback extends TitleAreaDialog {
37 private static final long serialVersionUID = -8918084784628179044L;
38
39 private final static Log log = LogFactory.getLog(ErrorFeedback.class);
40
41 private final String message;
42 private final Throwable exception;
43
44 public static void show(String message, Throwable e) {
45 // rethrow ThreaDeath in order to make sure that RAP will properly clean
46 // up the UI thread
47 if (e instanceof ThreadDeath)
48 throw (ThreadDeath) e;
49
50 new ErrorFeedback(newShell(), message, e).open();
51 }
52
53 public static void show(String message) {
54 new ErrorFeedback(newShell(), message, null).open();
55 }
56
57 private static Shell newShell() {
58 return new Shell(getDisplay(), SWT.NO_TRIM);
59 }
60
61 /** Tries to find a display */
62 private static Display getDisplay() {
63 try {
64 Display display = Display.getCurrent();
65 if (display != null)
66 return display;
67 else
68 return Display.getDefault();
69 } catch (Exception e) {
70 return Display.getCurrent();
71 }
72 }
73
74 public ErrorFeedback(Shell parentShell, String message, Throwable e) {
75 super(parentShell);
76 setShellStyle(SWT.NO_TRIM);
77 this.message = message;
78 this.exception = e;
79 log.error(message, e);
80 }
81
82 protected Point getInitialSize() {
83 if (exception != null)
84 return new Point(800, 600);
85 else
86 return new Point(400, 300);
87 }
88
89 @Override
90 protected Control createDialogArea(Composite parent) {
91 Composite dialogarea = (Composite) super.createDialogArea(parent);
92 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
93 Composite composite = new Composite(dialogarea, SWT.NONE);
94 composite.setLayout(new GridLayout(2, false));
95 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
96
97 setMessage(message != null ? message + (exception != null ? ": " + exception.getMessage() : "")
98 : exception != null ? exception.getMessage() : "Unkown Error", IMessageProvider.ERROR);
99
100 if (exception != null) {
101 Text stack = new Text(composite, SWT.MULTI | SWT.LEAD | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
102 stack.setEditable(false);
103 stack.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
104 StringWriter sw = new StringWriter();
105 exception.printStackTrace(new PrintWriter(sw));
106 stack.setText(sw.toString());
107 }
108
109 parent.pack();
110 return composite;
111 }
112
113 protected void configureShell(Shell shell) {
114 super.configureShell(shell);
115 shell.setText("Error");
116 }
117 }