diff options
author | Sven Gothel <[email protected]> | 2012-09-10 19:12:42 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2012-09-10 19:12:42 +0200 |
commit | 6fd3dea1ae94fd5862276ee5ed426276560531da (patch) | |
tree | 9b25cc77b82dd29f27f3c7c5c3883473139c14be /src/java/jogamp/common | |
parent | 7af0a33fd5855d4682050945a06b47fa922c4eeb (diff) |
AWTEDTExecutor: Add convenient "invoke(Object treeLock, boolean wait, Runnable r)" to be used directly
Diffstat (limited to 'src/java/jogamp/common')
-rw-r--r-- | src/java/jogamp/common/awt/AWTEDTExecutor.java | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/java/jogamp/common/awt/AWTEDTExecutor.java b/src/java/jogamp/common/awt/AWTEDTExecutor.java index dcf9818..67fd7e1 100644 --- a/src/java/jogamp/common/awt/AWTEDTExecutor.java +++ b/src/java/jogamp/common/awt/AWTEDTExecutor.java @@ -39,7 +39,7 @@ public class AWTEDTExecutor implements RunnableExecutor { /** {@link RunnableExecutor} implementation invoking {@link Runnable#run()} * on the AWT EDT. */ - public static final RunnableExecutor singleton = new AWTEDTExecutor(); + public static final AWTEDTExecutor singleton = new AWTEDTExecutor(); private AWTEDTExecutor() {} @@ -62,4 +62,28 @@ public class AWTEDTExecutor implements RunnableExecutor { } } + /** + * Executes the given runnable on the AWT EDT if current thread is not the EDT and the given tree + * lock is not hold, otherwise execute the runnable in current thread. + * @param treeLock representing the AWT-tree-lock, i.e. {@link java.awt.Component#getTreeLock()} + * @param wait if true method waits until {@link Runnable#run()} is completed, otherwise don't wait. + * @param r the {@link Runnable} to be executed. + */ + public void invoke(Object treeLock, boolean wait, Runnable r) { + if(EventQueue.isDispatchThread() || Thread.holdsLock(treeLock)) { + r.run(); + } else { + try { + if(wait) { + EventQueue.invokeAndWait(r); + } else { + EventQueue.invokeLater(r); + } + } catch (InvocationTargetException e) { + throw new RuntimeException(e.getTargetException()); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } } |