Changeset 14632 in josm for trunk/src/org/openstreetmap/josm/gui/preferences/imagery/CacheSettingsPanel.java
- Timestamp:
- 2019-01-03T20:46:14+01:00 (5 years ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/preferences/imagery/CacheSettingsPanel.java
r14631 r14632 6 6 import java.awt.GridBagLayout; 7 7 import java.awt.event.ActionEvent; 8 import java.io.File; 8 9 import java.util.ArrayList; 9 10 import java.util.Comparator; … … 19 20 import javax.swing.JPanel; 20 21 import javax.swing.JScrollPane; 22 import javax.swing.JSpinner; 21 23 import javax.swing.JTable; 24 import javax.swing.SpinnerNumberModel; 22 25 import javax.swing.table.DefaultTableModel; 23 26 import javax.swing.table.TableColumn; … … 29 32 import org.apache.commons.jcs.engine.stats.behavior.IStats; 30 33 import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry; 34 import org.openstreetmap.josm.data.cache.JCSCacheManager; 35 import org.openstreetmap.josm.data.imagery.CachedTileLoaderFactory; 31 36 import org.openstreetmap.josm.gui.MainApplication; 37 import org.openstreetmap.josm.gui.layer.AbstractCachedTileSourceLayer; 32 38 import org.openstreetmap.josm.gui.layer.TMSLayer; 33 39 import org.openstreetmap.josm.gui.layer.WMSLayer; … … 35 41 import org.openstreetmap.josm.gui.util.GuiHelper; 36 42 import org.openstreetmap.josm.gui.widgets.ButtonColumn; 43 import org.openstreetmap.josm.gui.widgets.JosmTextField; 37 44 import org.openstreetmap.josm.tools.GBC; 38 45 import org.openstreetmap.josm.tools.Logging; … … 41 48 42 49 /** 43 * Panel for cache content management.50 * Panel for cache size, location and content management. 44 51 * 45 52 * @author Wiktor Niesiobędzki 46 53 * 47 54 */ 48 public class CacheContentsPanel extends JPanel { 55 public class CacheSettingsPanel extends JPanel { 56 57 private final JosmTextField cacheDir = new JosmTextField(11); 58 private final JSpinner maxElementsOnDisk = new JSpinner(new SpinnerNumberModel( 59 AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get().intValue(), 0, Integer.MAX_VALUE, 1)); 49 60 50 61 /** 51 62 * Creates cache content panel 52 63 */ 53 public Cache ContentsPanel() {64 public CacheSettingsPanel() { 54 65 super(new GridBagLayout()); 66 67 add(new JLabel(tr("Tile cache directory: ")), GBC.std()); 68 add(GBC.glue(5, 0), GBC.std()); 69 add(cacheDir, GBC.eol().fill(GBC.HORIZONTAL)); 70 71 add(new JLabel(tr("Maximum size of disk cache (per imagery) in MB: ")), GBC.std()); 72 add(GBC.glue(5, 0), GBC.std()); 73 add(maxElementsOnDisk, GBC.eop()); 74 55 75 MainApplication.worker.submit(() -> { 56 76 addToPanel(TMSLayer.getCache(), "TMS"); … … 152 172 }; 153 173 } 174 175 /** 176 * Loads the common settings. 177 */ 178 void loadSettings() { 179 this.cacheDir.setText(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get()); 180 this.maxElementsOnDisk.setValue(AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get()); 181 } 182 183 /** 184 * Saves the common settings. 185 * @return true when restart is required 186 */ 187 boolean saveSettings() { 188 boolean restartRequired = removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get(), 189 1024L * 1024L * ((Integer) this.maxElementsOnDisk.getValue())); 190 191 if (!AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get().equals(this.maxElementsOnDisk.getValue())) { 192 AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.put((Integer) this.maxElementsOnDisk.getValue()); 193 restartRequired = true; 194 } 195 196 197 if (!CachedTileLoaderFactory.PROP_TILECACHE_DIR.get().equals(this.cacheDir.getText())) { 198 restartRequired = true; 199 removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get(), 0); // clear old cache directory 200 CachedTileLoaderFactory.PROP_TILECACHE_DIR.put(this.cacheDir.getText()); 201 } 202 203 return restartRequired; 204 } 205 206 private static boolean removeCacheFiles(String path, long maxSize) { 207 File directory = new File(path); 208 File[] cacheFiles = directory.listFiles((dir, name) -> name.endsWith(".data") || name.endsWith(".key")); 209 boolean restartRequired = false; 210 if (cacheFiles != null) { 211 for (File cacheFile: cacheFiles) { 212 if (cacheFile.length() > maxSize) { 213 if (!restartRequired) { 214 JCSCacheManager.shutdown(); // shutdown Cache - so files can by safely deleted 215 restartRequired = true; 216 } 217 Utils.deleteFile(cacheFile); 218 File otherFile = null; 219 if (cacheFile.getName().endsWith(".data")) { 220 otherFile = new File(cacheFile.getPath().replaceAll("\\.data$", ".key")); 221 } else if (cacheFile.getName().endsWith(".key")) { 222 otherFile = new File(cacheFile.getPath().replaceAll("\\.key$", ".data")); 223 } 224 if (otherFile != null) { 225 Utils.deleteFileIfExists(otherFile); 226 } 227 } 228 } 229 } 230 return restartRequired; 231 } 154 232 }
Note:
See TracChangeset
for help on using the changeset viewer.