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 Lambert CC (9 zones, EPSG:3942-3950).
020 * <p>
021 * @see <a href="https://fr.wikipedia.org/wiki/Projection_conique_conforme_de_Lambert#Lambert_zone_CC">Lambert CC</a>
022 */
023public class LambertCC9ZonesProjectionChoice extends ListProjectionChoice {
024
025    private static final String[] LAMBERT_9_ZONES = {
026        tr("{0} ({1} to {2} degrees)", 1, 41, 43),
027        tr("{0} ({1} to {2} degrees)", 2, 42, 44),
028        tr("{0} ({1} to {2} degrees)", 3, 43, 45),
029        tr("{0} ({1} to {2} degrees)", 4, 44, 46),
030        tr("{0} ({1} to {2} degrees)", 5, 45, 47),
031        tr("{0} ({1} to {2} degrees)", 6, 46, 48),
032        tr("{0} ({1} to {2} degrees)", 7, 47, 49),
033        tr("{0} ({1} to {2} degrees)", 8, 48, 50),
034        tr("{0} ({1} to {2} degrees)", 9, 49, 51)
035    };
036
037    /**
038     * Constructs a new {@code LambertCC9ZonesProjectionChoice}.
039     */
040    public LambertCC9ZonesProjectionChoice() {
041        super(tr("Lambert CC9 Zone (France)"), /* NO-ICON */ "core:lambertcc9", LAMBERT_9_ZONES, tr("Lambert CC Zone"));
042    }
043
044    private static class LambertCC9CBPanel extends CBPanel {
045        LambertCC9CBPanel(String[] entries, int initialIndex, String label, ActionListener listener) {
046            super(entries, initialIndex, label, listener);
047            this.add(new JLabel(ImageProvider.get("data/projection", "LambertCC9Zones")), GBC.eol().fill(GBC.HORIZONTAL));
048            this.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
049        }
050    }
051
052    @Override
053    public JPanel getPreferencePanel(ActionListener listener) {
054        return new LambertCC9CBPanel(entries, index, label, listener);
055    }
056
057    @Override
058    public String getCurrentCode() {
059        return "EPSG:" + Integer.toString(3942+index); //CC42 is EPSG:3942 (up to EPSG:3950 for CC50)
060    }
061
062    @Override
063    public String getProjectionName() {
064        return tr("Lambert CC9 Zone (France)");
065    }
066
067    @Override
068    public String[] allCodes() {
069        return IntStream.range(0, 9).mapToObj(zone -> "EPSG:" + (3942 + zone)).toArray(String[]::new);
070    }
071
072    @Override
073    public Collection<String> getPreferencesFromCode(String code) {
074        //zone 1=CC42=EPSG:3942 up to zone 9=CC50=EPSG:3950
075        if (code.startsWith("EPSG:39") && code.length() == 9) {
076            try {
077                String zonestring = code.substring(5, 9);
078                int zoneval = Integer.parseInt(zonestring)-3942;
079                if (zoneval >= 0 && zoneval <= 8)
080                    return Collections.singleton(String.valueOf(zoneval+1));
081            } catch (NumberFormatException ex) {
082                Logging.warn(ex);
083            }
084        }
085        return null;
086    }
087
088    @Override
089    protected String indexToZone(int idx) {
090        return Integer.toString(idx + 1);
091    }
092
093    @Override
094    protected int zoneToIndex(String zone) {
095        try {
096            return Integer.parseInt(zone) - 1;
097        } catch (NumberFormatException e) {
098            Logging.warn(e);
099        }
100        return defaultIndex;
101    }
102
103}