1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.projection;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.HashMap;
|
---|
8 |
|
---|
9 | public class ProjectionInfo {
|
---|
10 | private static HashMap<String, Projection> allCodes;
|
---|
11 |
|
---|
12 | private static ProjectionSubPrefs recreateProj(ProjectionSubPrefs proj) {
|
---|
13 | try {
|
---|
14 | return proj.getClass().newInstance();
|
---|
15 | } catch (Exception e) {
|
---|
16 | throw new IllegalStateException(
|
---|
17 | tr("Cannot instantiate projection ''{0}''", proj.getClass().toString()), e);
|
---|
18 | }
|
---|
19 | }
|
---|
20 |
|
---|
21 | static {
|
---|
22 | allCodes = new HashMap<String, Projection>();
|
---|
23 | for (Projection proj : Projections.getProjections()) {
|
---|
24 | if (proj instanceof ProjectionSubPrefs) {
|
---|
25 | ProjectionSubPrefs projSub = recreateProj((ProjectionSubPrefs)proj);
|
---|
26 | for (String code : projSub.allCodes()) {
|
---|
27 | allCodes.put(code, projSub);
|
---|
28 | }
|
---|
29 | } else {
|
---|
30 | allCodes.put(proj.toCode(), proj);
|
---|
31 | }
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | public static Projection getProjectionByCode(String code) {
|
---|
36 | Projection proj = allCodes.get(code);
|
---|
37 | if (proj == null) return null;
|
---|
38 | if (code.equals(proj.toCode())) return proj;
|
---|
39 | if (!(proj instanceof ProjectionSubPrefs))
|
---|
40 | throw new IllegalStateException(tr(
|
---|
41 | "Projection code mismatch in ''{0}'': toCode() returns ''{1}'', expected ''{2}''.",
|
---|
42 | proj.getClass().toString(), proj.toCode(), code));
|
---|
43 | ProjectionSubPrefs projSub = recreateProj((ProjectionSubPrefs)proj);
|
---|
44 | Collection<String> prefs = projSub.getPreferencesFromCode(code);
|
---|
45 | if (prefs != null) {
|
---|
46 | projSub.setPreferences(prefs);
|
---|
47 | }
|
---|
48 | if (!code.equals(projSub.toCode()))
|
---|
49 | throw new IllegalStateException(tr(
|
---|
50 | "Bad implementation of ''{0}'' projection class: cannot set preferences to match code ''{1}''.",
|
---|
51 | projSub.getClass().toString(), code));
|
---|
52 | allCodes.put(code, projSub);
|
---|
53 | return projSub;
|
---|
54 | }
|
---|
55 | }
|
---|