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