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