]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui.workbench/src/org/argeo/eclipse/ui/workbench/ErrorFeedback.java
9a5482584b5e7576f37adbac5e3470da59f3ed74
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
.workbench
;
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
;
34 import org
.eclipse
.ui
.PlatformUI
;
36 /** Generic error dialog to be used in try/catch blocks */
37 public class ErrorFeedback
extends TitleAreaDialog
{
38 private static final long serialVersionUID
= -8918084784628179044L;
40 private final static Log log
= LogFactory
.getLog(ErrorFeedback
.class);
42 private final String message
;
43 private final Throwable exception
;
45 public static void show(String message
, Throwable e
) {
46 // rethrow ThreaDeath in order to make sure that RAP will properly clean
48 if (e
instanceof ThreadDeath
)
49 throw (ThreadDeath
) e
;
51 new ErrorFeedback(getDisplay().getActiveShell(), message
, e
).open();
54 public static void show(String message
) {
55 new ErrorFeedback(getDisplay().getActiveShell(), message
, null).open();
58 /** Tries to find a display */
59 private static Display
getDisplay() {
61 Display display
= PlatformUI
.getWorkbench().getDisplay();
65 return Display
.getDefault();
66 } catch (Exception e
) {
67 return Display
.getCurrent();
71 public ErrorFeedback(Shell parentShell
, String message
, Throwable e
) {
73 this.message
= message
;
75 log
.error(message
, e
);
78 protected Point
getInitialSize() {
79 if (exception
!= null)
80 return new Point(800, 600);
82 return new Point(400, 300);
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));
93 setMessage(message
!= null ? message
94 + (exception
!= null ?
": " + exception
.getMessage() : "")
95 : exception
!= null ? exception
.getMessage() : "Unkown Error",
96 IMessageProvider
.ERROR
);
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());
112 protected void configureShell(Shell shell
) {
113 super.configureShell(shell
);
114 shell
.setText("Error");