1 package tim.prune.function.cache;
4 import java.util.ArrayList;
6 import tim.prune.gui.map.MapSource;
7 import tim.prune.gui.map.MapSourceLibrary;
10 * Class to obtain and hold information about the current
11 * tile cache including its subdirectories
13 public class TileCacheModel
15 /** Cache directory */
16 private File _cacheDir = null;
17 /** Array of tilesets */
18 private ArrayList<TileSet> _tileSets = null;
19 /** Summary information */
20 private RowInfo _summaryRow = null;
22 private boolean _cancelled = false;
27 * @param inDir start directory
29 public TileCacheModel(File inDir)
31 if (inDir != null && inDir.exists() && inDir.isDirectory() && inDir.canRead()) {
38 * Build the tilesets by searching recursively
40 public void buildTileSets()
42 if (_cacheDir == null) return;
44 _tileSets = new ArrayList<TileSet>();
45 // go through subdirectories, if any
46 File[] subdirs = _cacheDir.listFiles();
47 for (File subdir : subdirs)
49 if (subdir != null && subdir.isDirectory() && subdir.exists() && subdir.canRead()
52 getTileSets(subdir, null, _tileSets);
55 // Loop over found tile sets and create summary rowinfo
56 _summaryRow = new RowInfo();
57 for (TileSet ts : _tileSets)
59 _summaryRow.addRow(ts.getRowInfo());
64 * Get all the tilesets from the given directory
65 * @param inDir directory to search
66 * @return array of TileSet objects
68 private static void getTileSets(File inDir, String inParentPath, ArrayList<TileSet> inTsList)
70 final String wholePath = (inParentPath == null ? "" : inParentPath)
71 + inDir.getName() + File.separator;
72 // See if any configured backgrounds use this directory
73 // or if the directories match OSM structure
74 String usedByDesc = matchConfig(wholePath);
75 boolean tsFound = false;
76 if (usedByDesc != null || looksLikeCacheDir(inDir))
78 TileSet ts = new TileSet(inDir, wholePath, usedByDesc);
79 if (usedByDesc != null || ts.getRowInfo().getNumTiles() > 0)
85 // If a tileset wasn't found, look through subdirectories
88 // Go through subdirectories and look at each of them too
89 File[] subdirs = inDir.listFiles();
90 if (subdirs != null) {
91 for (File subdir : subdirs)
93 if (subdir != null && subdir.exists() && subdir.isDirectory()
96 getTileSets(subdir, wholePath, inTsList);
104 * Match the given directory name to find the configs which use it
105 * @param inName name of directory to match
106 * @return null if not used, otherwise comma-separated list of background names
108 private static String matchConfig(String inName)
110 if (inName == null || inName.equals(""))
112 String usedBy = null;
113 for (int i=0; i<MapSourceLibrary.getNumSources(); i++)
115 MapSource ms = MapSourceLibrary.getSource(i);
116 for (int l=0; l<2; l++)
118 String msdir = ms.getSiteName(l);
119 if (msdir != null && msdir.equals(inName))
122 usedBy = ms.getName();
124 usedBy = usedBy + ", " + ms.getName();
132 * @param inDir directory to test
133 * @return true if the subdirectories meet the normal osm layout
135 private static boolean looksLikeCacheDir(File inDir)
137 // look for at least one numeric directory, nothing else
138 boolean numFound = false;
139 if (inDir != null && inDir.exists() && inDir.isDirectory() && inDir.canRead())
141 for (File subdir : inDir.listFiles())
143 // Only consider readable things which exist
144 if (subdir != null && subdir.exists() && subdir.canRead())
146 // subdirectories should have numeric names (for the zoom levels)
147 if (subdir.isDirectory() && TileSet.isNumeric(subdir.getName())
148 && subdir.getName().length() < 3)
152 else return false; // either a file or non-numeric directory
160 * @return cache directory
162 public File getCacheDir() {
167 * @return number of tile sets
169 public int getNumTileSets()
171 if (_tileSets == null) return 0;
172 return _tileSets.size();
176 * @return the total number of tile images found
178 public int getTotalTiles()
180 return _summaryRow.getNumTiles();
184 * @return the total number of bytes taken up with tile images
186 public long getTotalBytes()
188 return _summaryRow.getTotalSize();
192 * @param inIndex index of tileset
193 * @return requested tileset
195 public TileSet getTileSet(int inIndex)
197 if (inIndex >= 0 && inIndex < getNumTileSets()) {
198 return _tileSets.get(inIndex);
206 public void cancel() {
211 * @return true if search was cancelled
213 public boolean isAborted() {