GlueGen: src/java/com/jogamp/common/util/awt/AWTEDTExecutor.java Source File

GlueGen v2.6.0-rc-20250721 GlueGen, Native Binding Generator for Java™ (public API).

AWTEDTExecutor.java
Go to the documentation of this file.
1/**
2 * Copyright 2012 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28 package com.jogamp.common.util.awt;
29
30import java.awt.EventQueue;
31import java.lang.reflect.InvocationTargetException;
32
33import com.jogamp.common.util.RunnableExecutor;
34
35/**
36 * AWT EDT implementation of RunnableExecutor
37 */
38 public class AWTEDTExecutor implements RunnableExecutor {
39 /** {@link RunnableExecutor} implementation invoking {@link Runnable#run()}
40 * on the AWT EDT.
41 */
42 public static final AWTEDTExecutor singleton = new AWTEDTExecutor();
43
44 private AWTEDTExecutor() {}
45
46 @Override
47 public void invoke(final boolean wait, final Runnable r) {
48 if(EventQueue.isDispatchThread()) {
49 r.run();
50 } else {
51 try {
52 if(wait) {
53 EventQueue.invokeAndWait(r);
54 } else {
55 EventQueue.invokeLater(r);
56 }
57 } catch (final InvocationTargetException e) {
58 throw new RuntimeException(e.getTargetException());
59 } catch (final InterruptedException e) {
60 throw new RuntimeException(e);
61 }
62 }
63 }
64
65 /**
66 * Executes the given runnable on the AWT-EDT and return <code>true</code>, if
67 * <ul>
68 * <li>current-thread is the AWT-EDT, <i>or</i></li>
69 * <li>the given tree-lock is not hold by current-thread (-> invoke on AWT-EDT)</li>
70 * </ul>
71 * <p>
72 * Otherwise execute the given runnable on the current-thread and return <code>true</code>, if
73 * <code>allowOnNonEDT</code> is <code>true</code>.<br/>
74 * This implies that the given tree-lock is being hold by the current-thread.
75 * </p>
76 * <p>
77 * Otherwise the runnable is not executed and <code>false</code> is returned.
78 * </p>
79 *
80 * @param treeLock representing the AWT-tree-lock, i.e. {@link java.awt.Component#getTreeLock()}
81 * @param allowOnNonEDT allow execution on non AWT-EDT in case current thread is not AWT-EDT and the tree-lock is being hold
82 * @param wait if true method waits until {@link Runnable#run()} is completed, otherwise don't wait.
83 * @param r the {@link Runnable} to be executed.
84 * @return <code>true</code> if the {@link Runnable} has been issued for execution, otherwise <code>false</code>
85 */
86 public boolean invoke(final Object treeLock, final boolean allowOnNonEDT, final boolean wait, final Runnable r) {
87 if( EventQueue.isDispatchThread() ) {
88 r.run();
89 return true;
90 } else if ( !Thread.holdsLock(treeLock) ) {
91 try {
92 if(wait) {
93 EventQueue.invokeAndWait(r);
94 } else {
95 EventQueue.invokeLater(r);
96 }
97 } catch (final InvocationTargetException e) {
98 throw new RuntimeException(e.getTargetException());
99 } catch (final InterruptedException e) {
100 throw new RuntimeException(e);
101 }
102 return true;
103 } else if ( allowOnNonEDT ) {
104 r.run();
105 return true;
106 } else {
107 return false;
108 }
109 }
110}
com.jogamp.common.util.awt.AWTEDTExecutor
AWT EDT implementation of RunnableExecutor.
Definition: AWTEDTExecutor.java:38
com.jogamp.common.util.awt.AWTEDTExecutor.invoke
void invoke(final boolean wait, final Runnable r)
Definition: AWTEDTExecutor.java:47
com.jogamp.common.util.awt.AWTEDTExecutor.invoke
boolean invoke(final Object treeLock, final boolean allowOnNonEDT, final boolean wait, final Runnable r)
Executes the given runnable on the AWT-EDT and return true, if.
Definition: AWTEDTExecutor.java:86
com.jogamp.common.util.awt.AWTEDTExecutor.singleton
static final AWTEDTExecutor singleton
RunnableExecutor implementation invoking Runnable#run() on the AWT EDT.
Definition: AWTEDTExecutor.java:42
com.jogamp.common.util.RunnableExecutor
Definition: RunnableExecutor.java:30