/******************************************************************************* * Copyright (c) 2007, 2020 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 * Semantum Oy - gitlab #522 - removed reflection hack *******************************************************************************/ /* * * @author Toni Kalajainen */ package org.simantics.utils.threads; import java.awt.EventQueue; import java.lang.reflect.InvocationTargetException; import java.util.List; import java.util.concurrent.AbstractExecutorService; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import javax.swing.SwingUtilities; public class AWTThread extends AbstractExecutorService implements IThreadWorkQueue, Executor { public static AWTThread INSTANCE = new AWTThread(); public static IThreadWorkQueue getThreadAccess() { return INSTANCE; } @Override public Thread asyncExec(Runnable runnable) { EventQueue.invokeLater(runnable); return null; } @Override public boolean syncExec(Runnable runnable) { if (EventQueue.isDispatchThread()) { runnable.run(); return true; } try { SwingUtilities.invokeAndWait(runnable); return true; } catch (InterruptedException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e.getCause()); } } @Override public boolean currentThreadAccess() { return EventQueue.isDispatchThread(); } @Override public Thread getThread() { return null; } public String toString() { return "AWT Thread"; } @Override public void execute(Runnable command) { EventQueue.invokeLater(command); } @Override public void shutdown() { } @Override public List shutdownNow() { return null; } @Override public boolean isShutdown() { return false; } @Override public boolean isTerminated() { return false; } @Override public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { return false; } }