Package uk.ac.starlink.util.gui
Class Downloader<T>
- java.lang.Object
-
- uk.ac.starlink.util.gui.Downloader<T>
-
public abstract class Downloader<T> extends java.lang.ObjectManages downloading of data that only needs to be got once.- Since:
- 13 Jun 2014
- Author:
- Mark Taylor
-
-
Constructor Summary
Constructors Constructor Description Downloader(java.lang.Class<T> clazz, java.lang.String dataDescription)Constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidaddActionListener(java.awt.event.ActionListener listener)Adds a listener that will be notified if the data acquisition status changes.abstract TattemptReadData()Performs the actual download.voidclearData()Resets the state of this downloader, as if no download attempt had been made.javax.swing.JComponentcreateMonitorComponent()Returns a little component that monitors status of this downloader.TgetData()Immediately returns the downloaded data, or null if it has not been downloaded, or if a download has failed.booleanisComplete()Indicates whether the data has been downloaded.voidremoveActionListener(java.awt.event.ActionListener listener)Removes a previously added listener.voidsetData(T value)Sets the state of this downloader as if it had just downloaded the given data item.TwaitForData()Downloads the data if necessary, and returns its content.
-
-
-
Constructor Detail
-
Downloader
public Downloader(java.lang.Class<T> clazz, java.lang.String dataDescription)
Constructor.- Parameters:
clazz- type of data downloadeddataDescription- short description of downloaded data, may be used in logging messages
-
-
Method Detail
-
attemptReadData
public abstract T attemptReadData() throws java.io.IOException
Performs the actual download. Implementations are encouraged to log query and details of success if applicable at the INFO level, but an error will be logged by the Downloader.- Returns:
- downloaded data
- Throws:
java.io.IOException
-
isComplete
public boolean isComplete()
Indicates whether the data has been downloaded. If this method returns true, thengetData()will return the result.- Returns:
- true iff download has completed, successfully or otherwise
-
getData
public T getData()
Immediately returns the downloaded data, or null if it has not been downloaded, or if a download has failed.- Returns:
- data
-
clearData
public void clearData()
Resets the state of this downloader, as if no download attempt had been made.
-
setData
public void setData(T value)
Sets the state of this downloader as if it had just downloaded the given data item. This can be necessary to restore its state, since it affects the monitor component.- Parameters:
value- value that would have been downloaded
-
waitForData
public T waitForData()
Downloads the data if necessary, and returns its content. If a download attempt has already been completed, this will return immediately, otherwise it will block. If the download failed, null will be returned.- Returns:
- data or null on failure
-
createMonitorComponent
public javax.swing.JComponent createMonitorComponent()
Returns a little component that monitors status of this downloader. Currently, it is blank before the download has happened, then turns to green on success or red on failure.
-
addActionListener
public void addActionListener(java.awt.event.ActionListener listener)
Adds a listener that will be notified if the data acquisition status changes.- Parameters:
listener- listener
-
removeActionListener
public void removeActionListener(java.awt.event.ActionListener listener)
Removes a previously added listener.- Parameters:
listener- listener
-
-