]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.thread.swt/src/org/simantics/utils/threads/SWTThread.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.thread.swt / src / org / simantics / utils / threads / SWTThread.java
diff --git a/bundles/org.simantics.utils.thread.swt/src/org/simantics/utils/threads/SWTThread.java b/bundles/org.simantics.utils.thread.swt/src/org/simantics/utils/threads/SWTThread.java
new file mode 100644 (file)
index 0000000..482c671
--- /dev/null
@@ -0,0 +1,92 @@
+/*******************************************************************************\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
+/*\r
+ *\r
+ * @author Toni Kalajainen\r
+ */\r
+package org.simantics.utils.threads;\r
+\r
+import java.util.concurrent.Executor;\r
+\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.swt.widgets.Widget;\r
+\r
+public class SWTThread implements IThreadWorkQueue, Executor {\r
+\r
+       final Display display;\r
+       \r
+       public static IThreadWorkQueue getThreadAccess(Display display)\r
+       {\r
+               return new SWTThread(display);\r
+       }\r
+       \r
+       public static IThreadWorkQueue getThreadAccess(Widget widget)\r
+       {\r
+               return new SWTThread(widget.getDisplay());\r
+       }\r
+       \r
+       public static IThreadWorkQueue getThreadAccess() {\r
+               return new SWTThread(Display.getDefault());\r
+       }\r
+       \r
+       SWTThread(Display display)\r
+       {\r
+               this.display = display;\r
+       }\r
+       \r
+       @Override\r
+       public Thread asyncExec(Runnable runnable) {\r
+               // Don't accept work if the SWT thread is disposed.\r
+               if (display.isDisposed())\r
+                       return null;\r
+               display.asyncExec(runnable);\r
+               return display.getThread();\r
+       }\r
+\r
+       @Override\r
+       public boolean syncExec(Runnable runnable) {\r
+               // Don't accept work if the SWT thread is disposed.\r
+               if (display.isDisposed())\r
+                       return false;\r
+               if (display.getThread() == Thread.currentThread ())\r
+                       runnable.run();\r
+               else \r
+                       display.syncExec(runnable);\r
+               return true;\r
+       }\r
+\r
+       @Override\r
+       public boolean currentThreadAccess() {\r
+               // A disposed SWT thread can never be the current thread access\r
+               if (display.isDisposed())\r
+                       return false;\r
+               return display.getThread() == Thread.currentThread ();\r
+       }\r
+\r
+       @Override\r
+       public Thread getThread() {\r
+               // Use null to indicate disposed SWT thread.\r
+               if (display.isDisposed())\r
+                       return null;\r
+               return display.getThread();\r
+       }\r
+       \r
+       @Override\r
+       public String toString() {\r
+               return "SWT Thread";\r
+       }\r
+\r
+       @Override\r
+       public void execute(Runnable command) {\r
+               syncExec(command);\r
+       }\r
+}\r