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