]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/FeedbackDialog.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
.argeo
.eclipse
.ui
.EclipseUiException
;
24 import org
.eclipse
.swt
.SWT
;
25 import org
.eclipse
.swt
.events
.ShellAdapter
;
26 import org
.eclipse
.swt
.events
.ShellEvent
;
27 import org
.eclipse
.swt
.graphics
.Point
;
28 import org
.eclipse
.swt
.graphics
.Rectangle
;
29 import org
.eclipse
.swt
.layout
.GridData
;
30 import org
.eclipse
.swt
.layout
.GridLayout
;
31 import org
.eclipse
.swt
.widgets
.Composite
;
32 import org
.eclipse
.swt
.widgets
.Control
;
33 import org
.eclipse
.swt
.widgets
.Display
;
34 import org
.eclipse
.swt
.widgets
.Label
;
35 import org
.eclipse
.swt
.widgets
.Shell
;
36 import org
.eclipse
.swt
.widgets
.Text
;
38 /** Generic lightweight dialog, not based on JFace. */
39 public class FeedbackDialog
extends LightweightDialog
{
40 private final static Log log
= LogFactory
.getLog(FeedbackDialog
.class);
42 private String message
;
43 private Throwable exception
;
45 private Shell parentShell
;
48 public static void show(String message
, Throwable e
) {
49 // rethrow ThreaDeath in order to make sure that RAP will properly clean
51 if (e
instanceof ThreadDeath
)
52 throw (ThreadDeath
) e
;
54 new FeedbackDialog(getDisplay().getActiveShell(), message
, e
).open();
57 public static void show(String message
) {
58 new FeedbackDialog(getDisplay().getActiveShell(), message
, null).open();
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 FeedbackDialog(Shell parentShell
, String message
, Throwable e
) {
76 this.message
= message
;
78 log
.error(message
, e
);
83 throw new EclipseUiException("There is already a shell");
84 shell
= new Shell(getDisplay(), SWT
.NO_TRIM
| SWT
.BORDER
| SWT
.ON_TOP
);
85 shell
.setLayout(new GridLayout());
86 // shell.setText("Error");
87 shell
.setSize(getInitialSize());
88 createDialogArea(shell
);
92 Rectangle shellBounds
= Display
.getCurrent().getBounds();// RAP
93 Point dialogSize
= shell
.getSize();
94 int x
= shellBounds
.x
+ (shellBounds
.width
- dialogSize
.x
) / 2;
95 int y
= shellBounds
.y
+ (shellBounds
.height
- dialogSize
.y
) / 2;
96 shell
.setLocation(x
, y
);
98 shell
.addShellListener(new ShellAdapter() {
99 private static final long serialVersionUID
= -2701270481953688763L;
102 public void shellDeactivated(ShellEvent e
) {
111 protected void closeShell() {
117 protected Point
getInitialSize() {
118 // if (exception != null)
119 // return new Point(800, 600);
121 return new Point(400, 300);
124 protected Control
createDialogArea(Composite parent
) {
125 Composite dialogarea
= new Composite(parent
, SWT
.NONE
);
126 dialogarea
.setLayout(new GridLayout());
127 // Composite dialogarea = (Composite) super.createDialogArea(parent);
128 dialogarea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
130 Label messageLbl
= new Label(dialogarea
, SWT
.NONE
);
132 messageLbl
.setText(message
);
133 else if (exception
!= null)
134 messageLbl
.setText(exception
.getLocalizedMessage());
136 Composite composite
= new Composite(dialogarea
, SWT
.NONE
);
137 composite
.setLayout(new GridLayout(2, false));
138 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
140 if (exception
!= null) {
141 Text stack
= new Text(composite
, SWT
.MULTI
| SWT
.LEAD
| SWT
.BORDER
| SWT
.V_SCROLL
| SWT
.H_SCROLL
);
142 stack
.setEditable(false);
143 stack
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
144 StringWriter sw
= new StringWriter();
145 exception
.printStackTrace(new PrintWriter(sw
));
146 stack
.setText(sw
.toString());