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
008abstract public class AbstractProjectionChoice implements ProjectionChoice {
009
010    protected String name;
011    protected String id;
012    protected String cacheDir;
013
014    /**
015     * Constructor.
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     * Constructor (without cacheDir argument).
029     *
030     * Only for core projection choices, where chacheDir is the same as
031     * the second part of the id.
032     */
033    public AbstractProjectionChoice(String name, String id) {
034        this(name, id, null);
035        if (!id.startsWith("core:")) throw new IllegalArgumentException();
036        this.cacheDir = id.substring(5);
037    }
038
039    @Override
040    public String getId() {
041        return id;
042    }
043
044    public String getCacheDir() {
045        return cacheDir;
046    }
047
048    @Override
049    public String toString() {
050        return name;
051    }
052
053    abstract public String getCurrentCode();
054
055    abstract public String getProjectionName();
056
057    @Override
058    public Projection getProjection() {
059        String code = getCurrentCode();
060        String pref = Projections.getInit(code);
061        if (pref == null)
062            throw new AssertionError("Error: Unkown projection code");
063        return new CustomProjection(getProjectionName(), code, pref, getCacheDir());
064    }
065
066}