X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Ffunction%2Fcache%2FTileCacheModel.java;fp=src%2Ftim%2Fprune%2Ffunction%2Fcache%2FTileCacheModel.java;h=2f454149bb7b9c01466ac940c5c98b2ad1525fb0;hp=0000000000000000000000000000000000000000;hb=ce6f2161b8596f7018d6a76bff79bc9e571f35fd;hpb=2d8cb72e84d5cc1089ce77baf1e34ea3ea2f8465 diff --git a/src/tim/prune/function/cache/TileCacheModel.java b/src/tim/prune/function/cache/TileCacheModel.java new file mode 100644 index 0000000..2f45414 --- /dev/null +++ b/src/tim/prune/function/cache/TileCacheModel.java @@ -0,0 +1,216 @@ +package tim.prune.function.cache; + +import java.io.File; +import java.util.ArrayList; + +import tim.prune.gui.map.MapSource; +import tim.prune.gui.map.MapSourceLibrary; + +/** + * Class to obtain and hold information about the current + * tile cache including its subdirectories + */ +public class TileCacheModel +{ + /** Cache directory */ + private File _cacheDir = null; + /** Array of tilesets */ + private ArrayList _tileSets = null; + /** Summary information */ + private RowInfo _summaryRow = null; + /** Cancelled flag */ + private boolean _cancelled = false; + + + /** + * Constructor + * @param inDir start directory + */ + public TileCacheModel(File inDir) + { + if (inDir != null && inDir.exists() && inDir.isDirectory() && inDir.canRead()) { + _cacheDir = inDir; + } + _cancelled = false; + } + + /** + * Build the tilesets by searching recursively + */ + public void buildTileSets() + { + if (_cacheDir == null) return; + + _tileSets = new ArrayList(); + // go through subdirectories, if any + File[] subdirs = _cacheDir.listFiles(); + for (File subdir : subdirs) + { + if (subdir != null && subdir.isDirectory() && subdir.exists() && subdir.canRead() + && !_cancelled) + { + getTileSets(subdir, null, _tileSets); + } + } + // Loop over found tile sets and create summary rowinfo + _summaryRow = new RowInfo(); + for (TileSet ts : _tileSets) + { + _summaryRow.addRow(ts.getRowInfo()); + } + } + + /** + * Get all the tilesets from the given directory + * @param inDir directory to search + * @return array of TileSet objects + */ + private static void getTileSets(File inDir, String inParentPath, ArrayList inTsList) + { + final String wholePath = (inParentPath == null ? "" : inParentPath) + + inDir.getName() + File.separator; + // See if any configured backgrounds use this directory + // or if the directories match OSM structure + String usedByDesc = matchConfig(wholePath); + boolean tsFound = false; + if (usedByDesc != null || looksLikeCacheDir(inDir)) + { + TileSet ts = new TileSet(inDir, wholePath, usedByDesc); + if (usedByDesc != null || ts.getRowInfo().getNumTiles() > 0) + { + tsFound = true; + inTsList.add(ts); + } + } + // If a tileset wasn't found, look through subdirectories + if (!tsFound) + { + // Go through subdirectories and look at each of them too + File[] subdirs = inDir.listFiles(); + if (subdirs != null) { + for (File subdir : subdirs) + { + if (subdir != null && subdir.exists() && subdir.isDirectory() + && subdir.canRead()) + { + getTileSets(subdir, wholePath, inTsList); + } + } + } + } + } + + /** + * Match the given directory name to find the configs which use it + * @param inName name of directory to match + * @return null if not used, otherwise comma-separated list of background names + */ + private static String matchConfig(String inName) + { + if (inName == null || inName.equals("")) + return null; + String usedBy = null; + for (int i=0; i= 0 && inIndex < getNumTileSets()) { + return _tileSets.get(inIndex); + } + return null; + } + + /** + * Cancel the search + */ + public void cancel() { + _cancelled = true; + } + + /** + * @return true if search was cancelled + */ + public boolean isAborted() { + return _cancelled; + } +}