]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/dialogs/FeedbackDialog.java
Introduce CMS Theme
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui / src / org / argeo / eclipse / ui / dialogs / FeedbackDialog.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.eclipse.ui.dialogs;
17
18 import java.io.PrintWriter;
19 import java.io.StringWriter;
20
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;
37
38 /** Generic lightweight dialog, not based on JFace. */
39 public class FeedbackDialog extends LightweightDialog {
40 private final static Log log = LogFactory.getLog(FeedbackDialog.class);
41
42 private String message;
43 private Throwable exception;
44
45 private Shell parentShell;
46 private Shell shell;
47
48 public static void show(String message, Throwable e) {
49 // rethrow ThreaDeath in order to make sure that RAP will properly clean
50 // up the UI thread
51 if (e instanceof ThreadDeath)
52 throw (ThreadDeath) e;
53
54 new FeedbackDialog(getDisplay().getActiveShell(), message, e).open();
55 }
56
57 public static void show(String message) {
58 new FeedbackDialog(getDisplay().getActiveShell(), message, null).open();
59 }
60
61 /** Tries to find a display */
62 private static Display getDisplay() {
63 try {
64 Display display = Display.getCurrent();
65 if (display != null)
66 return display;
67 else
68 return Display.getDefault();
69 } catch (Exception e) {
70 return Display.getCurrent();
71 }
72 }
73
74 public FeedbackDialog(Shell parentShell, String message, Throwable e) {
75 super(parentShell);
76 this.message = message;
77 this.exception = e;
78 log.error(message, e);
79 }
80
81 public void open() {
82 if (shell != null)
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);
89 // shell.pack();
90 // shell.layout();
91
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);
97
98 shell.addShellListener(new ShellAdapter() {
99 private static final long serialVersionUID = -2701270481953688763L;
100
101 @Override
102 public void shellDeactivated(ShellEvent e) {
103 closeShell();
104 }
105 });
106
107 shell.open();
108 }
109
110 protected void closeShell() {
111 shell.close();
112 shell.dispose();
113 shell = null;
114 }
115
116 protected Point getInitialSize() {
117 // if (exception != null)
118 // return new Point(800, 600);
119 // else
120 return new Point(400, 300);
121 }
122
123 protected Control createDialogArea(Composite parent) {
124 Composite dialogarea = new Composite(parent, SWT.NONE);
125 dialogarea.setLayout(new GridLayout());
126 // Composite dialogarea = (Composite) super.createDialogArea(parent);
127 dialogarea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
128
129 Label messageLbl = new Label(dialogarea, SWT.NONE);
130 if (message != null)
131 messageLbl.setText(message);
132 else if (exception != null)
133 messageLbl.setText(exception.getLocalizedMessage());
134
135 Composite composite = new Composite(dialogarea, SWT.NONE);
136 composite.setLayout(new GridLayout(2, false));
137 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
138
139 if (exception != null) {
140 Text stack = new Text(composite, SWT.MULTI | SWT.LEAD | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
141 stack.setEditable(false);
142 stack.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
143 StringWriter sw = new StringWriter();
144 exception.printStackTrace(new PrintWriter(sw));
145 stack.setText(sw.toString());
146 }
147
148 // parent.pack();
149 return composite;
150 }
151 }