]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/AWTThread.java
Merge "Testing SonarQube with Simantics Platform SDK"
[simantics/platform.git] / bundles / org.simantics.utils.thread / src / org / simantics / utils / threads / AWTThread.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 /*\r
13  *\r
14  * @author Toni Kalajainen\r
15  */\r
16 package org.simantics.utils.threads;\r
17 \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
26 \r
27 import javax.swing.SwingUtilities;\r
28 \r
29 \r
30 public class AWTThread extends AbstractExecutorService implements IThreadWorkQueue, Executor {\r
31         \r
32         public static AWTThread INSTANCE = new AWTThread();\r
33         \r
34         public static IThreadWorkQueue getThreadAccess()\r
35         {\r
36                 return INSTANCE;\r
37         }\r
38         \r
39         Field dispatchThread;\r
40         \r
41         AWTThread() {\r
42                 dispatchThread = getDispatchThreadField();\r
43         }\r
44         \r
45         private Field getDispatchThreadField()\r
46         {\r
47                 try {\r
48                         Field f = EventQueue.class.getDeclaredField("dispatchThread");\r
49                         f.setAccessible(true);\r
50                         return f;\r
51                 } catch (SecurityException e) {\r
52                         throw new RuntimeException(e);\r
53                 } catch (NoSuchFieldException e) {\r
54                         throw new RuntimeException(e);\r
55                 }\r
56         }\r
57         \r
58         @Override\r
59         public Thread asyncExec(Runnable runnable) {\r
60                 EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();\r
61                 EventQueue.invokeLater(runnable);\r
62                 try {\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
68                 }\r
69         }\r
70 \r
71         @Override\r
72         public boolean syncExec(Runnable runnable) {\r
73                 if (EventQueue.isDispatchThread())\r
74                 {\r
75                         runnable.run();\r
76                         return true;\r
77                 }\r
78                 \r
79                 try {\r
80                         SwingUtilities.invokeAndWait(runnable);\r
81                         return true;\r
82                 } catch (InterruptedException e) {\r
83                         throw new RuntimeException(e);\r
84                 } catch (InvocationTargetException e) {\r
85                         throw new RuntimeException(e.getCause());\r
86                 }\r
87         }\r
88 \r
89         @Override\r
90         public boolean currentThreadAccess() {\r
91                 return EventQueue.isDispatchThread();\r
92         }\r
93 \r
94         @Override\r
95         public Thread getThread() {\r
96                 EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();\r
97                 try {\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
103                 }\r
104         }\r
105         \r
106         public String toString() {\r
107                 return "AWT Thread";\r
108         }\r
109 \r
110         @Override\r
111         public void execute(Runnable command) {\r
112                 EventQueue.invokeLater(command);\r
113         }\r
114         \r
115         @Override\r
116         public void shutdown() {\r
117         }\r
118 \r
119         @Override\r
120         public List<Runnable> shutdownNow() {\r
121                 return null;\r
122         }\r
123 \r
124         @Override\r
125         public boolean isShutdown() {\r
126                 return false;\r
127         }\r
128 \r
129         @Override\r
130         public boolean isTerminated() {\r
131                 return false;\r
132         }\r
133 \r
134         @Override\r
135         public boolean awaitTermination(long timeout, TimeUnit unit)\r
136                         throws InterruptedException {\r
137                 return false;\r
138         }       \r
139 \r
140 }\r