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