]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/ErrorFeedback.java
692d8a4b9f3ae7b1a3388de4813d8aec3f9aecc4
[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 @SuppressWarnings("serial")
38 public class ErrorFeedback extends TitleAreaDialog {
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(getDisplay().getActiveShell(), message, e).open();
51 }
52
53 public static void show(String message) {
54 new ErrorFeedback(getDisplay().getActiveShell(), message, null).open();
55 }
56
57 /** Tries to find a display */
58 private static Display getDisplay() {
59 try {
60 Display display = PlatformUI.getWorkbench().getDisplay();
61 if (display != null)
62 return display;
63 else
64 return Display.getDefault();
65 } catch (Exception e) {
66 return Display.getCurrent();
67 }
68 }
69
70 public ErrorFeedback(Shell parentShell, String message, Throwable e) {
71 super(parentShell);
72 this.message = message;
73 this.exception = e;
74 log.error(message, e);
75 }
76
77 protected Point getInitialSize() {
78 if (exception != null)
79 return new Point(800, 600);
80 else
81 return new Point(400, 300);
82 }
83
84 @Override
85 protected Control createDialogArea(Composite parent) {
86 Composite dialogarea = (Composite) super.createDialogArea(parent);
87 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
88 Composite composite = new Composite(dialogarea, SWT.NONE);
89 composite.setLayout(new GridLayout(2, false));
90 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
91
92 setMessage(message != null ? message
93 + (exception != null ? ": " + exception.getMessage() : "")
94 : exception != null ? exception.getMessage() : "Unkown Error",
95 IMessageProvider.ERROR);
96
97 if (exception != null) {
98 Text stack = new Text(composite, SWT.MULTI | SWT.LEAD | SWT.BORDER
99 | SWT.V_SCROLL | SWT.H_SCROLL);
100 stack.setEditable(false);
101 stack.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
102 StringWriter sw = new StringWriter();
103 exception.printStackTrace(new PrintWriter(sw));
104 stack.setText(sw.toString());
105 }
106
107 parent.pack();
108 return composite;
109 }
110
111 protected void configureShell(Shell shell) {
112 super.configureShell(shell);
113 shell.setText("Error");
114 }
115 }