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