1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
14 * @author Toni Kalajainen
16 package org.simantics.utils.threads;
18 import java.util.concurrent.Executor;
20 import org.eclipse.swt.widgets.Display;
21 import org.eclipse.swt.widgets.Widget;
23 public class SWTThread implements IThreadWorkQueue, Executor {
25 final Display display;
27 public static IThreadWorkQueue getThreadAccess(Display display)
29 return new SWTThread(display);
32 public static IThreadWorkQueue getThreadAccess(Widget widget)
34 return new SWTThread(widget.getDisplay());
37 public static IThreadWorkQueue getThreadAccess() {
38 return new SWTThread(Display.getDefault());
41 SWTThread(Display display)
43 this.display = display;
47 public Thread asyncExec(Runnable runnable) {
48 // Don't accept work if the SWT thread is disposed.
49 if (display.isDisposed())
51 display.asyncExec(runnable);
52 return display.getThread();
56 public boolean syncExec(Runnable runnable) {
57 // Don't accept work if the SWT thread is disposed.
58 if (display.isDisposed())
60 if (display.getThread() == Thread.currentThread ())
63 display.syncExec(runnable);
68 public boolean currentThreadAccess() {
69 // A disposed SWT thread can never be the current thread access
70 if (display.isDisposed())
72 return display.getThread() == Thread.currentThread ();
76 public Thread getThread() {
77 // Use null to indicate disposed SWT thread.
78 if (display.isDisposed())
80 return display.getThread();
84 public String toString() {
89 public void execute(Runnable command) {