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