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.Color;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.FilenameFilter;
|
---|
10 |
|
---|
11 | import javax.swing.JButton;
|
---|
12 | import javax.swing.JColorChooser;
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 | import javax.swing.JSlider;
|
---|
17 | import javax.swing.JSpinner;
|
---|
18 | import javax.swing.SpinnerNumberModel;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.data.cache.JCSCacheManager;
|
---|
21 | import org.openstreetmap.josm.data.imagery.CachedTileLoaderFactory;
|
---|
22 | import org.openstreetmap.josm.gui.layer.AbstractCachedTileSourceLayer;
|
---|
23 | import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
|
---|
24 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
25 | import org.openstreetmap.josm.gui.widgets.JosmComboBox;
|
---|
26 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
27 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
28 | import org.openstreetmap.josm.tools.GBC;
|
---|
29 | import org.openstreetmap.josm.tools.Utils;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * {@code JPanel} giving access to common imagery settings.
|
---|
33 | * @since 5465
|
---|
34 | */
|
---|
35 | public class CommonSettingsPanel extends JPanel {
|
---|
36 |
|
---|
37 | // Common Settings
|
---|
38 | private final JButton btnFadeColor;
|
---|
39 | private final JSlider fadeAmount = new JSlider(0, 100);
|
---|
40 | private final JosmComboBox<String> sharpen;
|
---|
41 | private final JosmTextField tilecacheDir = new JosmTextField();
|
---|
42 | private final JSpinner maxElementsOnDisk;
|
---|
43 | private final JSlider tilesZoom = new JSlider(-2, 2, 0);
|
---|
44 |
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Constructs a new {@code CommonSettingsPanel}.
|
---|
48 | */
|
---|
49 | public CommonSettingsPanel() {
|
---|
50 | super(new GridBagLayout());
|
---|
51 |
|
---|
52 | this.maxElementsOnDisk = new JSpinner(new SpinnerNumberModel(
|
---|
53 | AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get().intValue(), 0, Integer.MAX_VALUE, 1));
|
---|
54 |
|
---|
55 | this.btnFadeColor = new JButton();
|
---|
56 | this.btnFadeColor.addActionListener(e -> {
|
---|
57 | JColorChooser chooser = new JColorChooser(btnFadeColor.getBackground());
|
---|
58 | int answer = JOptionPane.showConfirmDialog(
|
---|
59 | this, chooser,
|
---|
60 | tr("Choose a color for {0}", tr("imagery fade")),
|
---|
61 | JOptionPane.OK_CANCEL_OPTION,
|
---|
62 | JOptionPane.PLAIN_MESSAGE);
|
---|
63 | if (answer == JOptionPane.OK_OPTION) {
|
---|
64 | Color colFadeColor = chooser.getColor();
|
---|
65 | btnFadeColor.setBackground(colFadeColor);
|
---|
66 | btnFadeColor.setText(ColorHelper.color2html(colFadeColor));
|
---|
67 | }
|
---|
68 | });
|
---|
69 |
|
---|
70 | add(new JLabel(tr("Fade Color: ")), GBC.std());
|
---|
71 | add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
72 | add(this.btnFadeColor, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
73 |
|
---|
74 | add(new JLabel(tr("Fade amount: ")), GBC.std());
|
---|
75 | add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
76 | add(this.fadeAmount, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
77 |
|
---|
78 | this.sharpen = new JosmComboBox<>(new String[] {
|
---|
79 | tr("None"),
|
---|
80 | tr("Soft"),
|
---|
81 | tr("Strong")});
|
---|
82 | add(new JLabel(tr("Sharpen (requires layer re-add): ")));
|
---|
83 | add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
84 | add(this.sharpen, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
85 |
|
---|
86 | add(new JLabel(tr("Tile cache directory: ")), GBC.std());
|
---|
87 | add(GBC.glue(5, 0), GBC.std());
|
---|
88 | add(tilecacheDir, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
89 |
|
---|
90 | add(new JLabel(tr("Maximum size of disk cache (per imagery) in MB: ")), GBC.std());
|
---|
91 | add(GBC.glue(5, 0), GBC.std());
|
---|
92 | add(this.maxElementsOnDisk, GBC.eol());
|
---|
93 |
|
---|
94 | this.tilesZoom.setPaintLabels(true);
|
---|
95 | this.tilesZoom.setMajorTickSpacing(2);
|
---|
96 | this.tilesZoom.setMinorTickSpacing(1);
|
---|
97 | this.tilesZoom.setPaintTicks(true);
|
---|
98 | add(new JLabel(tr("Tiles zoom offset:")));
|
---|
99 | add(GBC.glue(5, 0), GBC.std());
|
---|
100 | add(this.tilesZoom, GBC.eol());
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Loads the common settings.
|
---|
105 | */
|
---|
106 | public void loadSettings() {
|
---|
107 | Color colFadeColor = ImageryLayer.PROP_FADE_COLOR.get();
|
---|
108 | this.btnFadeColor.setBackground(colFadeColor);
|
---|
109 | this.btnFadeColor.setText(ColorHelper.color2html(colFadeColor));
|
---|
110 | this.fadeAmount.setValue(ImageryLayer.PROP_FADE_AMOUNT.get());
|
---|
111 | this.sharpen.setSelectedIndex(Math.max(0, Math.min(2, ImageryLayer.PROP_SHARPEN_LEVEL.get())));
|
---|
112 | this.tilecacheDir.setText(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get());
|
---|
113 | this.maxElementsOnDisk.setValue(AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get());
|
---|
114 | this.tilesZoom.setValue(AbstractTileSourceLayer.ZOOM_OFFSET.get());
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Saves the common settings.
|
---|
119 | * @return true when restart is required
|
---|
120 | */
|
---|
121 | public boolean saveSettings() {
|
---|
122 | ImageryLayer.PROP_FADE_AMOUNT.put(this.fadeAmount.getValue());
|
---|
123 | ImageryLayer.PROP_FADE_COLOR.put(this.btnFadeColor.getBackground());
|
---|
124 | ImageryLayer.PROP_SHARPEN_LEVEL.put(sharpen.getSelectedIndex());
|
---|
125 |
|
---|
126 | boolean restartRequired = false;
|
---|
127 | if (!AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get().equals(this.maxElementsOnDisk.getValue())) {
|
---|
128 | if (((Integer) this.maxElementsOnDisk.getValue()) < AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.get() &&
|
---|
129 | JCSCacheManager.USE_BLOCK_CACHE.get()) {
|
---|
130 | // reducing size of the cache, this requires deletion of the files
|
---|
131 | removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get());
|
---|
132 | }
|
---|
133 | AbstractCachedTileSourceLayer.MAX_DISK_CACHE_SIZE.put((Integer) this.maxElementsOnDisk.getValue());
|
---|
134 | restartRequired = true;
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | if (!CachedTileLoaderFactory.PROP_TILECACHE_DIR.get().equals(this.tilecacheDir.getText())) {
|
---|
139 | restartRequired = true;
|
---|
140 | removeCacheFiles(CachedTileLoaderFactory.PROP_TILECACHE_DIR.get()); // clear old cache directory
|
---|
141 | CachedTileLoaderFactory.PROP_TILECACHE_DIR.put(this.tilecacheDir.getText());
|
---|
142 | }
|
---|
143 |
|
---|
144 | if (!AbstractTileSourceLayer.ZOOM_OFFSET.get().equals(this.tilesZoom.getValue())) {
|
---|
145 | // TODO: make warning about too small MEMORY_CACHE_SIZE?
|
---|
146 | AbstractTileSourceLayer.ZOOM_OFFSET.put(this.tilesZoom.getValue());
|
---|
147 | restartRequired = true;
|
---|
148 | }
|
---|
149 | return restartRequired;
|
---|
150 | }
|
---|
151 |
|
---|
152 | private void removeCacheFiles(String path) {
|
---|
153 | File directory = new File(path);
|
---|
154 | File[] cacheFiles = directory.listFiles((FilenameFilter) (dir, name) -> name.endsWith(".data") || name.endsWith(".key"));
|
---|
155 | JCSCacheManager.shutdown(); // shutdown Cache - so files can by safely deleted
|
---|
156 | for (File cacheFile: cacheFiles) {
|
---|
157 | Utils.deleteFile(cacheFile);
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|