1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.imagery;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.FilenameFilter;
|
---|
9 |
|
---|
10 | import javax.swing.JLabel;
|
---|
11 | import javax.swing.JPanel;
|
---|
12 | import javax.swing.JSlider;
|
---|
13 | import javax.swing.JSpinner;
|
---|
14 | import javax.swing.SpinnerNumberModel;
|
---|
15 |
|
---|
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 | import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
|
---|
20 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
21 | import org.openstreetmap.josm.gui.widgets.JosmComboBox;
|
---|
22 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
23 | import org.openstreetmap.josm.tools.GBC;
|
---|
24 | import org.openstreetmap.josm.tools.Utils;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * {@code JPanel} giving access to common imagery settings.
|
---|
28 | * @since 5465
|
---|
29 | */
|
---|
30 | public 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 | if (!AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get().equals(this.maxElementsOnDisk.getValue())) {
|
---|
92 | if (((Integer) this.maxElementsOnDisk.getValue()) < AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get() &&
|
---|
93 | JCSCacheManager.USE_BLOCK_CACHE.get()) {
|
---|
94 | // reducing size of the cache, this requires deletion of the files
|
---|
95 | removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get());
|
---|
96 | }
|
---|
97 | AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.put((Integer) this.maxElementsOnDisk.getValue());
|
---|
98 | restartRequired = true;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | if (!CachedTileLoaderFactory.PROP_TILECACHE_DIR.get().equals(this.tilecacheDir.getText())) {
|
---|
103 | restartRequired = true;
|
---|
104 | removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get()); // clear old cache directory
|
---|
105 | CachedTileLoaderFactory.PROP_TILECACHE_DIR.put(this.tilecacheDir.getText());
|
---|
106 | }
|
---|
107 |
|
---|
108 | if (!AbstractTileSourceLayer.ZOOM_OFFSET.get().equals(this.tilesZoom.getValue())) {
|
---|
109 | // TODO: make warning about too small MEMORY_CACHE_SIZE?
|
---|
110 | AbstractTileSourceLayer.ZOOM_OFFSET.put(this.tilesZoom.getValue());
|
---|
111 | restartRequired = true;
|
---|
112 | }
|
---|
113 | return restartRequired;
|
---|
114 | }
|
---|
115 |
|
---|
116 | private static void removeCacheFiles(String path) {
|
---|
117 | File directory = new File(path);
|
---|
118 | File[] cacheFiles = directory.listFiles((FilenameFilter) (dir, name) -> name.endsWith(".data") || name.endsWith(".key"));
|
---|
119 | JCSCacheManager.shutdown(); // shutdown Cache - so files can by safely deleted
|
---|
120 | if (cacheFiles != null) {
|
---|
121 | for (File cacheFile: cacheFiles) {
|
---|
122 | Utils.deleteFile(cacheFile);
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 | }
|
---|