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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
14 * @author Toni Kalajainen
\r
16 package org.simantics.utils.threads;
\r
18 import java.util.concurrent.Executor;
\r
20 import org.eclipse.swt.widgets.Display;
\r
21 import org.eclipse.swt.widgets.Widget;
\r
23 public class SWTThread implements IThreadWorkQueue, Executor {
\r
25 final Display display;
\r
27 public static IThreadWorkQueue getThreadAccess(Display display)
\r
29 return new SWTThread(display);
\r
32 public static IThreadWorkQueue getThreadAccess(Widget widget)
\r
34 return new SWTThread(widget.getDisplay());
\r
37 public static IThreadWorkQueue getThreadAccess() {
\r
38 return new SWTThread(Display.getDefault());
\r
41 SWTThread(Display display)
\r
43 this.display = display;
\r
47 public Thread asyncExec(Runnable runnable) {
\r
48 // Don't accept work if the SWT thread is disposed.
\r
49 if (display.isDisposed())
\r
51 display.asyncExec(runnable);
\r
52 return display.getThread();
\r
56 public boolean syncExec(Runnable runnable) {
\r
57 // Don't accept work if the SWT thread is disposed.
\r
58 if (display.isDisposed())
\r
60 if (display.getThread() == Thread.currentThread ())
\r
63 display.syncExec(runnable);
\r
68 public boolean currentThreadAccess() {
\r
69 // A disposed SWT thread can never be the current thread access
\r
70 if (display.isDisposed())
\r
72 return display.getThread() == Thread.currentThread ();
\r
76 public Thread getThread() {
\r
77 // Use null to indicate disposed SWT thread.
\r
78 if (display.isDisposed())
\r
80 return display.getThread();
\r
84 public String toString() {
\r
85 return "SWT Thread";
\r
89 public void execute(Runnable command) {
\r