X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.utils.thread.swt%2Fsrc%2Forg%2Fsimantics%2Futils%2Fthreads%2FSWTThread.java;h=07102a102cd8b861210a31d9ec26de60478b8c6c;hb=24b69f15e78bfdd5cdb30d4b135c4c734061914e;hp=482c6710e2c884860b91d60b202874656864a6a3;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git 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 index 482c6710e..07102a102 100644 --- 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 @@ -1,92 +1,100 @@ -/******************************************************************************* - * 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); - } -} +/******************************************************************************* + * 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); + } +}