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