1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.ui.dialogs;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.swt.widgets.Display;
17 import org.eclipse.swt.widgets.Shell;
20 * This class is shows message in dialog. This class can be instantiated from
23 * @author Toni Kalajainen
25 public class ShowMessage implements Runnable {
27 public enum MessageType {
28 ERROR, CONFIRM, INFORMATION, QUESTION, WARNING
33 private String message;
35 private MessageType type;
37 private Display display;
39 public ShowMessage(String title, String message, MessageType type) {
41 this.message = message;
43 this.display = getDisplay();
44 display.asyncExec(this);
47 public ShowMessage(Display display, String title, String message, MessageType type) {
49 this.message = message;
51 this.display = display;
52 display.asyncExec(this);
55 public ShowMessage(String title, String message, MessageType type, boolean sync) {
57 this.message = message;
59 this.display = getDisplay();
61 display.syncExec(this);
63 display.asyncExec(this);
66 public ShowMessage(Display display, String title, String message, MessageType type, boolean sync) {
68 this.message = message;
70 this.display = display;
72 display.syncExec(this);
74 display.asyncExec(this);
77 public Display getDisplay() {
78 if (display!=null) return display;
79 Display d = Display.getCurrent();
80 if (d!=null) return d;
81 return Display.getDefault();
85 Shell shell = display.getActiveShell();
86 if (type == MessageType.ERROR)
87 MessageDialog.openError(shell, title, message);
88 if (type == MessageType.CONFIRM)
89 MessageDialog.openConfirm(shell, title, message);
90 if (type == MessageType.INFORMATION)
91 MessageDialog.openInformation(shell, title, message);
92 if (type == MessageType.QUESTION)
93 MessageDialog.openQuestion(shell, title, message);
94 if (type == MessageType.WARNING)
95 MessageDialog.openWarning(shell, title, message);
98 public static void showError(Display display, String title, String message) {
99 new ShowMessage(display, title, message, MessageType.ERROR);
102 public static void showError(String title, String message) {
103 new ShowMessage(title, message, MessageType.ERROR);
106 public static void showStatus(IStatus status) {
107 MessageType type = MessageType.INFORMATION;
109 if (status.getSeverity() == IStatus.OK) {
110 type = MessageType.INFORMATION;
113 if (status.getSeverity() == IStatus.WARNING) {
114 type = MessageType.WARNING;
115 title = "Status Warning";
117 if (status.getSeverity() == IStatus.ERROR) {
118 type = MessageType.ERROR;
119 title = "Status Error";
121 if (status.getSeverity() == IStatus.CANCEL) {
122 type = MessageType.INFORMATION;
123 title = "Status Cancel";
125 if (status.getSeverity() == IStatus.INFO) {
126 type = MessageType.INFORMATION;
127 title = "Status Info";
129 new ShowMessage(title, status.getMessage(), type);
132 public static void showWarning(Display display, String title, String message) {
133 new ShowMessage(display, title, message, MessageType.WARNING);
136 public static void showWarning(String title, String message) {
137 new ShowMessage(title, message, MessageType.WARNING);
140 public static void showInformation(Display display, String title, String message) {
141 new ShowMessage(display, title, message, MessageType.INFORMATION);
144 public static void showInformation(String title, String message) {
145 new ShowMessage(title, message, MessageType.INFORMATION);
150 public static void syncShowError(Display display, String title, String message) {
151 new ShowMessage(display, title, message, MessageType.ERROR, true);
154 public static void syncShowError(String title, String message) {
155 new ShowMessage(title, message, MessageType.ERROR, true);
158 public static void syncShowWarning(Display display, String title, String message) {
159 new ShowMessage(display, title, message, MessageType.WARNING, true);
162 public static void syncShowWarning(String title, String message) {
163 new ShowMessage(title, message, MessageType.WARNING, true);
166 public static void syncShowInformation(Display display, String title, String message) {
167 new ShowMessage(display, title, message, MessageType.INFORMATION, true);
170 public static void syncShowInformation(String title, String message) {
171 new ShowMessage(title, message, MessageType.INFORMATION, true);