]>
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(newShell(), message
, e
).open();
53 public static void show(String message
) {
54 new ErrorFeedback(newShell(), message
, null).open();
57 private static Shell
newShell() {
58 return new Shell(getDisplay(), SWT
.NO_TRIM
);
61 /** Tries to find a display */
62 private static Display
getDisplay() {
64 Display display
= Display
.getCurrent();
68 return Display
.getDefault();
69 } catch (Exception e
) {
70 return Display
.getCurrent();
74 public ErrorFeedback(Shell parentShell
, String message
, Throwable e
) {
76 setShellStyle(SWT
.NO_TRIM
);
77 this.message
= message
;
79 log
.error(message
, e
);
82 protected Point
getInitialSize() {
83 if (exception
!= null)
84 return new Point(800, 600);
86 return new Point(400, 300);
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));
97 setMessage(message
!= null ? message
+ (exception
!= null ?
": " + exception
.getMessage() : "")
98 : exception
!= null ? exception
.getMessage() : "Unkown Error", IMessageProvider
.ERROR
);
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());
113 protected void configureShell(Shell shell
) {
114 super.configureShell(shell
);
115 shell
.setText("Error");