source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/imagery/CommonSettingsPanel.java@ 14295

Last change on this file since 14295 was 14295, checked in by wiktorn, 6 years ago

Always check if tile cache files are oversized

If tile cache file is oversized, remove it. When removing one file (.data or
.key) remove also the other file (resp. .key or .data).

Notify user about the need of the restart only, when there was something
deleted

  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences.imagery;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.io.File;
8import java.io.FilenameFilter;
9
10import javax.swing.JLabel;
11import javax.swing.JPanel;
12import javax.swing.JSlider;
13import javax.swing.JSpinner;
14import javax.swing.SpinnerNumberModel;
15
16import org.openstreetmap.josm.data.cache.JCSCacheManager;
17import org.openstreetmap.josm.data.imagery.CachedTileLoaderFactory;
18import org.openstreetmap.josm.gui.layer.AbstractCachedTileSourceLayer;
19import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
20import org.openstreetmap.josm.gui.layer.ImageryLayer;
21import org.openstreetmap.josm.gui.widgets.JosmComboBox;
22import org.openstreetmap.josm.gui.widgets.JosmTextField;
23import org.openstreetmap.josm.tools.GBC;
24import org.openstreetmap.josm.tools.Utils;
25
26/**
27 * {@code JPanel} giving access to common imagery settings.
28 * @since 5465
29 */
30public class CommonSettingsPanel extends JPanel {
31
32 // Common Settings
33 private final JosmComboBox<String> sharpen;
34 private final JosmTextField tilecacheDir = new JosmTextField(11);
35 private final JSpinner maxElementsOnDisk;
36 private final JSlider tilesZoom = new JSlider(-2, 2, 0);
37
38
39 /**
40 * Constructs a new {@code CommonSettingsPanel}.
41 */
42 public CommonSettingsPanel() {
43 super(new GridBagLayout());
44
45 this.maxElementsOnDisk = new JSpinner(new SpinnerNumberModel(
46 AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get().intValue(), 0, Integer.MAX_VALUE, 1));
47
48 this.sharpen = new JosmComboBox<>(new String[] {
49 tr("None"),
50 tr("Soft"),
51 tr("Strong")});
52 add(new JLabel(tr("Sharpen (requires layer re-add): ")));
53 add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
54 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
64 this.tilesZoom.setPaintLabels(true);
65 this.tilesZoom.setMajorTickSpacing(2);
66 this.tilesZoom.setMinorTickSpacing(1);
67 this.tilesZoom.setPaintTicks(true);
68 add(new JLabel(tr("Tiles zoom offset:")));
69 add(GBC.glue(5, 0), GBC.std());
70 add(this.tilesZoom, GBC.eol());
71 }
72
73 /**
74 * Loads the common settings.
75 */
76 public void loadSettings() {
77 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 this.tilesZoom.setValue(AbstractTileSourceLayer.ZOOM_OFFSET.get());
81 }
82
83 /**
84 * Saves the common settings.
85 * @return true when restart is required
86 */
87 public boolean saveSettings() {
88 ImageryLayer.PROP_SHARPEN_LEVEL.put(sharpen.getSelectedIndex());
89
90 boolean restartRequired = false;
91 restartRequired |= removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get(), 1024L * 1024L * ((Integer) this.maxElementsOnDisk.getValue()));
92
93 if (!AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get().equals(this.maxElementsOnDisk.getValue())) {
94 AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.put((Integer) this.maxElementsOnDisk.getValue());
95 restartRequired = true;
96 }
97
98
99 if (!CachedTileLoaderFactory.PROP_TILECACHE_DIR.get().equals(this.tilecacheDir.getText())) {
100 restartRequired = true;
101 restartRequired |= removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get(), 0); // clear old cache directory
102 CachedTileLoaderFactory.PROP_TILECACHE_DIR.put(this.tilecacheDir.getText());
103 }
104
105 if (!AbstractTileSourceLayer.ZOOM_OFFSET.get().equals(this.tilesZoom.getValue())) {
106 // TODO: make warning about too small MEMORY_CACHE_SIZE?
107 AbstractTileSourceLayer.ZOOM_OFFSET.put(this.tilesZoom.getValue());
108 restartRequired = true;
109 }
110 return restartRequired;
111 }
112
113 private static boolean removeCacheFiles(String path, long maxSize) {
114
115 File directory = new File(path);
116 File[] cacheFiles = directory.listFiles((FilenameFilter) (dir, name) -> name.endsWith(".data") || name.endsWith(".key"));
117 boolean restartRequired = false;
118 if (cacheFiles != null) {
119 for (File cacheFile: cacheFiles) {
120 if (cacheFile.length() > maxSize) {
121 if (!restartRequired) {
122 JCSCacheManager.shutdown(); // shutdown Cache - so files can by safely deleted
123 restartRequired = true;
124 }
125 Utils.deleteFile(cacheFile);
126 File otherFile = null;
127 if (cacheFile.getName().endsWith(".data")) {
128 otherFile = new File(cacheFile.getPath().replaceAll("\\.data$", ".key"));
129 } else if (cacheFile.getName().endsWith(".key")) {
130 otherFile = new File(cacheFile.getPath().replaceAll("\\.key$", ".data"));
131 }
132 if (otherFile != null) {
133 Utils.deleteFileIfExists(otherFile);
134 }
135 }
136 }
137 }
138 return restartRequired;
139 }
140}
Note: See TracBrowser for help on using the repository browser.