]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.thread.swt/src/org/simantics/utils/threads/SWTThread.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.thread.swt / src / org / simantics / utils / threads / SWTThread.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.util.concurrent.Executor;\r
19 \r
20 import org.eclipse.swt.widgets.Display;\r
21 import org.eclipse.swt.widgets.Widget;\r
22 \r
23 public class SWTThread implements IThreadWorkQueue, Executor {\r
24 \r
25         final Display display;\r
26         \r
27         public static IThreadWorkQueue getThreadAccess(Display display)\r
28         {\r
29                 return new SWTThread(display);\r
30         }\r
31         \r
32         public static IThreadWorkQueue getThreadAccess(Widget widget)\r
33         {\r
34                 return new SWTThread(widget.getDisplay());\r
35         }\r
36         \r
37         public static IThreadWorkQueue getThreadAccess() {\r
38                 return new SWTThread(Display.getDefault());\r
39         }\r
40         \r
41         SWTThread(Display display)\r
42         {\r
43                 this.display = display;\r
44         }\r
45         \r
46         @Override\r
47         public Thread asyncExec(Runnable runnable) {\r
48                 // Don't accept work if the SWT thread is disposed.\r
49                 if (display.isDisposed())\r
50                         return null;\r
51                 display.asyncExec(runnable);\r
52                 return display.getThread();\r
53         }\r
54 \r
55         @Override\r
56         public boolean syncExec(Runnable runnable) {\r
57                 // Don't accept work if the SWT thread is disposed.\r
58                 if (display.isDisposed())\r
59                         return false;\r
60                 if (display.getThread() == Thread.currentThread ())\r
61                         runnable.run();\r
62                 else \r
63                         display.syncExec(runnable);\r
64                 return true;\r
65         }\r
66 \r
67         @Override\r
68         public boolean currentThreadAccess() {\r
69                 // A disposed SWT thread can never be the current thread access\r
70                 if (display.isDisposed())\r
71                         return false;\r
72                 return display.getThread() == Thread.currentThread ();\r
73         }\r
74 \r
75         @Override\r
76         public Thread getThread() {\r
77                 // Use null to indicate disposed SWT thread.\r
78                 if (display.isDisposed())\r
79                         return null;\r
80                 return display.getThread();\r
81         }\r
82         \r
83         @Override\r
84         public String toString() {\r
85                 return "SWT Thread";\r
86         }\r
87 \r
88         @Override\r
89         public void execute(Runnable command) {\r
90                 syncExec(command);\r
91         }\r
92 }\r