X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.utils.thread%2Fsrc%2Forg%2Fsimantics%2Futils%2Fthreads%2FAWTThread.java;h=3b9d5b258d8a72163e1846dae2425e9c7af5ed3d;hp=aaf53490e2c4b63764c345bc36c1b17cdcc25827;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hpb=24e2b34260f219f0d1644ca7a138894980e25b14 diff --git a/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/AWTThread.java b/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/AWTThread.java index aaf53490e..3b9d5b258 100644 --- a/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/AWTThread.java +++ b/bundles/org.simantics.utils.thread/src/org/simantics/utils/threads/AWTThread.java @@ -1,140 +1,140 @@ -/******************************************************************************* - * 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 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; - } - -} +/******************************************************************************* + * 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 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; + } + +}