Changeset 14632 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2019-01-03T20:46:14+01:00 (6 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui/preferences/imagery
- Files:
-
- 2 edited
- 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 } -
trunk/src/org/openstreetmap/josm/gui/preferences/imagery/CommonSettingsPanel.java
r14327 r14632 5 5 6 6 import java.awt.GridBagLayout; 7 import java.io.File;8 import java.io.FilenameFilter;9 7 10 8 import javax.swing.JLabel; 11 9 import javax.swing.JPanel; 12 10 import javax.swing.JSlider; 13 import javax.swing.JSpinner;14 import javax.swing.SpinnerNumberModel;15 11 16 import org.openstreetmap.josm.data.cache.JCSCacheManager;17 import org.openstreetmap.josm.data.imagery.CachedTileLoaderFactory;18 import org.openstreetmap.josm.gui.layer.AbstractCachedTileSourceLayer;19 12 import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer; 20 13 import org.openstreetmap.josm.gui.layer.ImageryLayer; 21 14 import org.openstreetmap.josm.gui.widgets.JosmComboBox; 22 import org.openstreetmap.josm.gui.widgets.JosmTextField;23 15 import org.openstreetmap.josm.tools.GBC; 24 16 import org.openstreetmap.josm.tools.Utils; … … 32 24 // Common Settings 33 25 private final JosmComboBox<String> sharpen; 34 private final JosmTextField tilecacheDir = new JosmTextField(11);35 private final JSpinner maxElementsOnDisk;36 26 private final JSlider tilesZoom = new JSlider(-2, 2, 0); 37 27 … … 43 33 super(new GridBagLayout()); 44 34 45 this.maxElementsOnDisk = new JSpinner(new SpinnerNumberModel(46 AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get().intValue(), 0, Integer.MAX_VALUE, 1));47 48 35 this.sharpen = new JosmComboBox<>(new String[] { 49 36 tr("None"), … … 53 40 add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL)); 54 41 add(this.sharpen, GBC.eol().fill(GBC.HORIZONTAL)); 55 56 add(new JLabel(tr("Tile cache directory: ")), GBC.std());57 add(GBC.glue(5, 0), GBC.std());58 add(tilecacheDir, GBC.eol().fill(GBC.HORIZONTAL));59 60 add(new JLabel(tr("Maximum size of disk cache (per imagery) in MB: ")), GBC.std());61 add(GBC.glue(5, 0), GBC.std());62 add(this.maxElementsOnDisk, GBC.eol());63 42 64 43 this.tilesZoom.setPaintLabels(true); … … 76 55 public void loadSettings() { 77 56 this.sharpen.setSelectedIndex(Utils.clamp(ImageryLayer.PROP_SHARPEN_LEVEL.get(), 0, 2)); 78 this.tilecacheDir.setText(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get());79 this.maxElementsOnDisk.setValue(AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get());80 57 this.tilesZoom.setValue(AbstractTileSourceLayer.ZOOM_OFFSET.get()); 81 58 } … … 89 66 90 67 boolean restartRequired = false; 91 restartRequired |= removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get(),92 1024L * 1024L * ((Integer) this.maxElementsOnDisk.getValue()));93 94 if (!AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get().equals(this.maxElementsOnDisk.getValue())) {95 AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.put((Integer) this.maxElementsOnDisk.getValue());96 restartRequired = true;97 }98 99 100 if (!CachedTileLoaderFactory.PROP_TILECACHE_DIR.get().equals(this.tilecacheDir.getText())) {101 restartRequired = true;102 restartRequired |= removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get(), 0); // clear old cache directory103 CachedTileLoaderFactory.PROP_TILECACHE_DIR.put(this.tilecacheDir.getText());104 }105 106 68 if (!AbstractTileSourceLayer.ZOOM_OFFSET.get().equals(this.tilesZoom.getValue())) { 107 69 // TODO: make warning about too small MEMORY_CACHE_SIZE? … … 111 73 return restartRequired; 112 74 } 113 114 private static boolean removeCacheFiles(String path, long maxSize) {115 116 File directory = new File(path);117 File[] cacheFiles = directory.listFiles((FilenameFilter) (dir, name) -> name.endsWith(".data") || name.endsWith(".key"));118 boolean restartRequired = false;119 if (cacheFiles != null) {120 for (File cacheFile: cacheFiles) {121 if (cacheFile.length() > maxSize) {122 if (!restartRequired) {123 JCSCacheManager.shutdown(); // shutdown Cache - so files can by safely deleted124 restartRequired = true;125 }126 Utils.deleteFile(cacheFile);127 File otherFile = null;128 if (cacheFile.getName().endsWith(".data")) {129 otherFile = new File(cacheFile.getPath().replaceAll("\\.data$", ".key"));130 } else if (cacheFile.getName().endsWith(".key")) {131 otherFile = new File(cacheFile.getPath().replaceAll("\\.key$", ".data"));132 }133 if (otherFile != null) {134 Utils.deleteFileIfExists(otherFile);135 }136 }137 }138 }139 return restartRequired;140 }141 75 } -
trunk/src/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreference.java
r14629 r14632 90 90 private final WMSSettingsPanel wmsSettings = new WMSSettingsPanel(); 91 91 private final TMSSettingsPanel tmsSettings = new TMSSettingsPanel(); 92 private final CacheSettingsPanel cacheSettingsPanel = new CacheSettingsPanel(); 92 93 93 94 /** … … 143 144 pane.addTab(tr("Settings"), buildSettingsPanel()); 144 145 pane.addTab(tr("Offset bookmarks"), new OffsetBookmarksPanel(gui)); 145 pane.addTab(tr("Cache contents"), new CacheContentsPanel());146 pane.addTab(tr("Cache"), cacheSettingsPanel); 146 147 loadSettings(); 147 148 p.add(pane, GBC.std().fill(GBC.BOTH)); … … 160 161 wmsSettings.loadSettings(); 161 162 tmsSettings.loadSettings(); 163 cacheSettingsPanel.loadSettings(); 162 164 } 163 165 … … 177 179 boolean wmsRestartRequired = wmsSettings.saveSettings(); 178 180 boolean tmsRestartRequired = tmsSettings.saveSettings(); 179 180 return commonRestartRequired || wmsRestartRequired || tmsRestartRequired; 181 boolean cacheRestartRequired = cacheSettingsPanel.saveSettings(); 182 183 return commonRestartRequired || wmsRestartRequired || tmsRestartRequired || cacheRestartRequired; 181 184 } 182 185
Note:
See TracChangeset
for help on using the changeset viewer.