]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/ErrorFeedback.java
9a5482584b5e7576f37adbac5e3470da59f3ed74
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui.workbench / src / org / argeo / eclipse / ui / workbench / 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.workbench;
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 import org.eclipse.ui.PlatformUI;
35
36 /** Generic error dialog to be used in try/catch blocks */
37 public class ErrorFeedback extends TitleAreaDialog {
38 private static final long serialVersionUID = -8918084784628179044L;
39
40 private final static Log log = LogFactory.getLog(ErrorFeedback.class);
41
42 private final String message;
43 private final Throwable exception;
44
45 public static void show(String message, Throwable e) {
46 // rethrow ThreaDeath in order to make sure that RAP will properly clean
47 // up the UI thread
48 if (e instanceof ThreadDeath)
49 throw (ThreadDeath) e;
50
51 new ErrorFeedback(getDisplay().getActiveShell(), message, e).open();
52 }
53
54 public static void show(String message) {
55 new ErrorFeedback(getDisplay().getActiveShell(), message, null).open();
56 }
57
58 /** Tries to find a display */
59 private static Display getDisplay() {
60 try {
61 Display display = PlatformUI.getWorkbench().getDisplay();
62 if (display != null)
63 return display;
64 else
65 return Display.getDefault();
66 } catch (Exception e) {
67 return Display.getCurrent();
68 }
69 }
70
71 public ErrorFeedback(Shell parentShell, String message, Throwable e) {
72 super(parentShell);
73 this.message = message;
74 this.exception = e;
75 log.error(message, e);
76 }
77
78 protected Point getInitialSize() {
79 if (exception != null)
80 return new Point(800, 600);
81 else
82 return new Point(400, 300);
83 }
84
85 @Override
86 protected Control createDialogArea(Composite parent) {
87 Composite dialogarea = (Composite) super.createDialogArea(parent);
88 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
89 Composite composite = new Composite(dialogarea, SWT.NONE);
90 composite.setLayout(new GridLayout(2, false));
91 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
92
93 setMessage(message != null ? message
94 + (exception != null ? ": " + exception.getMessage() : "")
95 : exception != null ? exception.getMessage() : "Unkown Error",
96 IMessageProvider.ERROR);
97
98 if (exception != null) {
99 Text stack = new Text(composite, SWT.MULTI | SWT.LEAD | SWT.BORDER
100 | SWT.V_SCROLL | SWT.H_SCROLL);
101 stack.setEditable(false);
102 stack.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
103 StringWriter sw = new StringWriter();
104 exception.printStackTrace(new PrintWriter(sw));
105 stack.setText(sw.toString());
106 }
107
108 parent.pack();
109 return composite;
110 }
111
112 protected void configureShell(Shell shell) {
113 super.configureShell(shell);
114 shell.setText("Error");
115 }
116 }