1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.projection;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.awt.event.ActionListener;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import javax.swing.ButtonGroup;
|
---|
14 | import javax.swing.JLabel;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 | import javax.swing.JRadioButton;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.tools.GBC;
|
---|
20 |
|
---|
21 | public class UTMProjectionChoice extends ListProjectionChoice {
|
---|
22 |
|
---|
23 | /** Earth emispheres **/
|
---|
24 | public enum Hemisphere {
|
---|
25 | /** North emisphere */
|
---|
26 | North,
|
---|
27 | /** South emisphere */
|
---|
28 | South
|
---|
29 | }
|
---|
30 |
|
---|
31 | private static final Hemisphere DEFAULT_HEMISPHERE = Hemisphere.North;
|
---|
32 |
|
---|
33 | private Hemisphere hemisphere;
|
---|
34 |
|
---|
35 | private static final List<String> cbEntries = new ArrayList<>();
|
---|
36 | static {
|
---|
37 | for (int i = 1; i <= 60; i++) {
|
---|
38 | cbEntries.add(Integer.toString(i));
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Constructs a new {@code UTMProjectionChoice}.
|
---|
44 | */
|
---|
45 | public UTMProjectionChoice() {
|
---|
46 | super(tr("UTM"), /* NO-ICON */ "core:utm", cbEntries.toArray(new String[0]), tr("UTM Zone"));
|
---|
47 | }
|
---|
48 |
|
---|
49 | private class UTMPanel extends CBPanel {
|
---|
50 |
|
---|
51 | public JRadioButton north, south;
|
---|
52 |
|
---|
53 | public UTMPanel(String[] entries, int initialIndex, String label, ActionListener listener) {
|
---|
54 | super(entries, initialIndex, label, listener);
|
---|
55 |
|
---|
56 | north = new JRadioButton();
|
---|
57 | north.setSelected(hemisphere == Hemisphere.North);
|
---|
58 | south = new JRadioButton();
|
---|
59 | south.setSelected(hemisphere == Hemisphere.South);
|
---|
60 |
|
---|
61 | ButtonGroup group = new ButtonGroup();
|
---|
62 | group.add(north);
|
---|
63 | group.add(south);
|
---|
64 |
|
---|
65 | JPanel bPanel = new JPanel();
|
---|
66 | bPanel.setLayout(new GridBagLayout());
|
---|
67 |
|
---|
68 | bPanel.add(new JLabel(tr("North")), GBC.std().insets(5, 5, 0, 5));
|
---|
69 | bPanel.add(north, GBC.std().fill(GBC.HORIZONTAL));
|
---|
70 | bPanel.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
71 | bPanel.add(new JLabel(tr("South")), GBC.std().insets(5, 5, 0, 5));
|
---|
72 | bPanel.add(south, GBC.std().fill(GBC.HORIZONTAL));
|
---|
73 | bPanel.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
|
---|
74 |
|
---|
75 | this.add(new JLabel(tr("Hemisphere")), GBC.std().insets(5,5,0,5));
|
---|
76 | this.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
77 | this.add(bPanel, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
78 | this.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
|
---|
79 |
|
---|
80 | if (listener != null) {
|
---|
81 | north.addActionListener(listener);
|
---|
82 | south.addActionListener(listener);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public JPanel getPreferencePanel(ActionListener listener) {
|
---|
89 | return new UTMPanel(entries, index, label, listener);
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public String getCurrentCode() {
|
---|
94 | int zone = index + 1;
|
---|
95 | int code = 32600 + zone + (hemisphere == Hemisphere.South ? 100 : 0);
|
---|
96 | return "EPSG:" + Integer.toString(code);
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public String getProjectionName() {
|
---|
101 | return tr("UTM");
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public Collection<String> getPreferences(JPanel panel) {
|
---|
107 | if (!(panel instanceof UTMPanel)) {
|
---|
108 | throw new IllegalArgumentException("Unsupported panel: "+panel);
|
---|
109 | }
|
---|
110 | UTMPanel p = (UTMPanel) panel;
|
---|
111 | int idx = p.prefcb.getSelectedIndex();
|
---|
112 | Hemisphere hem = p.south.isSelected()?Hemisphere.South:Hemisphere.North;
|
---|
113 | return Arrays.asList(indexToZone(idx), hem.toString());
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | public String[] allCodes() {
|
---|
118 | List<String> projections = new ArrayList<>(60*4);
|
---|
119 | for (int zone = 1;zone <= 60; zone++) {
|
---|
120 | for (Hemisphere hem : Hemisphere.values()) {
|
---|
121 | projections.add("EPSG:" + (32600 + zone + (hem == Hemisphere.South?100:0)));
|
---|
122 | }
|
---|
123 | }
|
---|
124 | return projections.toArray(new String[projections.size()]);
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Override
|
---|
128 | public Collection<String> getPreferencesFromCode(String code) {
|
---|
129 |
|
---|
130 | if (code.startsWith("EPSG:326") || code.startsWith("EPSG:327")) {
|
---|
131 | try {
|
---|
132 | Hemisphere hem = code.charAt(7)=='6'?Hemisphere.North:Hemisphere.South;
|
---|
133 | String zonestring = code.substring(8);
|
---|
134 | int zoneval = Integer.parseInt(zonestring);
|
---|
135 | if(zoneval > 0 && zoneval <= 60)
|
---|
136 | return Arrays.asList(zonestring, hem.toString());
|
---|
137 | } catch(NumberFormatException e) {
|
---|
138 | Main.warn(e);
|
---|
139 | }
|
---|
140 | }
|
---|
141 | return null;
|
---|
142 | }
|
---|
143 |
|
---|
144 | @Override
|
---|
145 | public void setPreferences(Collection<String> args) {
|
---|
146 | super.setPreferences(args);
|
---|
147 | Hemisphere hem = DEFAULT_HEMISPHERE;
|
---|
148 |
|
---|
149 | if (args != null) {
|
---|
150 | String[] array = args.toArray(new String[args.size()]);
|
---|
151 |
|
---|
152 | if (array.length > 1) {
|
---|
153 | hem = Hemisphere.valueOf(array[1]);
|
---|
154 | }
|
---|
155 | }
|
---|
156 | this.hemisphere = hem;
|
---|
157 | }
|
---|
158 |
|
---|
159 | @Override
|
---|
160 | protected String indexToZone(int idx) {
|
---|
161 | return Integer.toString(idx + 1);
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Override
|
---|
165 | protected int zoneToIndex(String zone) {
|
---|
166 | try {
|
---|
167 | return Integer.parseInt(zone) - 1;
|
---|
168 | } catch(NumberFormatException e) {
|
---|
169 | Main.warn(e);
|
---|
170 | }
|
---|
171 | return defaultIndex;
|
---|
172 | }
|
---|
173 | }
|
---|