Class CachingSwingWorker<T>

  • All Implemented Interfaces:
    CachingLoader<T>

    public abstract class CachingSwingWorker<T>
    extends java.lang.Object
    implements CachingLoader<T>
    Class for managing the creation of some slow loading object that may be used by multiple threads, including the Swing thread. Further, repeated calls to this object will used the cached value.

    The basic uses cases are:

    1. Call get(TaskMonitor) from the Swing thread - this will block the Swing thread, showing a modal dialog, as needed.
    2. Call get(TaskMonitor) from a non-Swing thread - this will block the calling thread, with no effect on the UI.
    3. Call startLoading() - this will trigger this worker to load in the background without blocking the calling thread.
    4. Call getCachedValue() - this is a way to see if the value has been loaded without blocking the current thread.
    5. Override done() - this method will be called when the initial loading is finished.
    • Constructor Summary

      Constructors 
      Constructor Description
      CachingSwingWorker​(java.lang.String name, boolean hasProgress)
      Create a new CachingSwingWorker
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Clears the cached value for the object causing it to be recreated on the next call to get()
      void done()
      A method for clients to use as a callback for completion.
      T get​(TaskMonitor monitor)
      Returns the object that this class is managing/caching.
      T getCachedValue()
      Returns the value only if it is cached, otherwise return null.
      protected abstract T runInBackground​(TaskMonitor monitor)
      Subclasses must implement this method to create the object being managed/cached.
      void setTaskDialogDelay​(int delay)
      Sets the initial delay before showing a progress dialog.
      void startLoading()
      Allows clients to start this worker loading without blocking.
      • Methods inherited from class java.lang.Object

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

      • CachingSwingWorker

        public CachingSwingWorker​(java.lang.String name,
                                  boolean hasProgress)
        Create a new CachingSwingWorker
        Parameters:
        name - the name of worker. (Displayed in the progress dialog)
        hasProgress - true if the dialog should show progress or be indeterminate.
    • Method Detail

      • setTaskDialogDelay

        public void setTaskDialogDelay​(int delay)
        Sets the initial delay before showing a progress dialog. The default is 100ms.
        Parameters:
        delay - the delay to wait before displaying a progress dialog.
      • runInBackground

        protected abstract T runInBackground​(TaskMonitor monitor)
        Subclasses must implement this method to create the object being managed/cached.
        Parameters:
        monitor - A task monitor that can be used to provide progress information to a progress dialog is it is being shown. Implementers should also check the monitor periodically to check for cancelled. If cancelled, this method should not throw a cancelled exception but instead either return null or a partial result. (For example, if the object is a list being generated by a search, then it might make sense to return a list of the items found so far.)
        Returns:
        the newly created object.
      • get

        public T get​(TaskMonitor monitor)
        Returns the object that this class is managing/caching. It will return the object if it is already created or it will block until the object can be created. If called from the Swing thread, it will also launch a modal progress dialog while waiting for the object to be created.
        Specified by:
        get in interface CachingLoader<T>
        Parameters:
        monitor - the monitor (may be null)
        Returns:
        the object that this class is managing/caching
        See Also:
        #get()
      • startLoading

        public void startLoading()
        Allows clients to start this worker loading without blocking.
      • getCachedValue

        public T getCachedValue()
        Returns the value only if it is cached, otherwise return null.
        Returns:
        the value only if it is cached, otherwise return null.
      • clear

        public void clear()
        Clears the cached value for the object causing it to be recreated on the next call to get()
        Specified by:
        clear in interface CachingLoader<T>
      • done

        public void done()
        A method for clients to use as a callback for completion. This method will be called in the Swing thread, after the value has been set.