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.JLabel;
009import javax.swing.JPanel;
010import javax.swing.JSlider;
011
012import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
013import org.openstreetmap.josm.gui.layer.ImageryLayer;
014import org.openstreetmap.josm.gui.widgets.JosmComboBox;
015import org.openstreetmap.josm.tools.GBC;
016import org.openstreetmap.josm.tools.Utils;
017
018/**
019 * {@code JPanel} giving access to common imagery settings.
020 * @since 5465
021 */
022public class CommonSettingsPanel extends JPanel {
023
024    // Common Settings
025    private final JosmComboBox<String> sharpen;
026    private final JSlider tilesZoom = new JSlider(-2, 2, 0);
027
028    /**
029     * Constructs a new {@code CommonSettingsPanel}.
030     */
031    public CommonSettingsPanel() {
032        super(new GridBagLayout());
033
034        this.sharpen = new JosmComboBox<>(new String[] {
035                tr("None"),
036                tr("Soft"),
037                tr("Strong")});
038        add(new JLabel(tr("Sharpen (requires layer re-add): ")));
039        add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
040        add(this.sharpen, GBC.eol().fill(GBC.HORIZONTAL));
041
042        this.tilesZoom.setPaintLabels(true);
043        this.tilesZoom.setMajorTickSpacing(2);
044        this.tilesZoom.setMinorTickSpacing(1);
045        this.tilesZoom.setPaintTicks(true);
046        add(new JLabel(tr("Tiles zoom offset:")));
047        add(GBC.glue(5, 0), GBC.std());
048        add(this.tilesZoom, GBC.eol());
049    }
050
051    /**
052     * Loads the common settings.
053     */
054    public void loadSettings() {
055        this.sharpen.setSelectedIndex(Utils.clamp(ImageryLayer.PROP_SHARPEN_LEVEL.get(), 0, 2));
056        this.tilesZoom.setValue(AbstractTileSourceLayer.ZOOM_OFFSET.get());
057    }
058
059    /**
060     * Saves the common settings.
061     * @return true when restart is required
062     */
063    public boolean saveSettings() {
064        ImageryLayer.PROP_SHARPEN_LEVEL.put(sharpen.getSelectedIndex());
065
066        boolean restartRequired = false;
067        if (!AbstractTileSourceLayer.ZOOM_OFFSET.get().equals(this.tilesZoom.getValue())) {
068            // TODO: make warning about too small MEMORY_CACHE_SIZE?
069            AbstractTileSourceLayer.ZOOM_OFFSET.put(this.tilesZoom.getValue());
070            restartRequired = true;
071        }
072        return restartRequired;
073    }
074}