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; } }