Class RunManager


  • public class RunManager
    extends java.lang.Object
    Helper class to execute a Runnable in a separate thread and provides a progress monitor component that is shown as needed. This class can support several different scheduling models described below.

    1) Only allow one runnable at any given time. In this model, a new runnable will cause any running runnable to be cancelled and the new runnable will begin running. Because of this, there will never be any runnables waiting in the queue. Use the runNow(MonitoredRunnable, String) method to get this behavior.

    2) Allow one running runnable and one pending runnable. In this mode, any running runnable will be allowed to complete, but any currently pending runnable will be replaced by the new runnable. Use the runNext(MonitoredRunnable, String) method to get this behavior.

    3) Run all scheduled runnables in the order they are scheduled. Use the runLater(MonitoredRunnable, String, int) for this behavior.

    If the given runnable has Swing work to perform after the main Runnable.run() method completes (e.g., updating Swing components), the runnable should implement the SwingRunnable interface and perform this work in SwingRunnable#swingRun().

    The progress monitor component, retrieved via #getTaskMonitorComponent(), can be placed into a Swing widget. This RunManager will show and hide this progress component as necessary when runnables are being run.

    See Also:
    SwingRunnable
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void cancelAllRunnables()
      A convenience method to cancel the any currently running job and any scheduled jobs.
      void dispose()  
      javax.swing.JComponent getMonitorComponent()  
      boolean isInProgress()  
      void runLater​(MonitoredRunnable runnable, java.lang.String taskName, int showProgressDelay)
      Schedules this runnable to be run after all runnables currently queued.
      void runNext​(MonitoredRunnable runnable, java.lang.String taskName)
      Allows any currently running runnable to finish, clears any queued runnables, and then queues the given runnable to be run after the current runnable finishes.
      void runNext​(MonitoredRunnable runnable, java.lang.String taskName, int showProgressDelay)
      Allows any currently running runnable to finish, clears any queued runnables, and then queues the given runnable to be run after the current runnable finishes.
      void runNow​(MonitoredRunnable runnable, java.lang.String taskName)
      Cancels any currently running runnable, clears any queued runnables, and then runs the given runnable.
      void runNow​(MonitoredRunnable runnable, java.lang.String taskName, int showProgressDelay)
      Cancels any currently running runnable, clears any queued runnables, and then runs the given runnable.
      void showCancelButton​(boolean showCancel)
      Show the cancel button according to the showCancel parameter.
      void showProgressBar​(boolean showProgress)
      Show the progress bar according to the showProgress parameter.
      void showProgressIcon​(boolean showIcon)
      Show the progress icon according to the showIcon parameter.
      void waitForNotBusy​(int maxWaitMillis)  
      • Methods inherited from class java.lang.Object

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

      • RunManager

        public RunManager()
      • RunManager

        public RunManager​(java.lang.String name,
                          java.awt.Component defaultComponent)
      • RunManager

        public RunManager​(java.lang.String name,
                          java.awt.Component defaultComponent,
                          TaskListener listener)
    • Method Detail

      • getMonitorComponent

        public javax.swing.JComponent getMonitorComponent()
      • dispose

        public void dispose()
      • cancelAllRunnables

        public void cancelAllRunnables()
        A convenience method to cancel the any currently running job and any scheduled jobs. Note: this method does not block or wait for the currently running job to finish.
      • waitForNotBusy

        public void waitForNotBusy​(int maxWaitMillis)
      • runNow

        public void runNow​(MonitoredRunnable runnable,
                           java.lang.String taskName)
        Cancels any currently running runnable, clears any queued runnables, and then runs the given runnable.

        See the class header for more info.

        Parameters:
        runnable - Runnable to execute
        taskName - name of runnable; may be null (this will appear in the progress panel)
      • runNow

        public void runNow​(MonitoredRunnable runnable,
                           java.lang.String taskName,
                           int showProgressDelay)
        Cancels any currently running runnable, clears any queued runnables, and then runs the given runnable.

        See the class header for more info.

        Parameters:
        runnable - Runnable to execute
        taskName - name of runnable; may be null (this will appear in the progress panel)
        showProgressDelay - the amount of time (in milliseconds) before showing the progress panel
      • runNext

        public void runNext​(MonitoredRunnable runnable,
                            java.lang.String taskName)
        Allows any currently running runnable to finish, clears any queued runnables, and then queues the given runnable to be run after the current runnable finishes.

        This call will use the default delay of 500.

        See the class header for more info.

        Parameters:
        runnable - Runnable to execute
        taskName - name of runnable; may be null (this will appear in the progress panel)
      • runNext

        public void runNext​(MonitoredRunnable runnable,
                            java.lang.String taskName,
                            int showProgressDelay)
        Allows any currently running runnable to finish, clears any queued runnables, and then queues the given runnable to be run after the current runnable finishes.

        See the class header for more info.

        Parameters:
        runnable - Runnable to execute
        taskName - name of runnable; may be null (this will appear in the progress panel)
        showProgressDelay - the amount of time (in milliseconds) before showing the progress panel
      • runLater

        public void runLater​(MonitoredRunnable runnable,
                             java.lang.String taskName,
                             int showProgressDelay)
        Schedules this runnable to be run after all runnables currently queued.

        This method differs from the #runNow(Runnable, String, int) methods in that it will not cancel any currently running jobs. This allows you to add new jobs to this run manager, which lets them queue up. See header docs for details.

        Parameters:
        runnable - The runnable to run
        taskName - The name of the task to run
        showProgressDelay - The amount of time to wait before showing a progress monitor.
      • isInProgress

        public boolean isInProgress()
      • showCancelButton

        public void showCancelButton​(boolean showCancel)
        Show the cancel button according to the showCancel parameter.
        Parameters:
        showCancel - true means to show the cancel button
      • showProgressBar

        public void showProgressBar​(boolean showProgress)
        Show the progress bar according to the showProgress parameter.
        Parameters:
        showProgress - true means to show the progress bar
      • showProgressIcon

        public void showProgressIcon​(boolean showIcon)
        Show the progress icon according to the showIcon parameter.
        Parameters:
        showIcon - true means to show the progress icon