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