001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.preferences.imagery;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.GridBagLayout;
007
008import javax.swing.JCheckBox;
009import javax.swing.JLabel;
010import javax.swing.JPanel;
011import javax.swing.JSpinner;
012import javax.swing.SpinnerNumberModel;
013
014import org.openstreetmap.josm.data.imagery.TMSCachedTileLoader;
015import org.openstreetmap.josm.gui.layer.TMSLayer;
016import org.openstreetmap.josm.gui.layer.imagery.TileSourceDisplaySettings;
017import org.openstreetmap.josm.tools.GBC;
018import org.openstreetmap.josm.tools.Utils;
019
020/**
021 * {@code JPanel} giving access to TMS settings.
022 * @since 5465
023 */
024public class TMSSettingsPanel extends JPanel {
025
026    // TMS Settings
027    private final JCheckBox autozoomActive = new JCheckBox();
028    private final JCheckBox autoloadTiles = new JCheckBox();
029    private final JSpinner minZoomLvl;
030    private final JSpinner maxZoomLvl;
031    private final JCheckBox addToSlippyMapChosser = new JCheckBox();
032
033    private final JSpinner maxConcurrentDownloads;
034    private final JSpinner maxDownloadsPerHost;
035
036    /**
037     * Constructs a new {@code TMSSettingsPanel}.
038     */
039    public TMSSettingsPanel() {
040        super(new GridBagLayout());
041        minZoomLvl = new JSpinner(new SpinnerNumberModel(
042                Utils.clamp(TMSLayer.PROP_MIN_ZOOM_LVL.get().intValue(), TMSLayer.MIN_ZOOM, TMSLayer.MAX_ZOOM),
043                TMSLayer.MIN_ZOOM,
044                TMSLayer.MAX_ZOOM, 1));
045        maxZoomLvl = new JSpinner(new SpinnerNumberModel(
046                Utils.clamp(TMSLayer.PROP_MAX_ZOOM_LVL.get().intValue(), TMSLayer.MIN_ZOOM, TMSLayer.MAX_ZOOM),
047                TMSLayer.MIN_ZOOM,
048                TMSLayer.MAX_ZOOM, 1));
049        maxConcurrentDownloads = new JSpinner(new SpinnerNumberModel(
050                TMSCachedTileLoader.THREAD_LIMIT.get().intValue(), 0, Integer.MAX_VALUE, 1));
051        maxDownloadsPerHost = new JSpinner(new SpinnerNumberModel(
052                TMSCachedTileLoader.HOST_LIMIT.get().intValue(), 0, Integer.MAX_VALUE, 1));
053
054
055        add(new JLabel(tr("Auto zoom by default: ")), GBC.std());
056        add(GBC.glue(5, 0), GBC.std());
057        add(autozoomActive, GBC.eol().fill(GBC.HORIZONTAL));
058
059        add(new JLabel(tr("Autoload tiles by default: ")), GBC.std());
060        add(GBC.glue(5, 0), GBC.std());
061        add(autoloadTiles, GBC.eol().fill(GBC.HORIZONTAL));
062
063        add(new JLabel(tr("Min. zoom level: ")), GBC.std());
064        add(GBC.glue(5, 0), GBC.std());
065        add(this.minZoomLvl, GBC.eol());
066
067        add(new JLabel(tr("Max. zoom level: ")), GBC.std());
068        add(GBC.glue(5, 0), GBC.std());
069        add(this.maxZoomLvl, GBC.eol());
070
071        add(new JLabel(tr("Add to slippymap chooser: ")), GBC.std());
072        add(GBC.glue(5, 0), GBC.std());
073        add(addToSlippyMapChosser, GBC.eol().fill(GBC.HORIZONTAL));
074
075        add(new JLabel(tr("Maximum concurrent downloads: ")), GBC.std());
076        add(GBC.glue(5, 0), GBC.std());
077        add(maxConcurrentDownloads, GBC.eol());
078
079        add(new JLabel(tr("Maximum concurrent downloads per host: ")), GBC.std());
080        add(GBC.glue(5, 0), GBC.std());
081        add(maxDownloadsPerHost, GBC.eol());
082
083    }
084
085    /**
086     * Loads the TMS settings.
087     */
088    public void loadSettings() {
089        this.autozoomActive.setSelected(TileSourceDisplaySettings.PROP_AUTO_ZOOM.get());
090        this.autoloadTiles.setSelected(TileSourceDisplaySettings.PROP_AUTO_LOAD.get());
091        this.addToSlippyMapChosser.setSelected(TMSLayer.PROP_ADD_TO_SLIPPYMAP_CHOOSER.get());
092        this.maxZoomLvl.setValue(TMSLayer.getMaxZoomLvl(null));
093        this.minZoomLvl.setValue(TMSLayer.getMinZoomLvl(null));
094        this.maxConcurrentDownloads.setValue(TMSCachedTileLoader.THREAD_LIMIT.get());
095        this.maxDownloadsPerHost.setValue(TMSCachedTileLoader.HOST_LIMIT.get());
096    }
097
098    /**
099     * Saves the TMS settings.
100     * @return true when restart is required
101     */
102    public boolean saveSettings() {
103        boolean restartRequired = false;
104
105        if (!TMSLayer.PROP_ADD_TO_SLIPPYMAP_CHOOSER.get().equals(this.addToSlippyMapChosser.isSelected())) {
106            restartRequired = true;
107        }
108        TMSLayer.PROP_ADD_TO_SLIPPYMAP_CHOOSER.put(this.addToSlippyMapChosser.isSelected());
109        TileSourceDisplaySettings.PROP_AUTO_ZOOM.put(this.autozoomActive.isSelected());
110        TileSourceDisplaySettings.PROP_AUTO_LOAD.put(this.autoloadTiles.isSelected());
111        TMSLayer.setMaxZoomLvl((Integer) this.maxZoomLvl.getValue());
112        TMSLayer.setMinZoomLvl((Integer) this.minZoomLvl.getValue());
113
114        if (!TMSCachedTileLoader.THREAD_LIMIT.get().equals(this.maxConcurrentDownloads.getValue())) {
115            TMSCachedTileLoader.THREAD_LIMIT.put((Integer) this.maxConcurrentDownloads.getValue());
116            restartRequired = true;
117        }
118
119        if (!TMSCachedTileLoader.HOST_LIMIT.get().equals(this.maxDownloadsPerHost.getValue())) {
120            TMSCachedTileLoader.HOST_LIMIT.put((Integer) this.maxDownloadsPerHost.getValue());
121            restartRequired = true;
122        }
123
124        return restartRequired;
125    }
126}