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 4 zone Lambert (1920, EPSG:27561-27564).
019 * <p>
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        String[] codes = new String[4];
064        for (int zone = 0; zone < 4; zone++) {
065            codes[zone] = "EPSG:"+(27561+zone);
066        }
067        return codes;
068    }
069
070    @Override
071    public Collection<String> getPreferencesFromCode(String code) {
072        if (code.startsWith("EPSG:2756") && code.length() == 10) {
073            try {
074                String zonestring = code.substring(9);
075                int zoneval = Integer.parseInt(zonestring);
076                if (zoneval >= 1 && zoneval <= 4)
077                    return Collections.singleton(zonestring);
078            } catch (NumberFormatException e) {
079                Logging.warn(e);
080            }
081        }
082        return null;
083    }
084
085    @Override
086    protected String indexToZone(int idx) {
087        return Integer.toString(idx + 1);
088    }
089
090    @Override
091    protected int zoneToIndex(String zone) {
092        try {
093            return Integer.parseInt(zone) - 1;
094        } catch (NumberFormatException e) {
095            Logging.warn(e);
096        }
097        return defaultIndex;
098    }
099}