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

Last change on this file since 10418 was 10418, checked in by wiktorn, 8 years ago

Checkstyle fixes.

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