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