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

Last change on this file since 11786 was 11786, checked in by Don-vip, 7 years ago

see #7427 - checkstyle

  • Property svn:eol-style set to native
File size: 4.9 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 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 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 for (File cacheFile: cacheFiles) {
121 Utils.deleteFile(cacheFile);
122 }
123 }
124}
Note: See TracBrowser for help on using the repository browser.