]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/dialogs/SafeMessageDialog.java
Sync git svn branch with SVN repository r33269.
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / dialogs / SafeMessageDialog.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 /*\r
13  * Created on 16.12.2005\r
14  * @author Toni Kalajainen\r
15  */\r
16 package org.simantics.utils.ui.dialogs;\r
17 \r
18 import org.eclipse.jface.dialogs.IDialogConstants;\r
19 import org.eclipse.jface.dialogs.MessageDialog;\r
20 import org.eclipse.swt.graphics.Image;\r
21 import org.eclipse.swt.widgets.Display;\r
22 import org.eclipse.swt.widgets.Shell;\r
23 import org.simantics.utils.DataContainer;\r
24 \r
25 \r
26 /**\r
27  * Tool for making blocking message dialogs queries from non-UI thread\r
28  * \r
29  * \r
30  */\r
31 public class SafeMessageDialog {\r
32 \r
33     /** Yes and No buttons */\r
34     public final static String[] YES_NO = new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL };\r
35 \r
36     public static Integer doMessageDialog(final String title, final Image image, final String message, final int type,\r
37             final String buttons[], final int defaultIndex) {\r
38         final DataContainer<Integer> result = new DataContainer<Integer>();\r
39         Runnable r = new Runnable() {\r
40             public void run() {\r
41                 Shell shell = Display.getCurrent().getActiveShell();\r
42                 MessageDialog dialog = new MessageDialog(shell, title, image, message, type, buttons, defaultIndex);\r
43                 dialog.open();\r
44                 result.set(dialog.getReturnCode());\r
45             }\r
46         };\r
47         Display display = Display.getCurrent();\r
48         if (display == null)\r
49                 display = Display.getDefault();\r
50         display.syncExec(r);\r
51 \r
52         if (result.get() == null)\r
53             return null;\r
54         return result.get();\r
55     }\r
56 \r
57     /**\r
58      * method for asking simple yes/no question in a dialog.\r
59      * \r
60      * @param title the dialog's title, or <code>null</code> if none\r
61      * @param message the message\r
62      * @return true if user pressed OK\r
63      */\r
64     public static boolean openQuestion(String title, String message) {\r
65         Integer result = doMessageDialog(title, null, message, MessageDialog.QUESTION, YES_NO, 0);\r
66         if (result == null)\r
67             return false;\r
68         return result == 0;\r
69     }\r
70 \r
71 }\r