]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/dialogs/ShowMessage.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / dialogs / ShowMessage.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 package org.simantics.utils.ui.dialogs;
13
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;
18
19 /**
20  * This class is shows message in dialog. This class can be instantiated from
21  * any thread.
22  * 
23  * @author Toni Kalajainen
24  */
25 public class ShowMessage implements Runnable {
26
27     public enum MessageType {
28         ERROR, CONFIRM, INFORMATION, QUESTION, WARNING
29     };
30
31     private String title;
32
33     private String message;
34
35     private MessageType type;
36     
37     private Display display;
38
39     public ShowMessage(String title, String message, MessageType type) {
40         this.title = title;
41         this.message = message;
42         this.type = type;
43         this.display = getDisplay();
44         display.asyncExec(this);
45     }
46
47     public ShowMessage(Display display, String title, String message, MessageType type) {
48         this.title = title;
49         this.message = message;
50         this.type = type;
51         this.display = display;
52         display.asyncExec(this);
53     }
54
55     public ShowMessage(String title, String message, MessageType type, boolean sync) {
56         this.title = title;
57         this.message = message;
58         this.type = type;
59         this.display = getDisplay();
60         if (sync)
61             display.syncExec(this);
62         else
63             display.asyncExec(this);
64     }
65
66     public ShowMessage(Display display, String title, String message, MessageType type, boolean sync) {
67         this.title = title;
68         this.message = message;
69         this.type = type;
70         this.display = display;
71         if (sync)
72             display.syncExec(this);
73         else
74             display.asyncExec(this);
75     }
76     
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();        
82     }
83     
84     public void run() {
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);        
96     }
97
98     public static void showError(Display display, String title, String message) {
99         new ShowMessage(display, title, message, MessageType.ERROR);        
100     }
101     
102     public static void showError(String title, String message) {
103         new ShowMessage(title, message, MessageType.ERROR);        
104     }
105     
106     public static void showStatus(IStatus status) {
107         MessageType type = MessageType.INFORMATION;
108         String title = "";
109         if (status.getSeverity() == IStatus.OK) {
110                 type = MessageType.INFORMATION;
111                 title = "Status OK";
112         }
113         if (status.getSeverity() == IStatus.WARNING) {
114                 type = MessageType.WARNING;
115                 title = "Status Warning";
116         }
117         if (status.getSeverity() == IStatus.ERROR) {
118                 type = MessageType.ERROR;
119                 title = "Status Error";
120         }
121         if (status.getSeverity() == IStatus.CANCEL) {
122                 type = MessageType.INFORMATION;
123                 title = "Status Cancel";
124         }
125         if (status.getSeverity() == IStatus.INFO) {
126                 type = MessageType.INFORMATION;
127                 title = "Status Info";
128         }               
129         new ShowMessage(title, status.getMessage(), type);
130     }
131     
132     public static void showWarning(Display display, String title, String message) {
133         new ShowMessage(display, title, message, MessageType.WARNING);        
134     }
135     
136     public static void showWarning(String title, String message) {
137         new ShowMessage(title, message, MessageType.WARNING);        
138     }
139
140     public static void showInformation(Display display, String title, String message) {
141         new ShowMessage(display, title, message, MessageType.INFORMATION);        
142     }
143     
144     public static void showInformation(String title, String message) {
145         new ShowMessage(title, message, MessageType.INFORMATION);        
146     }
147     
148
149     
150     public static void syncShowError(Display display, String title, String message) {
151         new ShowMessage(display, title, message, MessageType.ERROR, true);        
152     }
153     
154     public static void syncShowError(String title, String message) {
155         new ShowMessage(title, message, MessageType.ERROR, true);        
156     }
157     
158     public static void syncShowWarning(Display display, String title, String message) {
159         new ShowMessage(display, title, message, MessageType.WARNING, true);        
160     }
161     
162     public static void syncShowWarning(String title, String message) {
163         new ShowMessage(title, message, MessageType.WARNING, true);        
164     }
165
166     public static void syncShowInformation(Display display, String title, String message) {
167         new ShowMessage(display, title, message, MessageType.INFORMATION, true);        
168     }
169     
170     public static void syncShowInformation(String title, String message) {
171         new ShowMessage(title, message, MessageType.INFORMATION, true);        
172     }
173     
174 }