]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/AWTThread.java
Move platform to only work with JDK's >= 11
[simantics/platform.git] / bundles / org.simantics.utils.thread / src / org / simantics / utils / threads / AWTThread.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2020 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  *     Semantum Oy - gitlab #522 - removed reflection hack
12  *******************************************************************************/
13 /*
14  *
15  * @author Toni Kalajainen
16  */
17 package org.simantics.utils.threads;
18
19 import java.awt.EventQueue;
20 import java.lang.reflect.InvocationTargetException;
21 import java.util.List;
22 import java.util.concurrent.AbstractExecutorService;
23 import java.util.concurrent.Executor;
24 import java.util.concurrent.TimeUnit;
25
26 import javax.swing.SwingUtilities;
27
28
29 public class AWTThread extends AbstractExecutorService implements IThreadWorkQueue, Executor {
30         
31         public static AWTThread INSTANCE = new AWTThread();
32         
33         public static IThreadWorkQueue getThreadAccess()
34         {
35                 return INSTANCE;
36         }
37         
38         @Override
39         public Thread asyncExec(Runnable runnable) {
40                 EventQueue.invokeLater(runnable);
41                 return null;
42         }
43
44         @Override
45         public boolean syncExec(Runnable runnable) {
46                 if (EventQueue.isDispatchThread())
47                 {
48                         runnable.run();
49                         return true;
50                 }
51                 
52                 try {
53                         SwingUtilities.invokeAndWait(runnable);
54                         return true;
55                 } catch (InterruptedException e) {
56                         throw new RuntimeException(e);
57                 } catch (InvocationTargetException e) {
58                         throw new RuntimeException(e.getCause());
59                 }
60         }
61
62         @Override
63         public boolean currentThreadAccess() {
64                 return EventQueue.isDispatchThread();
65         }
66
67         @Override
68         public Thread getThread() {
69                 return null;
70         }
71         
72         public String toString() {
73                 return "AWT Thread";
74         }
75
76         @Override
77         public void execute(Runnable command) {
78                 EventQueue.invokeLater(command);
79         }
80         
81         @Override
82         public void shutdown() {
83         }
84
85         @Override
86         public List<Runnable> shutdownNow() {
87                 return null;
88         }
89
90         @Override
91         public boolean isShutdown() {
92                 return false;
93         }
94
95         @Override
96         public boolean isTerminated() {
97                 return false;
98         }
99
100         @Override
101         public boolean awaitTermination(long timeout, TimeUnit unit)
102                         throws InterruptedException {
103                 return false;
104         }
105
106 }