001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.tagging.presets;
003
004import org.openstreetmap.josm.data.osm.IPrimitive;
005import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
006
007/**
008 * Enumeration of OSM primitive types associated with names and icons
009 * @since 6068
010 */
011public enum TaggingPresetType {
012    /** Node */
013    NODE(/* ICON */ "Mf_node", "node"),
014    /** Way */
015    WAY(/* ICON */ "Mf_way", "way"),
016    /** Relation */
017    RELATION(/* ICON */ "Mf_relation", "relation"),
018    /** Closed way */
019    CLOSEDWAY(/* ICON */ "Mf_closedway", "closedway"),
020    /** Multipolygon */
021    MULTIPOLYGON(/* ICON */ "Mf_multipolygon", "multipolygon");
022    private final String iconName;
023    private final String name;
024
025    TaggingPresetType(String iconName, String name) {
026        this.iconName = iconName + ".svg";
027        this.name = name;
028    }
029
030    /**
031     * Replies the SVG icon name.
032     * @return the SVG icon name
033     */
034    public String getIconName() {
035        return iconName;
036    }
037
038    /**
039     * Replies the name, as used in XML presets.
040     * @return the name: "node", "way", "relation" or "closedway"
041     */
042    public String getName() {
043        return name;
044    }
045
046    /**
047     * Determines the {@code TaggingPresetType} of a given primitive.
048     * @param p The OSM primitive
049     * @return the {@code TaggingPresetType} of {@code p}
050     */
051    public static TaggingPresetType forPrimitive(IPrimitive p) {
052        return forPrimitiveType(p.getDisplayType());
053    }
054
055    /**
056     * Determines the {@code TaggingPresetType} of a given primitive type.
057     * @param type The OSM primitive type
058     * @return the {@code TaggingPresetType} of {@code type}
059     */
060    public static TaggingPresetType forPrimitiveType(OsmPrimitiveType type) {
061        if (type == OsmPrimitiveType.NODE)
062            return NODE;
063        if (type == OsmPrimitiveType.WAY)
064            return WAY;
065        if (type == OsmPrimitiveType.CLOSEDWAY)
066            return CLOSEDWAY;
067        if (type == OsmPrimitiveType.MULTIPOLYGON)
068            return MULTIPOLYGON;
069        if (type == OsmPrimitiveType.RELATION)
070            return RELATION;
071        throw new IllegalArgumentException("Unexpected primitive type: " + type);
072    }
073
074    /**
075     * Determines the {@code TaggingPresetType} from a given string.
076     * @param type The OSM primitive type as string ("node", "way", "relation" or "closedway")
077     * @return the {@code TaggingPresetType} from {@code type}
078     */
079    public static TaggingPresetType fromString(String type) {
080        for (TaggingPresetType t : TaggingPresetType.values()) {
081            if (t.getName().equals(type)) {
082                return t;
083            }
084        }
085        return null;
086    }
087}