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.awt.EventQueue;
\r
19 import java.awt.Toolkit;
\r
20 import java.lang.reflect.Field;
\r
21 import java.lang.reflect.InvocationTargetException;
\r
22 import java.util.List;
\r
23 import java.util.concurrent.AbstractExecutorService;
\r
24 import java.util.concurrent.Executor;
\r
25 import java.util.concurrent.TimeUnit;
\r
27 import javax.swing.SwingUtilities;
\r
30 public class AWTThread extends AbstractExecutorService implements IThreadWorkQueue, Executor {
\r
32 public static AWTThread INSTANCE = new AWTThread();
\r
34 public static IThreadWorkQueue getThreadAccess()
\r
39 Field dispatchThread;
\r
42 dispatchThread = getDispatchThreadField();
\r
45 private Field getDispatchThreadField()
\r
48 Field f = EventQueue.class.getDeclaredField("dispatchThread");
\r
49 f.setAccessible(true);
\r
51 } catch (SecurityException e) {
\r
52 throw new RuntimeException(e);
\r
53 } catch (NoSuchFieldException e) {
\r
54 throw new RuntimeException(e);
\r
59 public Thread asyncExec(Runnable runnable) {
\r
60 EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
\r
61 EventQueue.invokeLater(runnable);
\r
63 return (Thread) dispatchThread.get(eq);
\r
64 } catch (IllegalArgumentException e1) {
\r
65 throw new RuntimeException(e1);
\r
66 } catch (IllegalAccessException e1) {
\r
67 throw new RuntimeException(e1);
\r
72 public boolean syncExec(Runnable runnable) {
\r
73 if (EventQueue.isDispatchThread())
\r
80 SwingUtilities.invokeAndWait(runnable);
\r
82 } catch (InterruptedException e) {
\r
83 throw new RuntimeException(e);
\r
84 } catch (InvocationTargetException e) {
\r
85 throw new RuntimeException(e.getCause());
\r
90 public boolean currentThreadAccess() {
\r
91 return EventQueue.isDispatchThread();
\r
95 public Thread getThread() {
\r
96 EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
\r
98 return (Thread) dispatchThread.get(eq);
\r
99 } catch (IllegalArgumentException e1) {
\r
100 throw new RuntimeException(e1);
\r
101 } catch (IllegalAccessException e1) {
\r
102 throw new RuntimeException(e1);
\r
106 public String toString() {
\r
107 return "AWT Thread";
\r
111 public void execute(Runnable command) {
\r
112 EventQueue.invokeLater(command);
\r
116 public void shutdown() {
\r
120 public List<Runnable> shutdownNow() {
\r
125 public boolean isShutdown() {
\r
130 public boolean isTerminated() {
\r
135 public boolean awaitTermination(long timeout, TimeUnit unit)
\r
136 throws InterruptedException {
\r