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

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

Sonar - fix various issues

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