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