X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Ffunction%2Fsrtm%2FDownloadSrtmFunction.java;h=311ff9290408adebd9c65ab0c2fe027337114477;hp=0350ce38ddc7af88508523fa9b2f9f7ef14d41be;hb=189a667821db055202dcb9f539ee26bd2af7ade4;hpb=2302358503c38817e19f6e529f6c9e530aac0e86 diff --git a/src/tim/prune/function/srtm/DownloadSrtmFunction.java b/src/tim/prune/function/srtm/DownloadSrtmFunction.java index 0350ce3..311ff92 100644 --- a/src/tim/prune/function/srtm/DownloadSrtmFunction.java +++ b/src/tim/prune/function/srtm/DownloadSrtmFunction.java @@ -1,11 +1,5 @@ package tim.prune.function.srtm; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.net.URLConnection; import java.util.ArrayList; import javax.swing.JOptionPane; @@ -14,7 +8,6 @@ import tim.prune.App; import tim.prune.GenericFunction; import tim.prune.GpsPrune; import tim.prune.I18nManager; -import tim.prune.config.Config; import tim.prune.data.DoubleRange; import tim.prune.gui.ProgressDialog; @@ -28,19 +21,20 @@ public class DownloadSrtmFunction extends GenericFunction implements Runnable private ProgressDialog _progress = null; /** Flag to check whether this function is currently running or not */ private boolean _running = false; - + private SrtmSource _srtmSource = null; /** * Constructor * @param inApp App object */ - public DownloadSrtmFunction(App inApp) { + public DownloadSrtmFunction(App inApp, SrtmSource inSrtmSource) { super(inApp); + _srtmSource = inSrtmSource; } /** @return name key */ public String getNameKey() { - return "function.downloadsrtm"; + return "function.downloadsrtm."+_srtmSource.getName(); } /** @@ -48,6 +42,17 @@ public class DownloadSrtmFunction extends GenericFunction implements Runnable */ public void begin() { + if (! SrtmDiskCache.ensureCacheIsUsable()) + { + _app.showErrorMessage(getNameKey(), "error.downloadsrtm.nocache"); + return; + } + if (! _srtmSource.isReadyToUse()) + { + _app.showErrorMessage(getNameKey(), getNameKey() + ".needsetup"); + return; + } + _running = true; if (_progress == null) { _progress = new ProgressDialog(_parentFrame, getNameKey()); @@ -61,6 +66,14 @@ public class DownloadSrtmFunction extends GenericFunction implements Runnable * Run method using separate thread */ public void run() + { + ArrayList tileList = buildCoveringTiles(); + downloadTiles(tileList); + // Finished + _running = false; + } + + private ArrayList buildCoveringTiles() { // Compile list of tiles to get ArrayList tileList = new ArrayList(); @@ -89,18 +102,16 @@ public class DownloadSrtmFunction extends GenericFunction implements Runnable } } - downloadTiles(tileList); - // Finished - _running = false; + return tileList; } - /** * Download the tiles of SRTM data * @param inTileList list of tiles to get */ private void downloadTiles(ArrayList inTileList) { + String errorMessage = ""; // Update progress bar if (_progress != null) { @@ -108,62 +119,29 @@ public class DownloadSrtmFunction extends GenericFunction implements Runnable _progress.setValue(0); } - String errorMessage = null; - - // Check the cache is ok - final String diskCachePath = Config.getConfigString(Config.KEY_DISK_CACHE); - if (diskCachePath != null) - { - File srtmDir = new File(diskCachePath, "srtm"); - if (!srtmDir.exists() && !srtmDir.mkdir()) { - // can't create the srtm directory - errorMessage = I18nManager.getText("error.downloadsrtm.nocache"); - } - } - else { - // no cache set up - errorMessage = I18nManager.getText("error.downloadsrtm.nocache"); - } - - // Get urls for each tile - URL[] urls = TileFinder.getUrls(inTileList); int numDownloaded = 0; for (int t=0; t 0) { - _app.showErrorMessage(getNameKey(), "confirm.downloadsrtm.none"); + JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("confirm.downloadsrtm.none")); } } - - /** - * See whether the SRTM file is already available locally - * @param inUrl URL for online resource - * @return file object to write to, or null if already there - */ - private static File getFileToWrite(URL inUrl) - { - String diskCachePath = Config.getConfigString(Config.KEY_DISK_CACHE); - if (diskCachePath != null) - { - File srtmDir = new File(diskCachePath, "srtm"); - if (srtmDir.exists() && srtmDir.isDirectory() && srtmDir.canRead()) - { - File srtmFile = new File(srtmDir, new File(inUrl.getFile()).getName()); - if (!srtmFile.exists() || !srtmFile.canRead() || srtmFile.length() <= 400) { - return srtmFile; - } - } - } - return null; - } - - /** - * @return true if a thread is currently running - */ - public boolean isRunning() - { - return _running; - } }