1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.projection;
|
---|
3 |
|
---|
4 | import java.util.Optional;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.data.projection.CustomProjection;
|
---|
7 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
8 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
9 |
|
---|
10 | public abstract class AbstractProjectionChoice implements ProjectionChoice {
|
---|
11 |
|
---|
12 | protected String name;
|
---|
13 | protected String id;
|
---|
14 | protected String cacheDir;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Constructs a new {@code AbstractProjectionChoice}.
|
---|
18 | *
|
---|
19 | * @param name short name of the projection choice as shown in the GUI
|
---|
20 | * @param id unique identifier for the projection choice
|
---|
21 | * @param cacheDir a cache directory name
|
---|
22 | */
|
---|
23 | public AbstractProjectionChoice(String name, String id, String cacheDir) {
|
---|
24 | this.name = name;
|
---|
25 | this.id = id;
|
---|
26 | this.cacheDir = cacheDir;
|
---|
27 | }
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Constructs a new {@code AbstractProjectionChoice}.
|
---|
31 | *
|
---|
32 | * Only for core projection choices, where chacheDir is the same as
|
---|
33 | * the second part of the id.
|
---|
34 | * @param name short name of the projection choice as shown in the GUI
|
---|
35 | * @param id unique identifier for the projection choice
|
---|
36 | */
|
---|
37 | public AbstractProjectionChoice(String name, String id) {
|
---|
38 | this(name, id, null);
|
---|
39 | if (!id.startsWith("core:")) throw new IllegalArgumentException(id+" does not start with core:");
|
---|
40 | this.cacheDir = id.substring(5);
|
---|
41 | }
|
---|
42 |
|
---|
43 | @Override
|
---|
44 | public String getId() {
|
---|
45 | return id;
|
---|
46 | }
|
---|
47 |
|
---|
48 | public String getCacheDir() {
|
---|
49 | return cacheDir;
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public String toString() {
|
---|
54 | return name;
|
---|
55 | }
|
---|
56 |
|
---|
57 | public abstract String getCurrentCode();
|
---|
58 |
|
---|
59 | public abstract String getProjectionName();
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public Projection getProjection() {
|
---|
63 | String code = getCurrentCode();
|
---|
64 | return new CustomProjection(getProjectionName(), code, Optional.ofNullable(Projections.getInit(code))
|
---|
65 | .orElseThrow(() -> new AssertionError("Error: Unknown projection code: " + code)), getCacheDir());
|
---|
66 | }
|
---|
67 | }
|
---|