001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.preferences.projection;
003
004import org.openstreetmap.josm.data.projection.CustomProjection;
005import org.openstreetmap.josm.data.projection.Projection;
006import org.openstreetmap.josm.data.projection.Projections;
007
008public abstract class AbstractProjectionChoice implements ProjectionChoice {
009
010    protected String name;
011    protected String id;
012    protected String cacheDir;
013
014    /**
015     * Constructs a new {@code AbstractProjectionChoice}.
016     *
017     * @param name short name of the projection choice as shown in the GUI
018     * @param id unique identifier for the projection choice
019     * @param cacheDir a cache directory name
020     */
021    public AbstractProjectionChoice(String name, String id, String cacheDir) {
022        this.name = name;
023        this.id = id;
024        this.cacheDir = cacheDir;
025    }
026
027    /**
028     * Constructs a new {@code AbstractProjectionChoice}.
029     *
030     * Only for core projection choices, where chacheDir is the same as
031     * the second part of the id.
032     * @param name short name of the projection choice as shown in the GUI
033     * @param id unique identifier for the projection choice
034     */
035    public AbstractProjectionChoice(String name, String id) {
036        this(name, id, null);
037        if (!id.startsWith("core:")) throw new IllegalArgumentException(id+" does not start with core:");
038        this.cacheDir = id.substring(5);
039    }
040
041    @Override
042    public String getId() {
043        return id;
044    }
045
046    public String getCacheDir() {
047        return cacheDir;
048    }
049
050    @Override
051    public String toString() {
052        return name;
053    }
054
055    public abstract String getCurrentCode();
056
057    public abstract String getProjectionName();
058
059    @Override
060    public Projection getProjection() {
061        String code = getCurrentCode();
062        String pref = Projections.getInit(code);
063        if (pref == null)
064            throw new AssertionError("Error: Unknown projection code");
065        return new CustomProjection(getProjectionName(), code, pref, getCacheDir());
066    }
067}