]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/runtime/org.argeo.eclipse.ui/src/main/java/org/argeo/eclipse/ui/Error.java
1 package org
.argeo
.eclipse
.ui
;
3 import java
.io
.PrintWriter
;
4 import java
.io
.StringWriter
;
6 import org
.apache
.commons
.logging
.Log
;
7 import org
.apache
.commons
.logging
.LogFactory
;
8 import org
.eclipse
.jface
.dialogs
.IMessageProvider
;
9 import org
.eclipse
.jface
.dialogs
.TitleAreaDialog
;
10 import org
.eclipse
.swt
.SWT
;
11 import org
.eclipse
.swt
.graphics
.Point
;
12 import org
.eclipse
.swt
.layout
.GridData
;
13 import org
.eclipse
.swt
.layout
.GridLayout
;
14 import org
.eclipse
.swt
.widgets
.Composite
;
15 import org
.eclipse
.swt
.widgets
.Control
;
16 import org
.eclipse
.swt
.widgets
.Display
;
17 import org
.eclipse
.swt
.widgets
.Shell
;
18 import org
.eclipse
.swt
.widgets
.Text
;
19 import org
.eclipse
.ui
.PlatformUI
;
21 /** Generic error dialog to be used in try/catch blocks */
22 public class Error
extends TitleAreaDialog
{
23 private final static Log log
= LogFactory
.getLog(Error
.class);
25 private final String message
;
26 private final Throwable exception
;
28 public static void show(String message
, Throwable e
) {
29 new Error(getDisplay().getActiveShell(), message
, e
).open();
32 public static void show(String message
) {
33 new Error(getDisplay().getActiveShell(), message
, null).open();
36 /** Tries to find a display */
37 private static Display
getDisplay() {
39 return PlatformUI
.getWorkbench().getDisplay();
40 } catch (Exception e
) {
41 return Display
.getCurrent();
45 public Error(Shell parentShell
, String message
, Throwable e
) {
47 this.message
= message
;
49 log
.error(message
, e
);
52 protected Point
getInitialSize() {
53 if (exception
!= null)
54 return new Point(800, 600);
56 return new Point(400, 300);
60 protected Control
createDialogArea(Composite parent
) {
61 Composite dialogarea
= (Composite
) super.createDialogArea(parent
);
62 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
63 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
64 composite
.setLayout(new GridLayout(2, false));
65 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
67 setMessage(message
!= null ? message
68 + (exception
!= null ?
": " + exception
.getMessage() : "")
69 : exception
!= null ? exception
.getMessage() : "Unkown Error",
70 IMessageProvider
.ERROR
);
72 if (exception
!= null) {
73 Text stack
= new Text(composite
, SWT
.MULTI
| SWT
.LEAD
| SWT
.BORDER
74 | SWT
.V_SCROLL
| SWT
.H_SCROLL
);
75 stack
.setEditable(false);
76 stack
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
77 StringWriter sw
= new StringWriter();
78 exception
.printStackTrace(new PrintWriter(sw
));
79 stack
.setText(sw
.toString());
86 protected void configureShell(Shell shell
) {
87 super.configureShell(shell
);
88 shell
.setText("Error");