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