]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/dialogs/ShowMessage.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / dialogs / ShowMessage.java
diff --git a/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/dialogs/ShowMessage.java b/bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/dialogs/ShowMessage.java
new file mode 100644 (file)
index 0000000..aa436a1
--- /dev/null
@@ -0,0 +1,174 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.utils.ui.dialogs;\r
+\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.jface.dialogs.MessageDialog;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.swt.widgets.Shell;\r
+\r
+/**\r
+ * This class is shows message in dialog. This class can be instantiated from\r
+ * any thread.\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class ShowMessage implements Runnable {\r
+\r
+    public enum MessageType {\r
+        ERROR, CONFIRM, INFORMATION, QUESTION, WARNING\r
+    };\r
+\r
+    private String title;\r
+\r
+    private String message;\r
+\r
+    private MessageType type;\r
+    \r
+    private Display display;\r
+\r
+    public ShowMessage(String title, String message, MessageType type) {\r
+        this.title = title;\r
+        this.message = message;\r
+        this.type = type;\r
+        this.display = getDisplay();\r
+        display.asyncExec(this);\r
+    }\r
+\r
+    public ShowMessage(Display display, String title, String message, MessageType type) {\r
+        this.title = title;\r
+        this.message = message;\r
+        this.type = type;\r
+        this.display = display;\r
+        display.asyncExec(this);\r
+    }\r
+\r
+    public ShowMessage(String title, String message, MessageType type, boolean sync) {\r
+        this.title = title;\r
+        this.message = message;\r
+        this.type = type;\r
+        this.display = getDisplay();\r
+        if (sync)\r
+            display.syncExec(this);\r
+        else\r
+            display.asyncExec(this);\r
+    }\r
+\r
+    public ShowMessage(Display display, String title, String message, MessageType type, boolean sync) {\r
+        this.title = title;\r
+        this.message = message;\r
+        this.type = type;\r
+        this.display = display;\r
+        if (sync)\r
+            display.syncExec(this);\r
+        else\r
+            display.asyncExec(this);\r
+    }\r
+    \r
+    public Display getDisplay() {\r
+        if (display!=null) return display;\r
+        Display d = Display.getCurrent();\r
+        if (d!=null) return d;\r
+        return Display.getDefault();        \r
+    }\r
+    \r
+    public void run() {\r
+        Shell shell = display.getActiveShell();\r
+        if (type == MessageType.ERROR)\r
+            MessageDialog.openError(shell, title, message);\r
+        if (type == MessageType.CONFIRM)\r
+            MessageDialog.openConfirm(shell, title, message);\r
+        if (type == MessageType.INFORMATION)\r
+            MessageDialog.openInformation(shell, title, message);\r
+        if (type == MessageType.QUESTION)\r
+            MessageDialog.openQuestion(shell, title, message);\r
+        if (type == MessageType.WARNING)\r
+            MessageDialog.openWarning(shell, title, message);        \r
+    }\r
+\r
+    public static void showError(Display display, String title, String message) {\r
+        new ShowMessage(display, title, message, MessageType.ERROR);        \r
+    }\r
+    \r
+    public static void showError(String title, String message) {\r
+        new ShowMessage(title, message, MessageType.ERROR);        \r
+    }\r
+    \r
+    public static void showStatus(IStatus status) {\r
+       MessageType type = MessageType.INFORMATION;\r
+       String title = "";\r
+       if (status.getSeverity() == IStatus.OK) {\r
+               type = MessageType.INFORMATION;\r
+               title = "Status OK";\r
+       }\r
+       if (status.getSeverity() == IStatus.WARNING) {\r
+               type = MessageType.WARNING;\r
+               title = "Status Warning";\r
+       }\r
+       if (status.getSeverity() == IStatus.ERROR) {\r
+               type = MessageType.ERROR;\r
+               title = "Status Error";\r
+       }\r
+       if (status.getSeverity() == IStatus.CANCEL) {\r
+               type = MessageType.INFORMATION;\r
+               title = "Status Cancel";\r
+       }\r
+       if (status.getSeverity() == IStatus.INFO) {\r
+               type = MessageType.INFORMATION;\r
+               title = "Status Info";\r
+       }               \r
+       new ShowMessage(title, status.getMessage(), type);\r
+    }\r
+    \r
+    public static void showWarning(Display display, String title, String message) {\r
+        new ShowMessage(display, title, message, MessageType.WARNING);        \r
+    }\r
+    \r
+    public static void showWarning(String title, String message) {\r
+        new ShowMessage(title, message, MessageType.WARNING);        \r
+    }\r
+\r
+    public static void showInformation(Display display, String title, String message) {\r
+        new ShowMessage(display, title, message, MessageType.INFORMATION);        \r
+    }\r
+    \r
+    public static void showInformation(String title, String message) {\r
+        new ShowMessage(title, message, MessageType.INFORMATION);        \r
+    }\r
+    \r
+\r
+    \r
+    public static void syncShowError(Display display, String title, String message) {\r
+        new ShowMessage(display, title, message, MessageType.ERROR, true);        \r
+    }\r
+    \r
+    public static void syncShowError(String title, String message) {\r
+        new ShowMessage(title, message, MessageType.ERROR, true);        \r
+    }\r
+    \r
+    public static void syncShowWarning(Display display, String title, String message) {\r
+        new ShowMessage(display, title, message, MessageType.WARNING, true);        \r
+    }\r
+    \r
+    public static void syncShowWarning(String title, String message) {\r
+        new ShowMessage(title, message, MessageType.WARNING, true);        \r
+    }\r
+\r
+    public static void syncShowInformation(Display display, String title, String message) {\r
+        new ShowMessage(display, title, message, MessageType.INFORMATION, true);        \r
+    }\r
+    \r
+    public static void syncShowInformation(String title, String message) {\r
+        new ShowMessage(title, message, MessageType.INFORMATION, true);        \r
+    }\r
+    \r
+}\r