-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-/*\r
- *\r
- * @author Toni Kalajainen\r
- */\r
-package org.simantics.utils.threads;\r
-\r
-import java.awt.EventQueue;\r
-import java.awt.Toolkit;\r
-import java.lang.reflect.Field;\r
-import java.lang.reflect.InvocationTargetException;\r
-import java.util.List;\r
-import java.util.concurrent.AbstractExecutorService;\r
-import java.util.concurrent.Executor;\r
-import java.util.concurrent.TimeUnit;\r
-\r
-import javax.swing.SwingUtilities;\r
-\r
-\r
-public class AWTThread extends AbstractExecutorService implements IThreadWorkQueue, Executor {\r
- \r
- public static AWTThread INSTANCE = new AWTThread();\r
- \r
- public static IThreadWorkQueue getThreadAccess()\r
- {\r
- return INSTANCE;\r
- }\r
- \r
- Field dispatchThread;\r
- \r
- AWTThread() {\r
- dispatchThread = getDispatchThreadField();\r
- }\r
- \r
- private Field getDispatchThreadField()\r
- {\r
- try {\r
- Field f = EventQueue.class.getDeclaredField("dispatchThread");\r
- f.setAccessible(true);\r
- return f;\r
- } catch (SecurityException e) {\r
- throw new RuntimeException(e);\r
- } catch (NoSuchFieldException e) {\r
- throw new RuntimeException(e);\r
- }\r
- }\r
- \r
- @Override\r
- public Thread asyncExec(Runnable runnable) {\r
- EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();\r
- EventQueue.invokeLater(runnable);\r
- try {\r
- return (Thread) dispatchThread.get(eq);\r
- } catch (IllegalArgumentException e1) {\r
- throw new RuntimeException(e1);\r
- } catch (IllegalAccessException e1) {\r
- throw new RuntimeException(e1);\r
- }\r
- }\r
-\r
- @Override\r
- public boolean syncExec(Runnable runnable) {\r
- if (EventQueue.isDispatchThread())\r
- {\r
- runnable.run();\r
- return true;\r
- }\r
- \r
- try {\r
- SwingUtilities.invokeAndWait(runnable);\r
- return true;\r
- } catch (InterruptedException e) {\r
- throw new RuntimeException(e);\r
- } catch (InvocationTargetException e) {\r
- throw new RuntimeException(e.getCause());\r
- }\r
- }\r
-\r
- @Override\r
- public boolean currentThreadAccess() {\r
- return EventQueue.isDispatchThread();\r
- }\r
-\r
- @Override\r
- public Thread getThread() {\r
- EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();\r
- try {\r
- return (Thread) dispatchThread.get(eq);\r
- } catch (IllegalArgumentException e1) {\r
- throw new RuntimeException(e1);\r
- } catch (IllegalAccessException e1) {\r
- throw new RuntimeException(e1);\r
- }\r
- }\r
- \r
- public String toString() {\r
- return "AWT Thread";\r
- }\r
-\r
- @Override\r
- public void execute(Runnable command) {\r
- EventQueue.invokeLater(command);\r
- }\r
- \r
- @Override\r
- public void shutdown() {\r
- }\r
-\r
- @Override\r
- public List<Runnable> shutdownNow() {\r
- return null;\r
- }\r
-\r
- @Override\r
- public boolean isShutdown() {\r
- return false;\r
- }\r
-\r
- @Override\r
- public boolean isTerminated() {\r
- return false;\r
- }\r
-\r
- @Override\r
- public boolean awaitTermination(long timeout, TimeUnit unit)\r
- throws InterruptedException {\r
- return false;\r
- } \r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+/*
+ *
+ * @author Toni Kalajainen
+ */
+package org.simantics.utils.threads;
+
+import java.awt.EventQueue;
+import java.awt.Toolkit;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.util.List;
+import java.util.concurrent.AbstractExecutorService;
+import java.util.concurrent.Executor;
+import java.util.concurrent.TimeUnit;
+
+import javax.swing.SwingUtilities;
+
+
+public class AWTThread extends AbstractExecutorService implements IThreadWorkQueue, Executor {
+
+ public static AWTThread INSTANCE = new AWTThread();
+
+ public static IThreadWorkQueue getThreadAccess()
+ {
+ return INSTANCE;
+ }
+
+ Field dispatchThread;
+
+ AWTThread() {
+ dispatchThread = getDispatchThreadField();
+ }
+
+ private Field getDispatchThreadField()
+ {
+ try {
+ Field f = EventQueue.class.getDeclaredField("dispatchThread");
+ f.setAccessible(true);
+ return f;
+ } catch (SecurityException e) {
+ throw new RuntimeException(e);
+ } catch (NoSuchFieldException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public Thread asyncExec(Runnable runnable) {
+ EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
+ EventQueue.invokeLater(runnable);
+ try {
+ return (Thread) dispatchThread.get(eq);
+ } catch (IllegalArgumentException e1) {
+ throw new RuntimeException(e1);
+ } catch (IllegalAccessException e1) {
+ throw new RuntimeException(e1);
+ }
+ }
+
+ @Override
+ public boolean syncExec(Runnable runnable) {
+ if (EventQueue.isDispatchThread())
+ {
+ runnable.run();
+ return true;
+ }
+
+ try {
+ SwingUtilities.invokeAndWait(runnable);
+ return true;
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e.getCause());
+ }
+ }
+
+ @Override
+ public boolean currentThreadAccess() {
+ return EventQueue.isDispatchThread();
+ }
+
+ @Override
+ public Thread getThread() {
+ EventQueue eq = Toolkit.getDefaultToolkit().getSystemEventQueue();
+ try {
+ return (Thread) dispatchThread.get(eq);
+ } catch (IllegalArgumentException e1) {
+ throw new RuntimeException(e1);
+ } catch (IllegalAccessException e1) {
+ throw new RuntimeException(e1);
+ }
+ }
+
+ public String toString() {
+ return "AWT Thread";
+ }
+
+ @Override
+ public void execute(Runnable command) {
+ EventQueue.invokeLater(command);
+ }
+
+ @Override
+ public void shutdown() {
+ }
+
+ @Override
+ public List<Runnable> shutdownNow() {
+ return null;
+ }
+
+ @Override
+ public boolean isShutdown() {
+ return false;
+ }
+
+ @Override
+ public boolean isTerminated() {
+ return false;
+ }
+
+ @Override
+ public boolean awaitTermination(long timeout, TimeUnit unit)
+ throws InterruptedException {
+ return false;
+ }
+
+}