com.levigo.util.swing
Class TGASwingUtil

java.lang.Object
  extended by com.levigo.util.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.

Author:
o.suck@levigo.de

Constructor Summary
TGASwingUtil()
           
 
Method Summary
static
<V> Future<V>
callLater(Callable<V> c)
          Schedule the given callable to be called on the EDT and return a Future providing access to the result of the call.
static
<V> Future<V>
callLater(ThreadGroup tg, Callable<V> c)
          Schedule the given callable to be called on the EDT and return a Future providing access to the result of the call.
static
<V> V
callNow(Callable<V> c)
          Synchronously execute a Callable and return the result of the call.
static
<V> V
callNow(ThreadGroup tg, Callable<V> c)
          Synchronously execute a Callable 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 the event dispatch thread.
static void ensureEventDispatchThread(Component c)
          Deprecated. 
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 ThreadGroup
static Throwable invokeAndWait(ThreadGroup threadGroup, Runnable code)
          Calls SwingUtilities.invokeAndWait(Runnable code) considering the ThreadGroup to be used
static 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 used
static void invokeLaterIfNotEDT(Runnable code)
          Initiate a call of the given Runnable.run() method on the event dispatch thread.
static void invokeLaterIfNotEDT(ThreadGroup threadGroup, Runnable code)
          Initiate a call of the given Runnable.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 main(String[] args)
           
static void setActive(boolean active)
          Sets the active flag.
static void setDefaultThreadGroup(ThreadGroup tg)
          Sets the default ThreadGroup.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TGASwingUtil

public TGASwingUtil()
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 a Callable 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 used
c - 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 a Callable 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 whose Callable.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 a Future providing access to the result of the call.

Type Parameters:
V - the type of result returned by the callable
Parameters:
c - the Callable whose Callable.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 a Future 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 used
c - the Callable whose Callable.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 used
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.

invokeLaterIfNotEDT

public static void invokeLaterIfNotEDT(Runnable code)
Initiate a call of the given Runnable.run() method on the event dispatch thread. If the current thread is already the event dispatch thread the Runnable.run() method is called synchronously. Otherwise the Runnable.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 given Runnable.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 the Runnable.run() method is called synchronously. Otherwise the Runnable.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 EventQueue
threadGroup - the ThreadGroup to be used
code - 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 used
code - 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 the event dispatch thread. If not this method will throw a IllegalStateException.

Throws:
IllegalStateException - if the calling thread is not the event dispatch thread

ensureEventDispatchThread

@Deprecated
public static void ensureEventDispatchThread(Component c)
                                      throws IllegalStateException
Deprecated. 

This method will ensure that the calling thread is the event dispatch thread. If not this method will throw a IllegalStateException.

NOTE: The previous implementation has been checking whether the component is displayable or not. With java 1.4 it has been considered legal practice to construct the UI tree Off-EDT. This has been changed with the newer versions of the JavaVM and the Swing implementation.

Throws:
IllegalStateException - if the calling thread is not the event dispatch thread

main

public static void main(String[] args)


Copyright © 1995-2020 levigo holding gmbh. All Rights Reserved.