source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/projection/ProjectionPreference.java@ 5546

Last change on this file since 5546 was 5546, checked in by bastiK, 11 years ago

remove old migration code

  • Property svn:eol-style set to native
File size: 13.4 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui.preferences.projection;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.GridBagLayout;
8import java.awt.event.ActionEvent;
9import java.awt.event.ActionListener;
10import java.util.ArrayList;
11import java.util.Collection;
12import java.util.Collections;
13import java.util.HashMap;
14import java.util.List;
15import java.util.Map;
16
17import javax.swing.BorderFactory;
18import javax.swing.JLabel;
19import javax.swing.JOptionPane;
20import javax.swing.JPanel;
21import javax.swing.JScrollPane;
22import javax.swing.JSeparator;
23
24import org.openstreetmap.josm.Main;
25import org.openstreetmap.josm.data.Bounds;
26import org.openstreetmap.josm.data.coor.CoordinateFormat;
27import org.openstreetmap.josm.data.preferences.CollectionProperty;
28import org.openstreetmap.josm.data.preferences.StringProperty;
29import org.openstreetmap.josm.data.projection.BelgianLambert1972;
30import org.openstreetmap.josm.data.projection.BelgianLambert2008;
31import org.openstreetmap.josm.data.projection.Epsg3008;
32import org.openstreetmap.josm.data.projection.Epsg4326;
33import org.openstreetmap.josm.data.projection.Lambert93;
34import org.openstreetmap.josm.data.projection.LambertEST;
35import org.openstreetmap.josm.data.projection.Mercator;
36import org.openstreetmap.josm.data.projection.Projection;
37import org.openstreetmap.josm.data.projection.TransverseMercatorLV;
38import org.openstreetmap.josm.gui.NavigatableComponent;
39import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
40import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
41import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
42import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
43import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
44import org.openstreetmap.josm.gui.widgets.JosmComboBox;
45import org.openstreetmap.josm.tools.GBC;
46
47public class ProjectionPreference implements SubPreferenceSetting {
48
49 public static class Factory implements PreferenceSettingFactory {
50 public PreferenceSetting createPreferenceSetting() {
51 return new ProjectionPreference();
52 }
53 }
54
55 private static List<ProjectionChoice> projectionChoices = new ArrayList<ProjectionChoice>();
56 private static Map<String, ProjectionChoice> projectionChoicesById = new HashMap<String, ProjectionChoice>();
57
58 public static ProjectionChoice mercator = new SingleProjectionChoice("core:mercator", new Mercator());
59 static {
60 // global projections
61 registerProjectionChoice("core:wgs84", new Epsg4326());
62 registerProjectionChoice(mercator);
63 registerProjectionChoice(new UTMProjectionChoice());
64 // regional - alphabetical order by country code
65 registerProjectionChoice("core:belambert1972", new BelgianLambert1972()); // BE
66 registerProjectionChoice("core:belambert2008", new BelgianLambert2008()); // BE
67 registerProjectionChoice(new SwissGridProjectionChoice()); // CH
68 registerProjectionChoice(new GaussKruegerProjectionChoice()); // DE
69 registerProjectionChoice("core:lambertest", new LambertEST()); // EE
70 registerProjectionChoice(new LambertProjectionChoice()); // FR
71 registerProjectionChoice("core:lambert93", new Lambert93()); // FR
72 registerProjectionChoice(new LambertCC9ZonesProjectionChoice()); // FR
73 registerProjectionChoice(new UTM_France_DOM_ProjectionChoice()); // FR
74 registerProjectionChoice("core:tmerclv", new TransverseMercatorLV()); // LV
75 registerProjectionChoice(new PuwgProjectionChoice()); // PL
76 registerProjectionChoice("core:sweref99", new Epsg3008()); // SE
77 registerProjectionChoice(new CustomProjectionChoice());
78 }
79
80 public static void registerProjectionChoice(ProjectionChoice c) {
81 projectionChoices.add(c);
82 projectionChoicesById.put(c.getId(), c);
83 }
84
85 public static void registerProjectionChoice(String id, Projection projection) {
86 registerProjectionChoice(new SingleProjectionChoice(id, projection));
87 }
88
89 public static List<ProjectionChoice> getProjectionChoices() {
90 return Collections.unmodifiableList(projectionChoices);
91 }
92
93 private static final StringProperty PROP_PROJECTION = new StringProperty("projection", mercator.getId());
94 private static final StringProperty PROP_COORDINATES = new StringProperty("coordinates", null);
95 private static final CollectionProperty PROP_SUB_PROJECTION = new CollectionProperty("projection.sub", null);
96 public static final StringProperty PROP_SYSTEM_OF_MEASUREMENT = new StringProperty("system_of_measurement", "Metric");
97 private static final String[] unitsValues = (new ArrayList<String>(NavigatableComponent.SYSTEMS_OF_MEASUREMENT.keySet())).toArray(new String[0]);
98 private static final String[] unitsValuesTr = new String[unitsValues.length];
99 static {
100 for (int i=0; i<unitsValues.length; ++i) {
101 unitsValuesTr[i] = tr(unitsValues[i]);
102 }
103 }
104
105 /**
106 * Combobox with all projections available
107 */
108 private JosmComboBox projectionCombo = new JosmComboBox(projectionChoices.toArray());
109
110 /**
111 * Combobox with all coordinate display possibilities
112 */
113 private JosmComboBox coordinatesCombo = new JosmComboBox(CoordinateFormat.values());
114
115 private JosmComboBox unitsCombo = new JosmComboBox(unitsValuesTr);
116
117 /**
118 * This variable holds the JPanel with the projection's preferences. If the
119 * selected projection does not implement this, it will be set to an empty
120 * Panel.
121 */
122 private JPanel projSubPrefPanel;
123 private JPanel projSubPrefPanelWrapper = new JPanel(new GridBagLayout());
124
125 private JLabel projectionCodeLabel;
126 private Component projectionCodeGlue;
127 private JLabel projectionCode = new JLabel();
128 private JLabel bounds = new JLabel();
129
130 /**
131 * This is the panel holding all projection preferences
132 */
133 private JPanel projPanel = new JPanel(new GridBagLayout());
134
135 /**
136 * The GridBagConstraints for the Panel containing the ProjectionSubPrefs.
137 * This is required twice in the code, creating it here keeps both occurrences
138 * in sync
139 */
140 static private GBC projSubPrefPanelGBC = GBC.std().fill(GBC.BOTH).weight(1.0, 1.0);
141
142 public void addGui(PreferenceTabbedPane gui) {
143 ProjectionChoice pc = setupProjectionCombo();
144
145 for (int i = 0; i < coordinatesCombo.getItemCount(); ++i) {
146 if (((CoordinateFormat)coordinatesCombo.getItemAt(i)).name().equals(PROP_COORDINATES.get())) {
147 coordinatesCombo.setSelectedIndex(i);
148 break;
149 }
150 }
151
152 for (int i = 0; i < unitsValues.length; ++i) {
153 if (unitsValues[i].equals(PROP_SYSTEM_OF_MEASUREMENT.get())) {
154 unitsCombo.setSelectedIndex(i);
155 break;
156 }
157 }
158
159 projPanel.setBorder(BorderFactory.createEmptyBorder( 0, 0, 0, 0 ));
160 projPanel.setLayout(new GridBagLayout());
161 projPanel.add(new JLabel(tr("Projection method")), GBC.std().insets(5,5,0,5));
162 projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
163 projPanel.add(projectionCombo, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5));
164 projPanel.add(projectionCodeLabel = new JLabel(tr("Projection code")), GBC.std().insets(25,5,0,5));
165 projPanel.add(projectionCodeGlue = GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
166 projPanel.add(projectionCode, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5));
167 projPanel.add(new JLabel(tr("Bounds")), GBC.std().insets(25,5,0,5));
168 projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
169 projPanel.add(bounds, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5));
170 projPanel.add(projSubPrefPanelWrapper, GBC.eol().fill(GBC.HORIZONTAL).insets(20,5,5,5));
171
172 projPanel.add(new JSeparator(), GBC.eol().fill(GBC.HORIZONTAL).insets(0,5,0,10));
173 projPanel.add(new JLabel(tr("Display coordinates as")), GBC.std().insets(5,5,0,5));
174 projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
175 projPanel.add(coordinatesCombo, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5));
176 projPanel.add(new JLabel(tr("System of measurement")), GBC.std().insets(5,5,0,5));
177 projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
178 projPanel.add(unitsCombo, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5));
179 projPanel.add(GBC.glue(1,1), GBC.std().fill(GBC.HORIZONTAL).weight(1.0, 1.0));
180
181 JScrollPane scrollpane = new JScrollPane(projPanel);
182 gui.getMapPreference().mapcontent.addTab(tr("Map Projection"), scrollpane);
183
184 selectedProjectionChanged(pc);
185 }
186
187 private void updateMeta(ProjectionChoice pc) {
188 pc.setPreferences(pc.getPreferences(projSubPrefPanel));
189 Projection proj = pc.getProjection();
190 projectionCode.setText(proj.toCode());
191 Bounds b = proj.getWorldBoundsLatLon();
192 CoordinateFormat cf = CoordinateFormat.getDefaultFormat();
193 bounds.setText(b.getMin().lonToString(cf)+", "+b.getMin().latToString(cf)+" : "+b.getMax().lonToString(cf)+", "+b.getMax().latToString(cf));
194 boolean showCode = true;
195 if (pc instanceof SubPrefsOptions) {
196 showCode = ((SubPrefsOptions) pc).showProjectionCode();
197 }
198 projectionCodeLabel.setVisible(showCode);
199 projectionCodeGlue.setVisible(showCode);
200 projectionCode.setVisible(showCode);
201 }
202
203 @Override
204 public boolean ok() {
205 ProjectionChoice pc = (ProjectionChoice) projectionCombo.getSelectedItem();
206
207 String id = pc.getId();
208 Collection<String> prefs = pc.getPreferences(projSubPrefPanel);
209
210 setProjection(id, prefs);
211
212 if(PROP_COORDINATES.put(((CoordinateFormat)coordinatesCombo.getSelectedItem()).name())) {
213 CoordinateFormat.setCoordinateFormat((CoordinateFormat)coordinatesCombo.getSelectedItem());
214 }
215
216 int i = unitsCombo.getSelectedIndex();
217 PROP_SYSTEM_OF_MEASUREMENT.put(unitsValues[i]);
218
219 return false;
220 }
221
222 static public void setProjection() {
223 setProjection(PROP_PROJECTION.get(), PROP_SUB_PROJECTION.get());
224 }
225
226 static public void setProjection(String id, Collection<String> pref) {
227 ProjectionChoice pc = projectionChoicesById.get(id);
228
229 if (pc == null) {
230 JOptionPane.showMessageDialog(
231 Main.parent,
232 tr("The projection {0} could not be activated. Using Mercator", id),
233 tr("Error"),
234 JOptionPane.ERROR_MESSAGE
235 );
236 pref = null;
237 pc = mercator;
238 }
239 id = pc.getId();
240 PROP_PROJECTION.put(id);
241 PROP_SUB_PROJECTION.put(pref);
242 Main.pref.putCollection("projection.sub."+id, pref);
243 pc.setPreferences(pref);
244 Projection proj = pc.getProjection();
245 Main.setProjection(proj);
246 }
247
248 /**
249 * Handles all the work related to update the projection-specific
250 * preferences
251 * @param proj
252 */
253 private void selectedProjectionChanged(final ProjectionChoice pc) {
254 // Don't try to update if we're still starting up
255 int size = projPanel.getComponentCount();
256 if(size < 1)
257 return;
258
259 final ActionListener listener = new ActionListener() {
260 @Override
261 public void actionPerformed(ActionEvent e) {
262 updateMeta(pc);
263 }
264 };
265
266 // Replace old panel with new one
267 projSubPrefPanelWrapper.removeAll();
268 projSubPrefPanel = pc.getPreferencePanel(listener);
269 projSubPrefPanelWrapper.add(projSubPrefPanel, projSubPrefPanelGBC);
270 projPanel.revalidate();
271 projSubPrefPanel.repaint();
272 updateMeta(pc);
273 }
274
275 /**
276 * Sets up projection combobox with default values and action listener
277 */
278 private ProjectionChoice setupProjectionCombo() {
279 ProjectionChoice pc = null;
280 for (int i = 0; i < projectionCombo.getItemCount(); ++i) {
281 ProjectionChoice pc1 = (ProjectionChoice) projectionCombo.getItemAt(i);
282 pc1.setPreferences(getSubprojectionPreference(pc1));
283 if (pc1.getId().equals(PROP_PROJECTION.get())) {
284 projectionCombo.setSelectedIndex(i);
285 selectedProjectionChanged(pc1);
286 pc = pc1;
287 }
288 }
289 // If the ProjectionChoice from the preferences is not available, it
290 // should have been set to Mercator at JOSM start.
291 if (pc == null)
292 throw new RuntimeException("Couldn't find the current projection in the list of available projections!");
293
294 projectionCombo.addActionListener(new ActionListener() {
295 public void actionPerformed(ActionEvent e) {
296 ProjectionChoice pc = (ProjectionChoice) projectionCombo.getSelectedItem();
297 selectedProjectionChanged(pc);
298 }
299 });
300 return pc;
301 }
302
303 private Collection<String> getSubprojectionPreference(ProjectionChoice pc) {
304 return Main.pref.getCollection("projection.sub."+pc.getId(), null);
305 }
306
307 @Override
308 public boolean isExpert() {
309 return false;
310 }
311
312 @Override
313 public TabPreferenceSetting getTabPreferenceSetting(final PreferenceTabbedPane gui) {
314 return gui.getMapPreference();
315 }
316}
Note: See TracBrowser for help on using the repository browser.