]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/dialogs/ShowError.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / dialogs / ShowError.java
1 package org.simantics.utils.ui.dialogs;
2
3 import java.io.PrintWriter;
4 import java.io.StringWriter;
5
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;
22
23 /**
24  * This class shows an error, Throwable or IStatus. Can be instantiated from any thread.
25  * 
26  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
27  *
28  */
29 public class ShowError implements Runnable {
30
31     private String title;
32
33     private String message;
34
35     private Throwable error;
36
37     private Display display;
38
39     private IStatus status;
40
41     public ShowError(String title, String message, Throwable error) {
42         this.title = title;
43         this.message = message;
44         this.error = error;
45         this.display = getDisplay();
46         display.asyncExec(this);
47     }
48
49     public ShowError(Display display, String title, String message, Throwable error) {
50         this.title = title;
51         this.message = message;
52         this.error = error;
53         this.display = display;
54         display.asyncExec(this);
55     }
56
57     public ShowError(String title, String message, Throwable error, boolean sync) {
58         this.title = title;
59         this.message = message;
60         this.error = error;
61         this.display = getDisplay();
62         if (sync)
63             display.syncExec(this);
64         else
65             display.asyncExec(this);
66     }
67
68     public ShowError(Display display, String title, String message, Throwable error, boolean sync) {
69         this.title = title;
70         this.message = message;
71         this.error = error;
72         this.display = display;
73         if (sync)
74             display.syncExec(this);
75         else
76             display.asyncExec(this);
77     }
78
79     public ShowError(String title, String message,IStatus status) {
80         this.title = title;
81         this.message = message;
82         this.status = status;
83         this.display = getDisplay();
84         display.asyncExec(this);
85     }
86
87     public ShowError(Display display,String title, String message, IStatus status) {
88         this.title = title;
89         this.message = message;
90         this.status = status;
91         this.display = display;
92         display.asyncExec(this);
93     }
94
95     public ShowError(String title, String message,IStatus status, boolean sync) {
96         this.title = title;
97         this.message = message;
98         this.status = status;
99         this.display = getDisplay();
100         if (sync)
101             display.syncExec(this);
102         else
103             display.asyncExec(this);
104     }
105
106     public ShowError(Display display,String title, String message, IStatus status, boolean sync) {
107         this.title = title;
108         this.message = message;
109         this.status = status;
110         this.display = display;
111         if (sync)
112             display.syncExec(this);
113         else
114             display.asyncExec(this);
115     }
116
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();
122     }
123
124     public void run() {
125         Shell shell = display.getActiveShell();
126         if (error == null && status != null) {
127             org.eclipse.jface.dialogs.ErrorDialog.openError(shell, title, message, status);
128         } else {
129             ErrorDialog dialog = new ErrorDialog(shell, title, message, error);
130             dialog.open();
131         }
132     }
133
134     public static void showError(String title, String message, Throwable error) {
135         new ShowError(title, message, error);
136     }
137
138     public static void showError(String title, String message,IStatus status) {
139         new ShowError(title, message, status);
140     }
141
142
143     private class ErrorDialog extends IconAndMessageDialog {
144
145         private String title;
146         //private String message;
147         private Text messageText;
148         private Text errorText;
149         private String errorString;
150
151         protected ErrorDialog(Shell parentShell, String title, String message, Throwable error) {
152             super(parentShell);
153             this.title = title;
154             this.message = message;
155             if (error != null) {
156                 StringWriter sw = new StringWriter();
157                 error.printStackTrace(new PrintWriter(sw));
158                 this.errorString = sw.toString();
159             }
160             setShellStyle(getShellStyle() | SWT.RESIZE);
161         }
162
163         @Override
164         protected Image getImage() {
165             return getErrorImage();
166         }
167
168         @Override
169         protected void configureShell(Shell newShell) {
170             super.configureShell(newShell);
171             newShell.setText(title);
172             newShell.setImage(newShell.getDisplay().getSystemImage(SWT.ICON_ERROR));
173         }
174
175 //        @Override
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);
186 //
187 //
188 //            Text label = new Text(composite, SWT.MULTI);
189 //            label.setEditable(false);
190 //            if (message != null)
191 //                label.setText(message);
192 //            else
193 //                label.setText("no message");
194 //
195 //            return composite;
196 //        }
197
198         protected Control createMessageArea(Composite composite) {
199             // create image
200             Image image = getImage();
201             if (image != null) {
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);
207             }
208             // create message
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);
213                 GridDataFactory
214                         .fillDefaults()
215                         .grab(true, true)
216                         .hint(
217                                 convertHorizontalDLUsToPixels(IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH),
218                                 SWT.DEFAULT).applyTo(messageText);
219             }
220             return composite;
221         }
222
223         protected Control createDialogArea(Composite parent) {
224             Composite composite = new Composite(parent, SWT.NONE);
225
226             createMessageArea(composite);
227
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);
240
241             return composite;
242         }
243
244         protected void createButtonsForButtonBar(Composite parent) {
245             // create OK and Cancel buttons by default
246             createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
247                     true);
248             if (errorString != null)
249                 createButton(parent, IDialogConstants.DETAILS_ID,
250                         IDialogConstants.SHOW_DETAILS_LABEL, false);
251         }
252
253         @Override
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);
261                 } else {
262                     errorText.dispose();
263                     errorText = null;
264                 }
265                 resize();
266             }
267         }
268
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)));
274         }
275
276     }
277
278 }