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