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