1 package org.simantics.utils.ui.dialogs;
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
6 import org.eclipse.core.runtime.IStatus;
7 import org.eclipse.jface.dialogs.IDialogConstants;
8 import org.eclipse.jface.dialogs.IconAndMessageDialog;
9 import org.eclipse.jface.layout.GridDataFactory;
10 import org.eclipse.swt.SWT;
11 import org.eclipse.swt.graphics.Image;
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;
24 * This class shows an error, Throwable or IStatus. Can be instantiated from any thread.
26 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
29 public class ShowError implements Runnable {
33 private String message;
35 private Throwable error;
37 private Display display;
39 private IStatus status;
41 public ShowError(String title, String message, Throwable error) {
43 this.message = message;
45 this.display = getDisplay();
46 display.asyncExec(this);
49 public ShowError(Display display, String title, String message, Throwable error) {
51 this.message = message;
53 this.display = display;
54 display.asyncExec(this);
57 public ShowError(String title, String message, Throwable error, boolean sync) {
59 this.message = message;
61 this.display = getDisplay();
63 display.syncExec(this);
65 display.asyncExec(this);
68 public ShowError(Display display, String title, String message, Throwable error, boolean sync) {
70 this.message = message;
72 this.display = display;
74 display.syncExec(this);
76 display.asyncExec(this);
79 public ShowError(String title, String message,IStatus status) {
81 this.message = message;
83 this.display = getDisplay();
84 display.asyncExec(this);
87 public ShowError(Display display,String title, String message, IStatus status) {
89 this.message = message;
91 this.display = display;
92 display.asyncExec(this);
95 public ShowError(String title, String message,IStatus status, boolean sync) {
97 this.message = message;
99 this.display = getDisplay();
101 display.syncExec(this);
103 display.asyncExec(this);
106 public ShowError(Display display,String title, String message, IStatus status, boolean sync) {
108 this.message = message;
109 this.status = status;
110 this.display = display;
112 display.syncExec(this);
114 display.asyncExec(this);
117 public Display getDisplay() {
118 if (display!=null) return display;
119 Display d = Display.getCurrent();
120 if (d!=null) return d;
121 return Display.getDefault();
125 Shell shell = display.getActiveShell();
126 if (error == null && status != null) {
127 org.eclipse.jface.dialogs.ErrorDialog.openError(shell, title, message, status);
129 ErrorDialog dialog = new ErrorDialog(shell, title, message, error);
134 public static void showError(String title, String message, Throwable error) {
135 new ShowError(title, message, error);
138 public static void showError(String title, String message,IStatus status) {
139 new ShowError(title, message, status);
143 private class ErrorDialog extends IconAndMessageDialog {
145 private String title;
146 //private String message;
147 private Text messageText;
148 private Text errorText;
149 private String errorString;
151 protected ErrorDialog(Shell parentShell, String title, String message, Throwable error) {
154 this.message = message;
156 StringWriter sw = new StringWriter();
157 error.printStackTrace(new PrintWriter(sw));
158 this.errorString = sw.toString();
160 setShellStyle(getShellStyle() | SWT.RESIZE);
164 protected Image getImage() {
165 return getErrorImage();
169 protected void configureShell(Shell newShell) {
170 super.configureShell(newShell);
171 newShell.setText(title);
172 newShell.setImage(newShell.getDisplay().getSystemImage(SWT.ICON_ERROR));
176 // protected Control createDialogArea(Composite parent) {
177 // Composite composite = new Composite(parent, SWT.NONE);
178 // GridLayout layout = new GridLayout();
179 // layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
180 // layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
181 // layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
182 // layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
183 // composite.setLayout(layout);
184 // composite.setLayoutData(new GridData(GridData.FILL_BOTH));
185 // applyDialogFont(composite);
188 // Text label = new Text(composite, SWT.MULTI);
189 // label.setEditable(false);
190 // if (message != null)
191 // label.setText(message);
193 // label.setText("no message");
198 protected Control createMessageArea(Composite composite) {
200 Image image = getImage();
202 imageLabel = new Label(composite, SWT.NULL);
203 image.setBackground(imageLabel.getBackground());
204 imageLabel.setImage(image);
205 GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.BEGINNING)
206 .applyTo(imageLabel);
209 if (message != null) {
210 messageText = new Text(composite, SWT.MULTI | SWT.BORDER | SWT.FLAT | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL);
211 messageText.setEditable(false);
212 messageText.setText(message);
217 convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH),
218 SWT.DEFAULT).applyTo(messageText);
223 protected Control createDialogArea(Composite parent) {
224 Composite composite = new Composite(parent, SWT.NONE);
226 createMessageArea(composite);
228 GridLayout layout = new GridLayout();
229 layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
230 layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
231 layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
232 layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
233 layout.numColumns = 2;
234 composite.setLayout(layout);
235 GridData childData = new GridData(GridData.FILL_BOTH);
236 childData.horizontalSpan = 2;
237 childData.grabExcessVerticalSpace = true;
238 composite.setLayoutData(childData);
239 applyDialogFont(composite);
244 protected void createButtonsForButtonBar(Composite parent) {
245 // create OK and Cancel buttons by default
246 createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
248 if (errorString != null)
249 createButton(parent, IDialogConstants.DETAILS_ID,
250 IDialogConstants.SHOW_DETAILS_LABEL, false);
254 protected void buttonPressed(int buttonId) {
255 super.buttonPressed(buttonId);
256 if (buttonId == IDialogConstants.DETAILS_ID) {
257 if (errorText == null) {
258 errorText = new Text((Composite)getDialogArea(), SWT.BORDER|SWT.H_SCROLL|SWT.V_SCROLL);
259 errorText.setText(errorString);
260 GridDataFactory.fillDefaults().span(2, 1).applyTo(errorText);
269 protected void resize() {
270 Point size = getInitialSize();
271 Point location = getInitialLocation(size);
272 getShell().setBounds(getConstrainedShellBounds(new Rectangle(location.x,
273 location.y, size.x, size.y)));