]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/ErrorFeedback.java
2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.eclipse
.ui
.dialogs
;
18 import java
.io
.PrintWriter
;
19 import java
.io
.StringWriter
;
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
;
35 /** Generic error dialog to be used in try/catch blocks */
36 public class ErrorFeedback
extends TitleAreaDialog
{
37 private static final long serialVersionUID
= -8918084784628179044L;
39 private final static Log log
= LogFactory
.getLog(ErrorFeedback
.class);
41 private final String message
;
42 private final Throwable exception
;
44 public static void show(String message
, Throwable e
) {
45 // rethrow ThreaDeath in order to make sure that RAP will properly clean
47 if (e
instanceof ThreadDeath
)
48 throw (ThreadDeath
) e
;
50 new ErrorFeedback(getDisplay().getActiveShell(), message
, e
).open();
53 public static void show(String message
) {
54 new ErrorFeedback(getDisplay().getActiveShell(), message
, null).open();
57 /** Tries to find a display */
58 private static Display
getDisplay() {
60 Display display
= Display
.getCurrent();
64 return Display
.getDefault();
65 } catch (Exception e
) {
66 return Display
.getCurrent();
70 public ErrorFeedback(Shell parentShell
, String message
, Throwable e
) {
72 this.message
= message
;
74 log
.error(message
, e
);
77 protected Point
getInitialSize() {
78 if (exception
!= null)
79 return new Point(800, 600);
81 return new Point(400, 300);
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));
92 setMessage(message
!= null ? message
93 + (exception
!= null ?
": " + exception
.getMessage() : "")
94 : exception
!= null ? exception
.getMessage() : "Unkown Error",
95 IMessageProvider
.ERROR
);
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());
111 protected void configureShell(Shell shell
) {
112 super.configureShell(shell
);
113 shell
.setText("Error");