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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
13 * Created on 16.12.2005
\r
14 * @author Toni Kalajainen
\r
16 package org.simantics.utils.ui.dialogs;
\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
27 * Tool for making blocking message dialogs queries from non-UI thread
\r
31 public class SafeMessageDialog {
\r
33 /** Yes and No buttons */
\r
34 public final static String[] YES_NO = new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL };
\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
41 Shell shell = Display.getCurrent().getActiveShell();
\r
42 MessageDialog dialog = new MessageDialog(shell, title, image, message, type, buttons, defaultIndex);
\r
44 result.set(dialog.getReturnCode());
\r
47 Display display = Display.getCurrent();
\r
48 if (display == null)
\r
49 display = Display.getDefault();
\r
50 display.syncExec(r);
\r
52 if (result.get() == null)
\r
54 return result.get();
\r
58 * method for asking simple yes/no question in a dialog.
\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
64 public static boolean openQuestion(String title, String message) {
\r
65 Integer result = doMessageDialog(title, null, message, MessageDialog.QUESTION, YES_NO, 0);
\r