]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/ErrorFeedback.java
2 * Copyright (C) 2007-2012 Mathieu Baudier
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
;
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 final static Log log
= LogFactory
.getLog(ErrorFeedback
.class);
40 private final String message
;
41 private final Throwable exception
;
43 public static void show(String message
, Throwable e
) {
44 // rethrow ThreaDeath in order to make sure that RAP will properly clean
46 if (e
instanceof ThreadDeath
)
47 throw (ThreadDeath
) e
;
49 new ErrorFeedback(getDisplay().getActiveShell(), message
, e
).open();
52 public static void show(String message
) {
53 new ErrorFeedback(getDisplay().getActiveShell(), message
, null).open();
56 /** Tries to find a display */
57 private static Display
getDisplay() {
59 Display display
= PlatformUI
.getWorkbench().getDisplay();
63 return Display
.getDefault();
64 } catch (Exception e
) {
65 return Display
.getCurrent();
69 public ErrorFeedback(Shell parentShell
, String message
, Throwable e
) {
71 this.message
= message
;
73 log
.error(message
, e
);
76 protected Point
getInitialSize() {
77 if (exception
!= null)
78 return new Point(800, 600);
80 return new Point(400, 300);
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));
91 setMessage(message
!= null ? message
92 + (exception
!= null ?
": " + exception
.getMessage() : "")
93 : exception
!= null ? exception
.getMessage() : "Unkown Error",
94 IMessageProvider
.ERROR
);
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());
110 protected void configureShell(Shell shell
) {
111 super.configureShell(shell
);
112 shell
.setText("Error");