001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.preferences.projection;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.event.ActionListener;
007import java.util.Collection;
008import java.util.Collections;
009import java.util.stream.IntStream;
010
011import javax.swing.JLabel;
012import javax.swing.JPanel;
013
014import org.openstreetmap.josm.tools.GBC;
015import org.openstreetmap.josm.tools.ImageProvider;
016import org.openstreetmap.josm.tools.Logging;
017
018/**
019 * ProjectionChoice for 4 zone Lambert (1920, EPSG:27561-27564).
020 * <p>
021 * @see <a href="https://fr.wikipedia.org/wiki/Projection_conique_conforme_de_Lambert#Lambert_zone">Lambert zone</a>
022 */
023public class LambertProjectionChoice extends ListProjectionChoice {
024
025    private static final String[] LAMBERT_4_ZONES = {
026        tr("{0} ({1} to {2} degrees)", 1, "51.30", "48.15"),
027        tr("{0} ({1} to {2} degrees)", 2, "48.15", "45.45"),
028        tr("{0} ({1} to {2} degrees)", 3, "45.45", "42.76"),
029        tr("{0} (Corsica)", 4)
030    };
031
032    /**
033     * Constructs a new {@code LambertProjectionChoice}.
034     */
035    public LambertProjectionChoice() {
036        super(tr("Lambert 4 Zones (France)"), /* NO-ICON */ "core:lambert", LAMBERT_4_ZONES, tr("Lambert CC Zone"));
037    }
038
039    private static class LambertCBPanel extends CBPanel {
040        LambertCBPanel(String[] entries, int initialIndex, String label, ActionListener listener) {
041            super(entries, initialIndex, label, listener);
042            this.add(new JLabel(ImageProvider.get("data/projection", "Departements_Lambert4Zones")), GBC.eol().fill(GBC.HORIZONTAL));
043            this.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
044        }
045    }
046
047    @Override
048    public JPanel getPreferencePanel(ActionListener listener) {
049        return new LambertCBPanel(entries, index, label, listener);
050    }
051
052    @Override
053    public String getCurrentCode() {
054        return "EPSG:" + Integer.toString(27561+index);
055    }
056
057    @Override
058    public String getProjectionName() {
059        return tr("Lambert 4 Zones (France)");
060    }
061
062    @Override
063    public String[] allCodes() {
064        return IntStream.range(0, 4).mapToObj(zone -> "EPSG:" + (27561 + zone)).toArray(String[]::new);
065    }
066
067    @Override
068    public Collection<String> getPreferencesFromCode(String code) {
069        if (code.startsWith("EPSG:2756") && code.length() == 10) {
070            try {
071                String zonestring = code.substring(9);
072                int zoneval = Integer.parseInt(zonestring);
073                if (zoneval >= 1 && zoneval <= 4)
074                    return Collections.singleton(zonestring);
075            } catch (NumberFormatException e) {
076                Logging.warn(e);
077            }
078        }
079        return null;
080    }
081
082    @Override
083    protected String indexToZone(int idx) {
084        return Integer.toString(idx + 1);
085    }
086
087    @Override
088    protected int zoneToIndex(String zone) {
089        try {
090            return Integer.parseInt(zone) - 1;
091        } catch (NumberFormatException e) {
092            Logging.warn(e);
093        }
094        return defaultIndex;
095    }
096}