source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/projection/UTMProjectionChoice.java@ 13167

Last change on this file since 13167 was 13167, checked in by Don-vip, 6 years ago

fix #13694 - Display UTM zone in node info panel (no i18n impact)

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