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.GridBagLayout;
007import java.awt.event.ActionListener;
008import java.util.ArrayList;
009import java.util.Arrays;
010import java.util.Collection;
011import java.util.List;
012
013import javax.swing.ButtonGroup;
014import javax.swing.JLabel;
015import javax.swing.JPanel;
016import javax.swing.JRadioButton;
017
018import org.openstreetmap.josm.data.projection.proj.TransverseMercator.Hemisphere;
019import org.openstreetmap.josm.tools.GBC;
020import org.openstreetmap.josm.tools.Logging;
021
022/**
023 * ProjectionChoice for UTM.
024 * @see <a href="https://en.wikipedia.org/wiki/Universal_Transverse_Mercator_coordinate_system">UTM</a>
025 */
026public class UTMProjectionChoice extends ListProjectionChoice {
027
028    private static final Hemisphere DEFAULT_HEMISPHERE = Hemisphere.North;
029
030    private Hemisphere hemisphere;
031
032    private static final List<String> cbEntries = new ArrayList<>();
033    static {
034        for (int i = 1; i <= 60; i++) {
035            cbEntries.add(Integer.toString(i));
036        }
037    }
038
039    /**
040     * Constructs a new {@code UTMProjectionChoice}.
041     */
042    public UTMProjectionChoice() {
043        super(tr("UTM"), /* NO-ICON */ "core:utm", cbEntries.toArray(new String[0]), tr("UTM Zone"));
044    }
045
046    private class UTMPanel extends CBPanel {
047
048        public JRadioButton north, south;
049
050        UTMPanel(String[] entries, int initialIndex, String label, ActionListener listener) {
051            super(entries, initialIndex, label, listener);
052
053            north = new JRadioButton();
054            north.setSelected(hemisphere == Hemisphere.North);
055            south = new JRadioButton();
056            south.setSelected(hemisphere == Hemisphere.South);
057
058            ButtonGroup group = new ButtonGroup();
059            group.add(north);
060            group.add(south);
061
062            JPanel bPanel = new JPanel(new GridBagLayout());
063
064            bPanel.add(new JLabel(tr("North")), GBC.std().insets(5, 5, 0, 5));
065            bPanel.add(north, GBC.std().fill(GBC.HORIZONTAL));
066            bPanel.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
067            bPanel.add(new JLabel(tr("South")), GBC.std().insets(5, 5, 0, 5));
068            bPanel.add(south, GBC.std().fill(GBC.HORIZONTAL));
069            bPanel.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
070
071            this.add(new JLabel(tr("Hemisphere")), GBC.std().insets(5, 5, 0, 5));
072            this.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
073            this.add(bPanel, GBC.eop().fill(GBC.HORIZONTAL));
074            this.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
075
076            if (listener != null) {
077                north.addActionListener(listener);
078                south.addActionListener(listener);
079            }
080        }
081    }
082
083    @Override
084    public JPanel getPreferencePanel(ActionListener listener) {
085        return new UTMPanel(entries, index, label, listener);
086    }
087
088    @Override
089    public String getCurrentCode() {
090        int zone = index + 1;
091        int code = 32600 + zone + (hemisphere == Hemisphere.South ? 100 : 0);
092        return "EPSG:" + Integer.toString(code);
093    }
094
095    @Override
096    public String getProjectionName() {
097        return tr("UTM");
098    }
099
100    @Override
101    public Collection<String> getPreferences(JPanel panel) {
102        if (!(panel instanceof UTMPanel)) {
103            throw new IllegalArgumentException("Unsupported panel: "+panel);
104        }
105        UTMPanel p = (UTMPanel) panel;
106        int idx = p.prefcb.getSelectedIndex();
107        Hemisphere hem = p.south.isSelected() ? Hemisphere.South : Hemisphere.North;
108        return Arrays.asList(indexToZone(idx), hem.toString());
109    }
110
111    @Override
112    public String[] allCodes() {
113        List<String> projections = new ArrayList<>(60*4);
114        for (int zone = 1; zone <= 60; zone++) {
115            for (Hemisphere hem : Hemisphere.values()) {
116                projections.add("EPSG:" + (32600 + zone + (hem == Hemisphere.South ? 100 : 0)));
117            }
118        }
119        return projections.toArray(new String[0]);
120    }
121
122    @Override
123    public Collection<String> getPreferencesFromCode(String code) {
124
125        if (code.startsWith("EPSG:326") || code.startsWith("EPSG:327")) {
126            try {
127                Hemisphere hem = code.charAt(7) == '6' ? Hemisphere.North : Hemisphere.South;
128                String zonestring = code.substring(8);
129                int zoneval = Integer.parseInt(zonestring);
130                if (zoneval > 0 && zoneval <= 60)
131                    return Arrays.asList(zonestring, hem.toString());
132            } catch (NumberFormatException e) {
133                Logging.warn(e);
134            }
135        }
136        return null;
137    }
138
139    @Override
140    public void setPreferences(Collection<String> args) {
141        super.setPreferences(args);
142        Hemisphere hem = DEFAULT_HEMISPHERE;
143
144        if (args != null) {
145            String[] array = args.toArray(new String[0]);
146
147            if (array.length > 1) {
148                hem = Hemisphere.valueOf(array[1]);
149            }
150        }
151        this.hemisphere = hem;
152    }
153
154    @Override
155    protected String indexToZone(int idx) {
156        return Integer.toString(idx + 1);
157    }
158
159    @Override
160    protected int zoneToIndex(String zone) {
161        try {
162            return Integer.parseInt(zone) - 1;
163        } catch (NumberFormatException e) {
164            Logging.warn(e);
165        }
166        return defaultIndex;
167    }
168}