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 * @see <a href="https://fr.wikipedia.org/wiki/Projection_conique_conforme_de_Lambert#Lambert_zone">Lambert zone</a>
021 */
022public class LambertProjectionChoice extends ListProjectionChoice {
023
024    private static final String[] LAMBERT_4_ZONES = {
025        tr("{0} ({1} to {2} degrees)", 1, "51.30", "48.15"),
026        tr("{0} ({1} to {2} degrees)", 2, "48.15", "45.45"),
027        tr("{0} ({1} to {2} degrees)", 3, "45.45", "42.76"),
028        tr("{0} (Corsica)", 4)
029    };
030
031    /**
032     * Constructs a new {@code LambertProjectionChoice}.
033     */
034    public LambertProjectionChoice() {
035        super(tr("Lambert 4 Zones (France)"), /* NO-ICON */ "core:lambert", LAMBERT_4_ZONES, tr("Lambert CC Zone"));
036    }
037
038    private static class LambertCBPanel extends CBPanel {
039        LambertCBPanel(String[] entries, int initialIndex, String label, ActionListener listener) {
040            super(entries, initialIndex, label, listener);
041            this.add(new JLabel(ImageProvider.get("data/projection", "Departements_Lambert4Zones")), GBC.eol().fill(GBC.HORIZONTAL));
042            this.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
043        }
044    }
045
046    @Override
047    public JPanel getPreferencePanel(ActionListener listener) {
048        return new LambertCBPanel(entries, index, label, listener);
049    }
050
051    @Override
052    public String getCurrentCode() {
053        return "EPSG:" + Integer.toString(27561+index);
054    }
055
056    @Override
057    public String getProjectionName() {
058        return tr("Lambert 4 Zones (France)");
059    }
060
061    @Override
062    public String[] allCodes() {
063        return IntStream.range(0, 4).mapToObj(zone -> "EPSG:" + (27561 + zone)).toArray(String[]::new);
064    }
065
066    @Override
067    public Collection<String> getPreferencesFromCode(String code) {
068        if (code.startsWith("EPSG:2756") && code.length() == 10) {
069            try {
070                String zonestring = code.substring(9);
071                int zoneval = Integer.parseInt(zonestring);
072                if (zoneval >= 1 && zoneval <= 4)
073                    return Collections.singleton(zonestring);
074            } catch (NumberFormatException e) {
075                Logging.warn(e);
076            }
077        }
078        return null;
079    }
080
081    @Override
082    protected String indexToZone(int idx) {
083        return Integer.toString(idx + 1);
084    }
085
086    @Override
087    protected int zoneToIndex(String zone) {
088        try {
089            return Integer.parseInt(zone) - 1;
090        } catch (NumberFormatException e) {
091            Logging.warn(e);
092        }
093        return defaultIndex;
094    }
095}