Class TaskDialog

All Implemented Interfaces:
ActionContextProvider, StatusListener, TaskListener, TaskMonitor

public class TaskDialog extends DialogComponentProvider implements TaskMonitor
Dialog that is displayed to show activity for a Task that is running outside of the Swing Thread. This dialog uses a delay before showing in order to give the background task thread a chance to finish. This prevents a flashing dialog for tasks that finish before the delay time period.

Implementation note: if this class is constructed with a hasProgress value of false, then an activity component will be shown, not a progress monitor. Any calls to update progress will not affect the display. However, the display can be converted to use progress by first calling setIndeterminate(boolean) with a false value and then calling initialize(long). Once this has happened, this dialog will no longer use the activity display--the progress bar is in effect for the duration of this dialog's usage.

This dialog can be toggled between indeterminate mode and progress mode via calls to setIndeterminate(boolean).

API Usage Note: If this dialog is used outside of the task API, then the client must be sure to call taskProcessed() from the background thread performing the work. Otherwise, this dialog will always wait for the delay amount of time for the background thread to finish. This happens since the default completed notification mechanism is performed on the Swing thread. If a client has triggered blocking on the Swing thread, then the notification on the Swing thread must wait, causing the full delay to take place. Calling taskProcessed() from the background thread allows the dialog to get notified before the delay period has expired. The blocking issue only exists with a non-0 delay value.

  • Field Details

  • Constructor Details

    • TaskDialog

      public TaskDialog(Task task)
      Constructor
      Parameters:
      task - the Task that this dialog will be associated with
    • TaskDialog

      public TaskDialog(String title, boolean canCancel, boolean isModal, boolean hasProgress)
      Constructor
      Parameters:
      title - title for the dialog
      canCancel - true if the task can be canceled
      isModal - true if the dialog should be modal
      hasProgress - true if the dialog should show a progress bar
    • TaskDialog

      public TaskDialog(String title, boolean canCancel, boolean isModal, boolean hasProgress, CountDownLatch finished)
      Constructor
      Parameters:
      title - title for the dialog
      canCancel - true if the task can be canceled
      isModal - true if the dialog should be modal
      hasProgress - true if the dialog should show a progress bar
      finished - the finished latch used by the background thread to notify of completion
  • Method Details

    • cancelCallback

      protected void cancelCallback()
      Description copied from class: DialogComponentProvider
      The callback method for when the "Cancel" button is pressed. The default behavior is to call setVisible(false) and dispose() on the dialog.
      Overrides:
      cancelCallback in class DialogComponentProvider
    • setCancelEnabled

      public void setCancelEnabled(boolean enable)
      Description copied from class: DialogComponentProvider
      Sets the enablement state of the "CANCEL" button.
      Specified by:
      setCancelEnabled in interface TaskMonitor
      Overrides:
      setCancelEnabled in class DialogComponentProvider
      Parameters:
      enable - true to enable the button, false to disable the button.
    • isCancelEnabled

      public boolean isCancelEnabled()
      Description copied from class: DialogComponentProvider
      Returns true if the cancel button is enabled
      Specified by:
      isCancelEnabled in interface TaskMonitor
      Overrides:
      isCancelEnabled in class DialogComponentProvider
      Returns:
      true if the cancel button is enabled
    • taskProcessed

      public void taskProcessed()
      Called after the task has been executed or when the task is cancelled
    • isCompleted

      public boolean isCompleted()
      Returns true if this dialog's task has completed normally or been cancelled
      Returns:
      true if this dialog's task has completed normally or been cancelled
    • show

      public void show(int delay)
      Shows the dialog window centered on the parent window. Dialog display is delayed if delay greater than zero.
      Parameters:
      delay - number of milliseconds to delay displaying of the task dialog. If the delay is greater than MAX_DELAY, then the delay will be MAX_DELAY;
      Throws:
      IllegalArgumentException - if delay is negative
    • wasShown

      public boolean wasShown()
      Returns true if this dialog was ever made visible
      Returns:
      true if shown
    • doShow

      protected void doShow()
    • dispose

      public void dispose()
      Description copied from class: DialogComponentProvider
      Disposes this dialog. Only call this when the dialog is no longer used. Calling this method will close the dialog if it is open.
      Overrides:
      dispose in class DialogComponentProvider
    • setMessage

      public void setMessage(String str)
      Description copied from interface: TaskMonitor
      Sets the message displayed on the task monitor
      Specified by:
      setMessage in interface TaskMonitor
      Parameters:
      str - the message to display
    • getMessage

      public String getMessage()
      Description copied from interface: TaskMonitor
      Gets the last set message of this monitor
      Specified by:
      getMessage in interface TaskMonitor
      Returns:
      the message
    • setShowProgressValue

      public void setShowProgressValue(boolean showProgressValue)
      Description copied from interface: TaskMonitor
      True (the default) signals to paint the progress information inside of the progress bar
      Specified by:
      setShowProgressValue in interface TaskMonitor
      Parameters:
      showProgressValue - true to paint the progress value; false to not
    • setProgress

      public void setProgress(long progress)
      Description copied from interface: TaskMonitor
      Sets the current progress value
      Specified by:
      setProgress in interface TaskMonitor
      Parameters:
      progress - progress value
    • initialize

      public void initialize(long max)
      Description copied from interface: TaskMonitor
      Initialized this TaskMonitor to the given max values. The current value of this monitor will be set to zero.
      Specified by:
      initialize in interface TaskMonitor
      Parameters:
      max - maximum value for progress
    • setMaximum

      public void setMaximum(long max)
      Description copied from interface: TaskMonitor
      Set the progress maximum value

      Note: setting this value will reset the progress to be the max if the progress is currently greater than the new new max value.

      Specified by:
      setMaximum in interface TaskMonitor
      Parameters:
      max - maximum value for progress
    • getMaximum

      public long getMaximum()
      Description copied from interface: TaskMonitor
      Returns the current maximum value for progress
      Specified by:
      getMaximum in interface TaskMonitor
      Returns:
      the maximum progress value
    • setIndeterminate

      public void setIndeterminate(boolean indeterminate)
      Description copied from interface: TaskMonitor
      An indeterminate task monitor may choose to show an animation instead of updating progress
      Specified by:
      setIndeterminate in interface TaskMonitor
      Parameters:
      indeterminate - true if indeterminate
    • isIndeterminate

      public boolean isIndeterminate()
      Description copied from interface: TaskMonitor
      Returns true if this monitor shows no progress
      Specified by:
      isIndeterminate in interface TaskMonitor
      Returns:
      true if this monitor shows no progress
    • isCancelled

      public boolean isCancelled()
      Description copied from interface: TaskMonitor
      Returns true if the user has cancelled the operation
      Specified by:
      isCancelled in interface TaskMonitor
      Returns:
      true if the user has cancelled the operation
    • cancel

      public void cancel()
      Description copied from interface: TaskMonitor
      Cancel the task
      Specified by:
      cancel in interface TaskMonitor
    • clearCanceled

      public void clearCanceled()
      Description copied from interface: TaskMonitor
      Clear the cancellation so that this TaskMonitor may be reused
      Specified by:
      clearCanceled in interface TaskMonitor
    • checkCanceled

      public void checkCanceled() throws CancelledException
      Description copied from interface: TaskMonitor
      Check to see if this monitor has been cancelled
      Specified by:
      checkCanceled in interface TaskMonitor
      Throws:
      CancelledException - if monitor has been cancelled
    • incrementProgress

      public void incrementProgress(long incrementAmount)
      Description copied from interface: TaskMonitor
      Changes the progress value by the specified amount.
      Specified by:
      incrementProgress in interface TaskMonitor
      Parameters:
      incrementAmount - The amount by which to increment the progress
    • getProgress

      public long getProgress()
      Description copied from interface: TaskMonitor
      Returns the current progress value or TaskMonitor.NO_PROGRESS_VALUE if there is no value set
      Specified by:
      getProgress in interface TaskMonitor
      Returns:
      the current progress value or TaskMonitor.NO_PROGRESS_VALUE if there is no value set
    • addCancelledListener

      public void addCancelledListener(CancelledListener listener)
      Description copied from interface: TaskMonitor
      Add cancelled listener
      Specified by:
      addCancelledListener in interface TaskMonitor
      Parameters:
      listener - the cancel listener
    • removeCancelledListener

      public void removeCancelledListener(CancelledListener listener)
      Description copied from interface: TaskMonitor
      Remove cancelled listener
      Specified by:
      removeCancelledListener in interface TaskMonitor
      Parameters:
      listener - the cancel listener