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    /**
030     * Constructs a new {@code CommonSettingsPanel}.
031     */
032    public CommonSettingsPanel() {
033        super(new GridBagLayout());
034
035        this.sharpen = new JosmComboBox<>(new String[] {
036                tr("None"),
037                tr("Soft"),
038                tr("Strong")});
039        add(new JLabel(tr("Sharpen (requires layer re-add): ")));
040        add(GBC.glue(5, 0), GBC.std().fill(GBC.HORIZONTAL));
041        add(this.sharpen, GBC.eol().fill(GBC.HORIZONTAL));
042
043        this.tilesZoom.setPaintLabels(true);
044        this.tilesZoom.setMajorTickSpacing(2);
045        this.tilesZoom.setMinorTickSpacing(1);
046        this.tilesZoom.setPaintTicks(true);
047        add(new JLabel(tr("Tiles zoom offset:")));
048        add(GBC.glue(5, 0), GBC.std());
049        add(this.tilesZoom, GBC.eol());
050    }
051
052    /**
053     * Loads the common settings.
054     */
055    public void loadSettings() {
056        this.sharpen.setSelectedIndex(Utils.clamp(ImageryLayer.PROP_SHARPEN_LEVEL.get(), 0, 2));
057        this.tilesZoom.setValue(AbstractTileSourceLayer.ZOOM_OFFSET.get());
058    }
059
060    /**
061     * Saves the common settings.
062     * @return true when restart is required
063     */
064    public boolean saveSettings() {
065        ImageryLayer.PROP_SHARPEN_LEVEL.put(sharpen.getSelectedIndex());
066
067        boolean restartRequired = false;
068        if (!AbstractTileSourceLayer.ZOOM_OFFSET.get().equals(this.tilesZoom.getValue())) {
069            // TODO: make warning about too small MEMORY_CACHE_SIZE?
070            AbstractTileSourceLayer.ZOOM_OFFSET.put(this.tilesZoom.getValue());
071            restartRequired = true;
072        }
073        return restartRequired;
074    }
075}