Package ghidra.util

Class Swing


  • public class Swing
    extends java.lang.Object
    A utility class to handle running code on the AWT Event Dispatch Thread
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void allowSwingToProcessEvents()
      Wait until AWT event queue (Swing) has been flushed and no more (to a point) events are pending.
      static boolean assertSwingThread​(java.lang.String errorMessage)
      Logs a stack trace if the current calling thread is not the Swing thread
      static boolean isSwingThread()
      Returns true if this is the event dispatch thread.
      static void runIfSwingOrRunLater​(java.lang.Runnable r)
      Runs the given runnable now if the caller is on the Swing thread.
      static void runLater​(java.lang.Runnable r)
      Calls the given runnable on the Swing thread in the future by putting the request on the back of the event queue.
      static void runNow​(java.lang.Runnable r)
      Calls the given runnable on the Swing thread
      static void runNow​(java.lang.Runnable r, long timeout, java.util.concurrent.TimeUnit unit)
      Calls the given runnable on the Swing thread
      static <T> T runNow​(java.util.function.Supplier<T> s)
      Calls the given suppler on the Swing thread, blocking with a SwingUtilities.invokeAndWait(Runnable) if not on the Swing thread.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • GSWING_THREAD_POOL_NAME

        public static final java.lang.String GSWING_THREAD_POOL_NAME
        See Also:
        Constant Field Values
    • Method Detail

      • isSwingThread

        public static boolean isSwingThread()
        Returns true if this is the event dispatch thread. Note that this method returns true in headless mode because any thread in headless mode can dispatch its own events. In swing environments, the swing thread is usually used to dispatch events.
        Returns:
        true if this is the event dispatch thread -OR- is in headless mode.
      • allowSwingToProcessEvents

        public static void allowSwingToProcessEvents()
        Wait until AWT event queue (Swing) has been flushed and no more (to a point) events are pending.
      • assertSwingThread

        public static boolean assertSwingThread​(java.lang.String errorMessage)
        Logs a stack trace if the current calling thread is not the Swing thread
        Parameters:
        errorMessage - The message to display when not on the Swing thread
        Returns:
        true if the calling thread is the Swing thread
      • runLater

        public static void runLater​(java.lang.Runnable r)
        Calls the given runnable on the Swing thread in the future by putting the request on the back of the event queue.
        Parameters:
        r - the runnable
      • runIfSwingOrRunLater

        public static void runIfSwingOrRunLater​(java.lang.Runnable r)
        Runs the given runnable now if the caller is on the Swing thread. Otherwise, the runnable will be posted later.
        Parameters:
        r - the runnable
      • runNow

        public static <T> T runNow​(java.util.function.Supplier<T> s)
        Calls the given suppler on the Swing thread, blocking with a SwingUtilities.invokeAndWait(Runnable) if not on the Swing thread.

        Use this method when you are not on the Swing thread and you need to get a value that is managed/synchronized by the Swing thread.

         		String value = runNow(() -> label.getText());
         
        Parameters:
        s - the supplier that will be called on the Swing thread
        Returns:
        the result of the supplier
        See Also:
        runNow(Runnable)
      • runNow

        public static void runNow​(java.lang.Runnable r,
                                  long timeout,
                                  java.util.concurrent.TimeUnit unit)
                           throws UnableToSwingException
        Calls the given runnable on the Swing thread

        This method will throw an exception if the Swing thread is not available within the given timeout. This method is useful for preventing deadlocks.

        Parameters:
        r - the runnable
        timeout - the timeout value
        unit - the time unit of the timeout value
        Throws:
        UnableToSwingException - if the timeout was reach waiting for the Swing thread
        See Also:
        if you need to return a value from the Swing thread.