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

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

refactoring - global simplification of use of setLayout method - simply pass layout to JPanel constructor

  • Property svn:eol-style set to native
File size: 5.3 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.Main;
19import org.openstreetmap.josm.tools.GBC;
20
21public 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 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(new GridBagLayout());
66
67 bPanel.add(new JLabel(tr("North")), GBC.std().insets(5, 5, 0, 5));
68 bPanel.add(north, GBC.std().fill(GBC.HORIZONTAL));
69 bPanel.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
70 bPanel.add(new JLabel(tr("South")), GBC.std().insets(5, 5, 0, 5));
71 bPanel.add(south, GBC.std().fill(GBC.HORIZONTAL));
72 bPanel.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
73
74 this.add(new JLabel(tr("Hemisphere")), GBC.std().insets(5, 5, 0, 5));
75 this.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
76 this.add(bPanel, GBC.eop().fill(GBC.HORIZONTAL));
77 this.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
78
79 if (listener != null) {
80 north.addActionListener(listener);
81 south.addActionListener(listener);
82 }
83 }
84 }
85
86 @Override
87 public JPanel getPreferencePanel(ActionListener listener) {
88 return new UTMPanel(entries, index, label, listener);
89 }
90
91 @Override
92 public String getCurrentCode() {
93 int zone = index + 1;
94 int code = 32600 + zone + (hemisphere == Hemisphere.South ? 100 : 0);
95 return "EPSG:" + Integer.toString(code);
96 }
97
98 @Override
99 public String getProjectionName() {
100 return tr("UTM");
101 }
102
103 @Override
104 public Collection<String> getPreferences(JPanel panel) {
105 if (!(panel instanceof UTMPanel)) {
106 throw new IllegalArgumentException("Unsupported panel: "+panel);
107 }
108 UTMPanel p = (UTMPanel) panel;
109 int idx = p.prefcb.getSelectedIndex();
110 Hemisphere hem = p.south.isSelected() ? Hemisphere.South : Hemisphere.North;
111 return Arrays.asList(indexToZone(idx), hem.toString());
112 }
113
114 @Override
115 public String[] allCodes() {
116 List<String> projections = new ArrayList<>(60*4);
117 for (int zone = 1; zone <= 60; zone++) {
118 for (Hemisphere hem : Hemisphere.values()) {
119 projections.add("EPSG:" + (32600 + zone + (hem == Hemisphere.South ? 100 : 0)));
120 }
121 }
122 return projections.toArray(new String[projections.size()]);
123 }
124
125 @Override
126 public Collection<String> getPreferencesFromCode(String code) {
127
128 if (code.startsWith("EPSG:326") || code.startsWith("EPSG:327")) {
129 try {
130 Hemisphere hem = code.charAt(7) == '6' ? Hemisphere.North : Hemisphere.South;
131 String zonestring = code.substring(8);
132 int zoneval = Integer.parseInt(zonestring);
133 if (zoneval > 0 && zoneval <= 60)
134 return Arrays.asList(zonestring, hem.toString());
135 } catch (NumberFormatException e) {
136 Main.warn(e);
137 }
138 }
139 return null;
140 }
141
142 @Override
143 public void setPreferences(Collection<String> args) {
144 super.setPreferences(args);
145 Hemisphere hem = DEFAULT_HEMISPHERE;
146
147 if (args != null) {
148 String[] array = args.toArray(new String[args.size()]);
149
150 if (array.length > 1) {
151 hem = Hemisphere.valueOf(array[1]);
152 }
153 }
154 this.hemisphere = hem;
155 }
156
157 @Override
158 protected String indexToZone(int idx) {
159 return Integer.toString(idx + 1);
160 }
161
162 @Override
163 protected int zoneToIndex(String zone) {
164 try {
165 return Integer.parseInt(zone) - 1;
166 } catch (NumberFormatException e) {
167 Main.warn(e);
168 }
169 return defaultIndex;
170 }
171}
Note: See TracBrowser for help on using the repository browser.