-/*******************************************************************************\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
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+/*
+ *
+ * @author Toni Kalajainen
+ */
+package org.simantics.utils.threads;
+
+import java.util.concurrent.Executor;
+
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Widget;
+
+public class SWTThread implements IThreadWorkQueue, Executor {
+
+ private final Display display;
+ private final boolean executeAsync;
+
+ public static IThreadWorkQueue getThreadAccess(Display display, boolean executeAsync)
+ {
+ return new SWTThread(display, executeAsync);
+ }
+
+ public static IThreadWorkQueue getThreadAccess(Display display)
+ {
+ return getThreadAccess(display, false);
+ }
+
+ public static IThreadWorkQueue getThreadAccess(Widget widget)
+ {
+ return new SWTThread(widget.getDisplay(), false);
+ }
+
+ public static IThreadWorkQueue getThreadAccess() {
+ return new SWTThread(Display.getDefault(), false);
+ }
+
+ SWTThread(Display display, boolean executeAsync)
+ {
+ this.display = display;
+ this.executeAsync = executeAsync;
+ }
+
+ @Override
+ public Thread asyncExec(Runnable runnable) {
+ // Don't accept work if the SWT thread is disposed.
+ if (display.isDisposed())
+ return null;
+ display.asyncExec(runnable);
+ return display.getThread();
+ }
+
+ @Override
+ public boolean syncExec(Runnable runnable) {
+ // Don't accept work if the SWT thread is disposed.
+ if (display.isDisposed())
+ return false;
+ if (display.getThread() == Thread.currentThread ())
+ runnable.run();
+ else
+ display.syncExec(runnable);
+ return true;
+ }
+
+ @Override
+ public boolean currentThreadAccess() {
+ // A disposed SWT thread can never be the current thread access
+ if (display.isDisposed())
+ return false;
+ return display.getThread() == Thread.currentThread ();
+ }
+
+ @Override
+ public Thread getThread() {
+ // Use null to indicate disposed SWT thread.
+ if (display.isDisposed())
+ return null;
+ return display.getThread();
+ }
+
+ @Override
+ public String toString() {
+ return "SWT Thread";
+ }
+
+ @Override
+ public void execute(Runnable command) {
+ if (executeAsync) asyncExec(command);
+ else syncExec(command);
+ }
+}