Package org.jadice.util.base.swing
Class TGASwingUtil
- java.lang.Object
-
- org.jadice.util.base.swing.TGASwingUtil
-
public class TGASwingUtil extends Object
A ThreadGroupAware replacement for SwingUtilities.invokeLater(), SwingUtilities.invokeAndWait() and SwingUtilities.isEventDispatchThread() considering the correct EventQueue to use in environments where more than one EventQueue may occur, such as in Applets and WebStart.
-
-
Constructor Summary
Constructors Constructor Description TGASwingUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <V> Future<V>
callLater(ThreadGroup tg, Callable<V> c)
Schedule the given callable to be called on the EDT and return aFuture
providing access to the result of the call.static <V> Future<V>
callLater(Callable<V> c)
Schedule the given callable to be called on the EDT and return aFuture
providing access to the result of the call.static <V> Future<V>
callLaterIfNotEDT(ThreadGroup tg, Callable<V> c)
Schedule the given callable to be called on the EDT and return aFuture
providing access to the result of the call.static <V> Future<V>
callLaterIfNotEDT(Callable<V> c)
Schedule the given callable to be called on the EDT and return aFuture
providing access to the result of the call.static <V> V
callNow(ThreadGroup tg, Callable<V> c)
Synchronously execute aCallable
and return the result of the call.static <V> V
callNow(Callable<V> c)
Synchronously execute aCallable
and return the result of the call.static void
destroy()
Stops the ExecutionQueues redirecting Runnables to EventQueues and performs cleanup.static void
destroy(ThreadGroup tg)
Stops the ExecutionQueues redirecting Runnables to EventQueues and performs cleanup for the ThreadGroup supplied.static void
ensureEventDispatchThread()
This method will ensure that the calling thread is theevent dispatch thread
.static ThreadGroup
getDefaultThreadGroup()
Gets the default ThreadGroup.static void
init()
Sets the destroyed flag to false so Runnables are directed at the EventQueues.static Throwable
invokeAndWait(Runnable code)
Calls SwingUtilities.invokeAndWait(Runnable code) using the default ThreadGroupstatic Throwable
invokeAndWait(ThreadGroup threadGroup, Runnable code)
Calls SwingUtilities.invokeAndWait(Runnable code) considering the ThreadGroup to be usedstatic void
invokeLater(Runnable code)
Calls SwingUtilities.invokeLater(Runnable code) using the default ThreadGroup.static void
invokeLater(ThreadGroup threadGroup, Runnable code)
Calls SwingUtilities.invokeLater(Runnable code) considering the ThreadGroup to be usedstatic void
invokeLaterIfNotEDT(Runnable code)
Initiate a call of the givenRunnable.run()
method on the event dispatch thread.static void
invokeLaterIfNotEDT(ThreadGroup threadGroup, Runnable code)
Initiate a call of the givenRunnable.run()
method considering the thread group to be used on the event dispatch thread.static boolean
isActive()
Gets the active flag.static boolean
isDestroyed()
static boolean
isEventDispatchThread()
Checks whether the current thread (no matter if EventQueue or not) is the correct EventDispatchThread for the current ThreadGroup.static boolean
isEventDispatchThread(ThreadGroup tg)
Checks whether the current thread (no matter if EventQueue or not) is the correct EventDispatchThread for the current ThreadGroup.static void
setActive(boolean active)
Sets the active flag. if not active, invokeLater, invokeAndWait and isEventDispatchThread will be called on the regular SwingUtilities.static void
setDefaultThreadGroup(ThreadGroup tg)
Sets the default ThreadGroup.
-
-
-
Method Detail
-
getDefaultThreadGroup
public static ThreadGroup getDefaultThreadGroup()
Gets the default ThreadGroup. If not explicitly set, the first one which touches invokerLater or invokeAndWait- Returns:
- the default ThreadGroup. If not explicitly set, the first one which touches invokerLater or invokeAndWait
-
setDefaultThreadGroup
public static void setDefaultThreadGroup(ThreadGroup tg)
Sets the default ThreadGroup.- Parameters:
tg
- the default ThreadGroup.
-
callNow
public static <V> V callNow(ThreadGroup tg, Callable<V> c)
Synchronously execute aCallable
and return the result of the call. If this method is called off-EDT, the call will block until the callable has been called on the EDT. If it is called on-EDT, the callable will be called immediately.- Parameters:
tg
- the ThreadGroup to be usedc
- the Callable whose run method should be executed on a ThreadGroup specific EventQueue- Returns:
- Throwable Return the throwable the Runnable.run() method may throw as an uncaught
exception (on the event dispatching thread) or
null
if no throwable was thrown.
-
callNow
public static <V> V callNow(Callable<V> c)
Synchronously execute aCallable
and return the result of the call. If this method is called off-EDT, the call will block until the callable has been called on the EDT. If it is called on-EDT, the callable will be called immediately.- Parameters:
c
- the Callable whoseCallable.call()
is to be executed on a ThreadGroup specific EventQueue- Returns:
- Throwable Return the throwable the Runnable.run() method may throw as an uncaught
exception (on the event dispatching thread) or
null
if no throwable was thrown.
-
callLater
public static <V> Future<V> callLater(Callable<V> c)
Schedule the given callable to be called on the EDT and return aFuture
providing access to the result of the call.- Type Parameters:
V
- the type of result returned by the callable- Parameters:
c
- the Callable whoseCallable.call()
is to be executed on a ThreadGroup specific EventQueue- Returns:
- a
Future
providing access to the result of the call.
-
callLaterIfNotEDT
public static <V> Future<V> callLaterIfNotEDT(Callable<V> c)
Schedule the given callable to be called on the EDT and return aFuture
providing access to the result of the call. If the current thread is already the event dispatch thread theCallable
is called synchronously. Otherwise it will be enqueued in the event dispatch thread to be be called asynchronously.- Type Parameters:
V
- the type of result returned by the callable- Parameters:
c
- the Callable whoseCallable.call()
is to be executed on a ThreadGroup specific EventQueue- Returns:
- a
Future
providing access to the result of the call.
-
callLaterIfNotEDT
public static <V> Future<V> callLaterIfNotEDT(ThreadGroup tg, Callable<V> c)
Schedule the given callable to be called on the EDT and return aFuture
providing access to the result of the call. If the current thread is already the event dispatch thread theCallable
is called synchronously. Otherwise it will be enqueued in the event dispatch thread to be be called asynchronously.- Type Parameters:
V
- the type of result returned by the callable- Parameters:
tg
- the ThreadGroup to be usedc
- the Callable whoseCallable.call()
is to be executed on a ThreadGroup specific EventQueue- Returns:
- a
Future
providing access to the result of the call.
-
callLater
public static <V> Future<V> callLater(ThreadGroup tg, Callable<V> c)
Schedule the given callable to be called on the EDT and return aFuture
providing access to the result of the call.- Type Parameters:
V
- the type of result returned by the callable- Parameters:
tg
- the ThreadGroup to be usedc
- the Callable whoseCallable.call()
is to be executed on a ThreadGroup specific EventQueue- Returns:
- a
Future
providing access to the result of the call.
-
invokeAndWait
public static Throwable invokeAndWait(Runnable code)
Calls SwingUtilities.invokeAndWait(Runnable code) using the default ThreadGroup- Parameters:
code
- the Runnable whose run method should be executed on a ThreadGroup specific EventQueue- Returns:
- Throwable Return the throwable the Runnable.run() method may throw as an uncaught
exception (on the event dispatching thread) or
null
if no throwable was thrown.
-
invokeAndWait
public static Throwable invokeAndWait(ThreadGroup threadGroup, Runnable code)
Calls SwingUtilities.invokeAndWait(Runnable code) considering the ThreadGroup to be used- Parameters:
threadGroup
- the ThreadGroup to be usedcode
- the Runnable whose run method should be executed on a ThreadGroup specific EventQueue- Returns:
- Throwable Return the throwable the Runnable.run() method may throw as an uncaught
exception (on the event dispatching thread) or
null
if no throwable was thrown.
-
invokeLaterIfNotEDT
public static void invokeLaterIfNotEDT(Runnable code)
Initiate a call of the givenRunnable.run()
method on the event dispatch thread. If the current thread is already the event dispatch thread theRunnable.run()
method is called synchronously. Otherwise theRunnable.run()
will be enqueued in the event dispatch thread to be be called asynchronously.- Parameters:
code
- the Runnable whose run method should be executed on a ThreadGroup specific EventQueue- See Also:
invokeLaterIfNotEDT(ThreadGroup, Runnable)
,invokeLater(Runnable)
,invokeLater(ThreadGroup, Runnable)
,isEventDispatchThread()
,isEventDispatchThread(ThreadGroup)
-
invokeLaterIfNotEDT
public static void invokeLaterIfNotEDT(ThreadGroup threadGroup, Runnable code)
Initiate a call of the givenRunnable.run()
method considering the thread group to be used on the event dispatch thread. If the current thread is already the event dispatch thread for the given thread group theRunnable.run()
method is called synchronously. Otherwise theRunnable.run()
will be enqueued in the approbate event dispatch thread to be called asynchronously.- Parameters:
code
- the Runnable whose run method should be executed on a ThreadGroup specific EventQueuethreadGroup
- the ThreadGroup to be usedcode
- the Runnable whose run method should be executed on a thread group specific EventQueue- See Also:
invokeLaterIfNotEDT(Runnable)
,invokeLater(Runnable)
,invokeLater(ThreadGroup, Runnable)
,isEventDispatchThread()
,isEventDispatchThread(ThreadGroup)
-
invokeLater
public static void invokeLater(Runnable code)
Calls SwingUtilities.invokeLater(Runnable code) using the default ThreadGroup.- Parameters:
code
- the Runnable whose run method should be executed on a ThreadGroup specific EventQueue
-
invokeLater
public static void invokeLater(ThreadGroup threadGroup, Runnable code)
Calls SwingUtilities.invokeLater(Runnable code) considering the ThreadGroup to be used- Parameters:
threadGroup
- the ThreadGroup to be usedcode
- the Runnable whose run method should be executed on a ThreadGroup specific EventQueue
-
destroy
public static void destroy()
Stops the ExecutionQueues redirecting Runnables to EventQueues and performs cleanup.
-
destroy
public static void destroy(ThreadGroup tg)
Stops the ExecutionQueues redirecting Runnables to EventQueues and performs cleanup for the ThreadGroup supplied.- Parameters:
tg
- the threadGroup to be used for removing the execution queues
-
isActive
public static boolean isActive()
Gets the active flag.- Returns:
- the active flag.
-
setActive
public static void setActive(boolean active)
Sets the active flag. if not active, invokeLater, invokeAndWait and isEventDispatchThread will be called on the regular SwingUtilities.- Parameters:
active
- the active flag to set.
-
isEventDispatchThread
public static boolean isEventDispatchThread()
Checks whether the current thread (no matter if EventQueue or not) is the correct EventDispatchThread for the current ThreadGroup. Returns false, if not.- Returns:
- whether the current thread (no matter if EventQueue or not) is the correct EventDispatchThread for the current ThreadGroup. Returns false, if not.
-
isEventDispatchThread
public static boolean isEventDispatchThread(ThreadGroup tg)
Checks whether the current thread (no matter if EventQueue or not) is the correct EventDispatchThread for the current ThreadGroup. Returns false, if not.- Parameters:
tg
- the threadGroup to be checked- Returns:
- whether the current thread (no matter if EventQueue or not) is the correct EventDispatchThread for the current ThreadGroup. Returns false, if not.
-
isDestroyed
public static boolean isDestroyed()
-
init
public static void init()
Sets the destroyed flag to false so Runnables are directed at the EventQueues.- See Also:
destroy()
-
ensureEventDispatchThread
public static void ensureEventDispatchThread() throws IllegalStateException
This method will ensure that the calling thread is theevent dispatch thread
. If not this method will throw aIllegalStateException
.- Throws:
IllegalStateException
- if the calling thread is not theevent dispatch thread
-
-