]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/AWTThread.java
Stop using pack200 because it has been deprecated and removed
[simantics/platform.git] / bundles / org.simantics.utils.thread / src / org / simantics / utils / threads / AWTThread.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 /*
13  *
14  * @author Toni Kalajainen
15  */
16 package org.simantics.utils.threads;
17
18 import java.awt.EventQueue;
19 import java.awt.Toolkit;
20 import java.lang.reflect.Field;
21 import java.lang.reflect.InvocationTargetException;
22 import java.util.List;
23 import java.util.concurrent.AbstractExecutorService;
24 import java.util.concurrent.Executor;
25 import java.util.concurrent.TimeUnit;
26
27 import javax.swing.SwingUtilities;
28
29
30 public class AWTThread extends AbstractExecutorService implements IThreadWorkQueue, Executor {
31         
32         public static AWTThread INSTANCE = new AWTThread();
33         
34         public static IThreadWorkQueue getThreadAccess()
35         {
36                 return INSTANCE;
37         }
38         
39         Field dispatchThread;
40         
41         AWTThread() {
42                 dispatchThread = getDispatchThreadField();
43         }
44         
45         private Field getDispatchThreadField()
46         {
47                 try {
48                         Field f = EventQueue.class.getDeclaredField("dispatchThread");
49                         f.setAccessible(true);
50                         return f;
51                 } catch (SecurityException e) {
52                         throw new RuntimeException(e);
53                 } catch (NoSuchFieldException e) {
54                         throw new RuntimeException(e);
55                 }
56         }
57         
58         @Override
59         public Thread asyncExec(Runnable runnable) {
60                 EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
61                 EventQueue.invokeLater(runnable);
62                 try {
63                         return (Thread) dispatchThread.get(eq);
64                 } catch (IllegalArgumentException e1) {
65                         throw new RuntimeException(e1);
66                 } catch (IllegalAccessException e1) {
67                         throw new RuntimeException(e1);
68                 }
69         }
70
71         @Override
72         public boolean syncExec(Runnable runnable) {
73                 if (EventQueue.isDispatchThread())
74                 {
75                         runnable.run();
76                         return true;
77                 }
78                 
79                 try {
80                         SwingUtilities.invokeAndWait(runnable);
81                         return true;
82                 } catch (InterruptedException e) {
83                         throw new RuntimeException(e);
84                 } catch (InvocationTargetException e) {
85                         throw new RuntimeException(e.getCause());
86                 }
87         }
88
89         @Override
90         public boolean currentThreadAccess() {
91                 return EventQueue.isDispatchThread();
92         }
93
94         @Override
95         public Thread getThread() {
96                 EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
97                 try {
98                         return (Thread) dispatchThread.get(eq);
99                 } catch (IllegalArgumentException e1) {
100                         throw new RuntimeException(e1);
101                 } catch (IllegalAccessException e1) {
102                         throw new RuntimeException(e1);
103                 }
104         }
105         
106         public String toString() {
107                 return "AWT Thread";
108         }
109
110         @Override
111         public void execute(Runnable command) {
112                 EventQueue.invokeLater(command);
113         }
114         
115         @Override
116         public void shutdown() {
117         }
118
119         @Override
120         public List<Runnable> shutdownNow() {
121                 return null;
122         }
123
124         @Override
125         public boolean isShutdown() {
126                 return false;
127         }
128
129         @Override
130         public boolean isTerminated() {
131                 return false;
132         }
133
134         @Override
135         public boolean awaitTermination(long timeout, TimeUnit unit)
136                         throws InterruptedException {
137                 return false;
138         }       
139
140 }