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