]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/NonModalErrorFeedback.java
1 package org
.argeo
.eclipse
.ui
.dialogs
;
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
.argeo
.eclipse
.ui
.EclipseUiException
;
9 import org
.eclipse
.swt
.SWT
;
10 import org
.eclipse
.swt
.events
.ShellAdapter
;
11 import org
.eclipse
.swt
.events
.ShellEvent
;
12 import org
.eclipse
.swt
.graphics
.Point
;
13 import org
.eclipse
.swt
.graphics
.Rectangle
;
14 import org
.eclipse
.swt
.layout
.GridData
;
15 import org
.eclipse
.swt
.layout
.GridLayout
;
16 import org
.eclipse
.swt
.widgets
.Composite
;
17 import org
.eclipse
.swt
.widgets
.Control
;
18 import org
.eclipse
.swt
.widgets
.Display
;
19 import org
.eclipse
.swt
.widgets
.Label
;
20 import org
.eclipse
.swt
.widgets
.Shell
;
21 import org
.eclipse
.swt
.widgets
.Text
;
23 /** Generic error dialog to be used in try/catch blocks */
24 class NonModalErrorFeedback
{
25 private final static Log log
= LogFactory
26 .getLog(NonModalErrorFeedback
.class);
28 private final String message
;
29 private final Throwable exception
;
33 public static void show(String message
, Throwable e
) {
34 // rethrow ThreaDeath in order to make sure that RAP will properly clean
36 if (e
instanceof ThreadDeath
)
37 throw (ThreadDeath
) e
;
39 new NonModalErrorFeedback(getDisplay().getActiveShell(), message
, e
)
43 public static void show(String message
) {
44 new NonModalErrorFeedback(getDisplay().getActiveShell(), message
, null)
48 /** Tries to find a display */
49 private static Display
getDisplay() {
51 Display display
= Display
.getCurrent();
55 return Display
.getDefault();
56 } catch (Exception e
) {
57 return Display
.getCurrent();
61 public NonModalErrorFeedback(Shell parentShell
, String message
, Throwable e
) {
62 this.message
= message
;
64 log
.error(message
, e
);
69 throw new EclipseUiException("There is already a shell");
70 shell
= new Shell(getDisplay(), SWT
.NO_TRIM
| SWT
.BORDER
| SWT
.ON_TOP
);
71 shell
.setLayout(new GridLayout());
72 // shell.setText("Error");
73 shell
.setSize(getInitialSize());
74 createDialogArea(shell
);
78 Rectangle shellBounds
= Display
.getCurrent().getBounds();// RAP
79 Point dialogSize
= shell
.getSize();
80 int x
= shellBounds
.x
+ (shellBounds
.width
- dialogSize
.x
) / 2;
81 int y
= shellBounds
.y
+ (shellBounds
.height
- dialogSize
.y
) / 2;
82 shell
.setLocation(x
, y
);
84 shell
.addShellListener(new ShellAdapter() {
85 private static final long serialVersionUID
= -2701270481953688763L;
88 public void shellDeactivated(ShellEvent e
) {
96 protected void closeShell() {
102 protected Point
getInitialSize() {
103 // if (exception != null)
104 // return new Point(800, 600);
106 return new Point(400, 300);
109 protected Control
createDialogArea(Composite parent
) {
110 Composite dialogarea
= new Composite(parent
, SWT
.NONE
);
111 dialogarea
.setLayout(new GridLayout());
112 // Composite dialogarea = (Composite) super.createDialogArea(parent);
113 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
115 Label messageLbl
= new Label(dialogarea
, SWT
.NONE
);
117 messageLbl
.setText(message
);
118 else if (exception
!= null)
119 messageLbl
.setText(exception
.getLocalizedMessage());
121 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
122 composite
.setLayout(new GridLayout(2, false));
123 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
125 if (exception
!= null) {
126 Text stack
= new Text(composite
, SWT
.MULTI
| SWT
.LEAD
| SWT
.BORDER
127 | SWT
.V_SCROLL
| SWT
.H_SCROLL
);
128 stack
.setEditable(false);
129 stack
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
130 StringWriter sw
= new StringWriter();
131 exception
.printStackTrace(new PrintWriter(sw
));
132 stack
.setText(sw
.toString());