/******************************************************************************* * 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 { final Display display; public static IThreadWorkQueue getThreadAccess(Display display) { return new SWTThread(display); } public static IThreadWorkQueue getThreadAccess(Widget widget) { return new SWTThread(widget.getDisplay()); } public static IThreadWorkQueue getThreadAccess() { return new SWTThread(Display.getDefault()); } SWTThread(Display display) { this.display = display; } @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) { syncExec(command); } }